| author | Christian Urban <urbanc@in.tum.de> |
| Mon, 27 Aug 2012 10:24:10 +0100 | |
| changeset 535 | 5734ab5dd86d |
| parent 518 | 7ff1a681f758 |
| child 539 | 12861a362099 |
| permissions | -rw-r--r-- |
|
395
2c392f61f400
spilt the Essential's chapter
Christian Urban <urbanc@in.tum.de>
parents:
394
diff
changeset
|
1 |
theory Advanced |
| 441 | 2 |
imports Base First_Steps |
|
318
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
3 |
begin |
|
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
|
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
345
diff
changeset
|
5 |
|
| 414 | 6 |
chapter {* Advanced Isabelle\label{chp:advanced} *}
|
|
381
97518188ef0e
added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents:
380
diff
changeset
|
7 |
|
|
97518188ef0e
added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents:
380
diff
changeset
|
8 |
text {*
|
| 410 | 9 |
\begin{flushright}
|
10 |
{\em All things are difficult before they are easy.} \\[1ex]
|
|
11 |
proverb |
|
12 |
\end{flushright}
|
|
13 |
||
14 |
\medskip |
|
| 400 | 15 |
While terms, types and theorems are the most basic data structures in |
16 |
Isabelle, there are a number of layers built on top of them. Most of these |
|
| 408 | 17 |
layers are concerned with storing and manipulating data. Handling them |
| 487 | 18 |
properly is an essential skill for programming on the ML-level of Isabelle. |
19 |
The most basic layer are theories. They contain global data and |
|
| 408 | 20 |
can be seen as the ``long-term memory'' of Isabelle. There is usually only |
21 |
one theory active at each moment. Proof contexts and local theories, on the |
|
| 400 | 22 |
other hand, store local data for a task at hand. They act like the |
| 408 | 23 |
``short-term memory'' and there can be many of them that are active in |
24 |
parallel. |
|
|
318
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
25 |
*} |
|
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
26 |
|
| 506 | 27 |
section {* Theories\label{sec:theories} *}
|
| 358 | 28 |
|
29 |
text {*
|
|
| 492 | 30 |
Theories, as said above, are the most basic layer of abstraction in |
31 |
Isabelle. They record information about definitions, syntax declarations, axioms, |
|
| 488 | 32 |
theorems and much more. For example, if a definition is made, it |
33 |
must be stored in a theory in order to be usable later on. Similar |
|
34 |
with proofs: once a proof is finished, the proved theorem needs to |
|
35 |
be stored in the theorem database of the theory in order to be |
|
| 491 | 36 |
usable. All relevant data of a theory can be queried with the |
37 |
Isabelle command \isacommand{print\_theory}.
|
|
| 358 | 38 |
|
39 |
\begin{isabelle}
|
|
40 |
\isacommand{print\_theory}\\
|
|
41 |
@{text "> names: Pure Code_Generator HOL \<dots>"}\\
|
|
42 |
@{text "> classes: Inf < type \<dots>"}\\
|
|
43 |
@{text "> default sort: type"}\\
|
|
44 |
@{text "> syntactic types: #prop \<dots>"}\\
|
|
45 |
@{text "> logical types: 'a \<times> 'b \<dots>"}\\
|
|
46 |
@{text "> type arities: * :: (random, random) random \<dots>"}\\
|
|
47 |
@{text "> logical constants: == :: 'a \<Rightarrow> 'a \<Rightarrow> prop \<dots>"}\\
|
|
48 |
@{text "> abbreviations: \<dots>"}\\
|
|
49 |
@{text "> axioms: \<dots>"}\\
|
|
50 |
@{text "> oracles: \<dots>"}\\
|
|
51 |
@{text "> definitions: \<dots>"}\\
|
|
52 |
@{text "> theorems: \<dots>"}
|
|
53 |
\end{isabelle}
|
|
54 |
||
| 487 | 55 |
Functions acting on theories often end with the suffix @{text "_global"},
|
56 |
for example the function @{ML read_term_global in Syntax} in the structure
|
|
57 |
@{ML_struct Syntax}. The reason is to set them syntactically apart from
|
|
| 491 | 58 |
functions acting on contexts or local theories, which will be discussed in |
59 |
the next sections. There is a tendency amongst Isabelle developers to prefer |
|
| 487 | 60 |
``non-global'' operations, because they have some advantages, as we will also |
| 490 | 61 |
discuss later. However, some basic understanding of theories is still necessary |
62 |
for effective Isabelle programming. |
|
| 487 | 63 |
|
| 491 | 64 |
An important Isabelle command with theories is \isacommand{setup}. In the
|
| 492 | 65 |
previous chapters we used it already to make a theorem attribute known |
66 |
to Isabelle and to register a theorem under a name. What happens behind the |
|
| 490 | 67 |
scenes is that \isacommand{setup} expects a function of type @{ML_type
|
68 |
"theory -> theory"}: the input theory is the current theory and the output |
|
69 |
the theory where the attribute has been registered or the theorem has been |
|
70 |
stored. This is a fundamental principle in Isabelle. A similar situation |
|
| 491 | 71 |
arises with declaring a constant, which can be done on the ML-level with |
72 |
function @{ML_ind declare_const in Sign} from the structure @{ML_struct
|
|
| 490 | 73 |
Sign}. To see how \isacommand{setup} works, consider the following code:
|
74 |
||
| 348 | 75 |
*} |
76 |
||
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
77 |
ML %grayML{*let
|
|
485
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
78 |
val thy = @{theory}
|
|
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
79 |
val bar_const = ((@{binding "BAR"}, @{typ "nat"}), NoSyn)
|
|
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
80 |
in |
|
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
81 |
Sign.declare_const @{context} bar_const thy
|
|
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
82 |
end*} |
| 348 | 83 |
|
84 |
text {*
|
|
| 488 | 85 |
If you simply run this code\footnote{Recall that ML-code needs to be enclosed in
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
86 |
\isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} with the
|
| 491 | 87 |
intention of declaring a constant @{text "BAR"} having type @{typ nat}, then
|
| 488 | 88 |
indeed you obtain a theory as result. But if you query the |
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
89 |
constant on the Isabelle level using the command \isacommand{term}
|
| 348 | 90 |
|
91 |
\begin{isabelle}
|
|
|
485
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
92 |
\isacommand{term}~@{text BAR}\\
|
| 348 | 93 |
@{text "> \"BAR\" :: \"'a\""}
|
94 |
\end{isabelle}
|
|
95 |
||
| 495 | 96 |
you can see that you do \emph{not} obtain the expected constant of type @{typ nat}, but a free
|
| 484 | 97 |
variable (printed in blue) of polymorphic type. The problem is that the |
98 |
ML-expression above did not ``register'' the declaration with the current theory. |
|
99 |
This is what the command \isacommand{setup} is for. The constant is properly
|
|
100 |
declared with |
|
| 348 | 101 |
*} |
102 |
||
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
103 |
setup %gray {* fn thy =>
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
104 |
let |
|
485
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
105 |
val bar_const = ((@{binding "BAR"}, @{typ "nat"}), NoSyn)
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
106 |
val (_, thy') = Sign.declare_const @{context} bar_const thy
|
|
485
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
107 |
in |
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
108 |
thy' |
|
485
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
109 |
end *} |
| 348 | 110 |
|
111 |
text {*
|
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
112 |
where the declaration is actually applied to the current theory and |
| 348 | 113 |
|
114 |
\begin{isabelle}
|
|
115 |
\isacommand{term}~@{text [quotes] "BAR"}\\
|
|
116 |
@{text "> \"BAR\" :: \"nat\""}
|
|
117 |
\end{isabelle}
|
|
118 |
||
| 492 | 119 |
now returns a (black) constant with the type @{typ nat}, as expected.
|
| 348 | 120 |
|
| 491 | 121 |
In a sense, \isacommand{setup} can be seen as a transaction that
|
122 |
takes the current theory @{text thy}, applies an operation, and
|
|
123 |
produces a new current theory @{text thy'}. This means that we have
|
|
124 |
to be careful to apply operations always to the most current theory, |
|
125 |
not to a \emph{stale} one. Consider again the function inside the
|
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
126 |
\isacommand{setup}-command:
|
|
485
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
127 |
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
128 |
\begin{isabelle}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
129 |
\begin{graybox}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
130 |
\isacommand{setup}~@{text "\<verbopen>"} @{ML
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
131 |
"fn thy => |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
132 |
let |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
133 |
val bar_const = ((@{binding \"BAR\"}, @{typ \"nat\"}), NoSyn)
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
134 |
val (_, thy') = Sign.declare_const @{context} bar_const thy
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
135 |
in |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
136 |
thy |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
137 |
end"}~@{text "\<verbclose>"}\isanewline
|
| 495 | 138 |
@{text "> ERROR: \"Stale theory encountered\""}
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
139 |
\end{graybox}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
140 |
\end{isabelle}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
141 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
142 |
This time we erroneously return the original theory @{text thy}, instead of
|
| 488 | 143 |
the modified one @{text thy'}. Such buggy code will always result into
|
144 |
a runtime error message about stale theories. |
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
145 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
146 |
However, sometimes it does make sense to work with two theories at the same |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
147 |
time, especially in the context of parsing and typing. In the code below we |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
148 |
use in Line 3 the function @{ML_ind copy in Theory} from the structure
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
149 |
@{ML_struct Theory} for obtaining a new theory that contains the same
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
150 |
data, but is unrelated to the existing theory. |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
151 |
*} |
|
485
f3536f0b47a9
added section about testboard
Christian Urban <urbanc@in.tum.de>
parents:
484
diff
changeset
|
152 |
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
153 |
setup %graylinenos {* fn thy =>
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
154 |
let |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
155 |
val tmp_thy = Theory.copy thy |
| 498 | 156 |
val foo_const = ((@{binding "FOO"}, @{typ "nat \<Rightarrow> nat"}), NoSyn)
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
157 |
val (_, tmp_thy') = Sign.declare_const @{context} foo_const tmp_thy
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
158 |
val trm1 = Syntax.read_term_global tmp_thy' "FOO baz" |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
159 |
val trm2 = Syntax.read_term_global thy "FOO baz" |
| 502 | 160 |
val _ = pwriteln |
161 |
(Pretty.str (@{make_string} trm1 ^ "\n" ^ @{make_string} trm2))
|
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
162 |
in |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
163 |
thy |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
164 |
end *} |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
165 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
166 |
text {*
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
167 |
That means we can make changes to the theory @{text tmp_thy} without
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
168 |
affecting the current theory @{text thy}. In this case we declare in @{text
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
169 |
"tmp_thy"} the constant @{text FOO} (Lines 4 and 5). The point of this code
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
170 |
is that we next, in Lines 6 and 7, parse a string to become a term (both |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
171 |
times the string is @{text [quotes] "FOO baz"}). But since we parse the string
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
172 |
once in the context of the theory @{text tmp_thy'} in which @{text FOO} is
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
173 |
declared to be a constant of type @{typ "nat \<Rightarrow>nat"} and once in the context
|
| 488 | 174 |
of @{text thy} where it is not, we obtain two different terms, namely
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
175 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
176 |
\begin{isabelle}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
177 |
\begin{graybox}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
178 |
@{text "> Const (\"Advanced.FOO\", \"nat \<Rightarrow> nat\") $ Free (\"baz\", \"nat\")"}\isanewline
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
179 |
@{text "> Free (\"FOO\", \"'a \<Rightarrow> 'b\") $ Free (\"baz\", \"'a\")"}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
180 |
\end{graybox}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
181 |
\end{isabelle}
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
182 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
183 |
There are two reasons for parsing a term in a temporary theory. One is to |
| 488 | 184 |
obtain fully qualified names for constants and the other is appropriate type |
185 |
inference. This is relevant in situations where definitions are made later, |
|
186 |
but parsing and type inference has to take already proceed as if the definitions |
|
187 |
were already made. |
|
| 502 | 188 |
|
189 |
\begin{readmore}
|
|
190 |
Most of the functions about theories are implemented in |
|
191 |
@{ML_file "Pure/theory.ML"} and @{ML_file "Pure/global_theory.ML"}.
|
|
192 |
\end{readmore}
|
|
| 348 | 193 |
*} |
|
318
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
194 |
|
| 495 | 195 |
section {* Contexts *}
|
|
341
62dea749d5ed
more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents:
340
diff
changeset
|
196 |
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
197 |
text {*
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
198 |
Contexts are arguably more important than theories, even though they only |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
199 |
contain ``short-term memory data''. The reason is that a vast number of |
| 490 | 200 |
functions in Isabelle depend in one way or another on contexts. Even such |
201 |
mundane operations like printing out a term make essential use of contexts. |
|
| 494 | 202 |
For this consider the following contrived proof-snippet whose purpose is to |
| 490 | 203 |
fix two variables: |
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
204 |
*} |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
205 |
|
| 488 | 206 |
lemma "True" |
207 |
proof - |
|
| 492 | 208 |
ML_prf {* Variable.dest_fixes @{context} *}
|
| 518 | 209 |
fix x y |
210 |
ML_prf {* Variable.dest_fixes @{context} *}(*<*)oops(*>*)
|
|
| 488 | 211 |
|
| 490 | 212 |
text {*
|
| 496 | 213 |
The interesting point is that we injected ML-code before and after |
| 490 | 214 |
the variables are fixed. For this remember that ML-code inside a proof |
| 494 | 215 |
needs to be enclosed inside \isacommand{ML\_prf}~@{text "\<verbopen> \<dots> \<verbclose>"},
|
| 490 | 216 |
not \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}. The function
|
217 |
@{ML_ind dest_fixes in Variable} from the structure @{ML_struct Variable} takes
|
|
218 |
a context and returns all its currently fixed variable (names). That |
|
219 |
means a context has a dataslot containing information about fixed variables. |
|
220 |
The ML-antiquotation @{text "@{context}"} points to the context that is
|
|
221 |
active at that point of the theory. Consequently, in the first call to |
|
222 |
@{ML dest_fixes in Variable} this dataslot is empty; in the second it is
|
|
223 |
filled with @{text x} and @{text y}. What is interesting is that contexts
|
|
| 494 | 224 |
can be stacked. For this consider the following proof fragment: |
| 490 | 225 |
*} |
226 |
||
227 |
lemma "True" |
|
228 |
proof - |
|
229 |
fix x y |
|
230 |
{ fix z w
|
|
| 518 | 231 |
ML_prf {* Variable.dest_fixes @{context} *}
|
232 |
} |
|
233 |
ML_prf {* Variable.dest_fixes @{context} *}(*<*)oops(*>*)
|
|
| 491 | 234 |
|
235 |
text {*
|
|
| 495 | 236 |
Here the first time we call @{ML dest_fixes in Variable} we have four fixes variables;
|
237 |
the second time we get only the fixes variables @{text x} and @{text y} as answer, since
|
|
238 |
@{text z} and @{text w} are not anymore in the scope of the context.
|
|
239 |
This means the curly-braces act on the Isabelle level as opening and closing statements |
|
| 496 | 240 |
for a context. The above proof fragment corresponds roughly to the following ML-code |
| 491 | 241 |
*} |
| 488 | 242 |
|
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
243 |
ML %grayML{*val ctxt0 = @{context};
|
| 492 | 244 |
val ([x, y], ctxt1) = Variable.add_fixes ["x", "y"] ctxt0; |
245 |
val ([z, w], ctxt2) = Variable.add_fixes ["z", "w"] ctxt1*} |
|
246 |
||
247 |
text {*
|
|
| 494 | 248 |
where the function @{ML_ind add_fixes in Variable} fixes a list of variables
|
249 |
specified by strings. Let us come back to the point about printing terms. Consider |
|
| 495 | 250 |
printing out the term \mbox{@{text "(x, y, z, w)"}} using our function @{ML_ind pretty_term}.
|
| 496 | 251 |
This function takes a term and a context as argument. Notice how the printing |
| 498 | 252 |
of the term changes according to which context is used. |
| 492 | 253 |
|
| 495 | 254 |
\begin{isabelle}
|
255 |
\begin{graybox}
|
|
256 |
@{ML "let
|
|
257 |
val trm = @{term \"(x, y, z, w)\"}
|
|
| 493 | 258 |
in |
259 |
pwriteln (Pretty.chunks |
|
260 |
[ pretty_term ctxt0 trm, |
|
261 |
pretty_term ctxt1 trm, |
|
262 |
pretty_term ctxt2 trm ]) |
|
| 495 | 263 |
end"}\\ |
264 |
\setlength{\fboxsep}{0mm}
|
|
265 |
@{text ">"}~@{text "("}\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text x}}}@{text ","}~%
|
|
266 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text y}}}@{text ","}~%
|
|
267 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text z}}}@{text ","}~%
|
|
268 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text w}}}@{text ")"}\\
|
|
269 |
@{text ">"}~@{text "("}\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text x}}}@{text ","}~%
|
|
270 |
\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text y}}}@{text ","}~%
|
|
271 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text z}}}@{text ","}~%
|
|
272 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text w}}}@{text ")"}\\
|
|
273 |
@{text ">"}~@{text "("}\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text x}}}@{text ","}~%
|
|
274 |
\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text y}}}@{text ","}~%
|
|
275 |
\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text z}}}@{text ","}~%
|
|
276 |
\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text w}}}@{text ")"}
|
|
277 |
\end{graybox}
|
|
278 |
\end{isabelle}
|
|
279 |
||
280 |
||
281 |
The term we are printing is in all three cases the same---a tuple containing |
|
| 496 | 282 |
four free variables. As you can see, however, in case of @{ML "ctxt0"} all
|
| 495 | 283 |
variables are highlighted indicating a problem, while in case of @{ML
|
284 |
"ctxt1"} @{text x} and @{text y} are printed as normal (blue) free
|
|
285 |
variables, but not @{text z} and @{text w}. In the last case all variables
|
|
286 |
are printed as expected. The point of this example is that the context |
|
287 |
contains the information which variables are fixed, and designates all other |
|
| 497 | 288 |
free variables as being alien or faulty. Therefore the highlighting. |
289 |
While this seems like a minor detail, the concept of making the context aware |
|
290 |
of fixed variables is actually quite useful. For example it prevents us from |
|
| 501 | 291 |
fixing a variable twice |
| 495 | 292 |
|
293 |
@{ML_response_fake [gray, display]
|
|
294 |
"@{context}
|
|
295 |
|> Variable.add_fixes [\"x\", \"x\"]" |
|
296 |
"ERROR: Duplicate fixed variable(s): \"x\""} |
|
297 |
||
| 501 | 298 |
More importantly it also allows us to easily create fresh names for |
299 |
fixed variables. For this you have to use the function @{ML_ind
|
|
300 |
variant_fixes in Variable} from the structure @{ML_struct Variable}.
|
|
301 |
||
302 |
@{ML_response_fake [gray, display]
|
|
303 |
"@{context}
|
|
304 |
|> Variable.variant_fixes [\"y\", \"y\", \"z\"]" |
|
305 |
"([\"y\", \"ya\", \"z\"], ...)"} |
|
306 |
||
307 |
Now a fresh variant for the second occurence of @{text y} is created
|
|
| 502 | 308 |
avoiding any clash. In this way we can also create fresh free variables |
| 501 | 309 |
that avoid any clashes with fixed variables. In Line~3 below we fix |
310 |
the variable @{text x} in the context @{text ctxt1}. Next we want to
|
|
311 |
create two fresh variables of type @{typ nat} as variants of the
|
|
312 |
string @{text [quotes] "x"} (Lines 6 and 7).
|
|
| 495 | 313 |
|
314 |
@{ML_response_fake [display, gray, linenos]
|
|
315 |
"let |
|
316 |
val ctxt0 = @{context}
|
|
317 |
val (_, ctxt1) = Variable.add_fixes [\"x\"] ctxt0 |
|
318 |
val frees = replicate 2 (\"x\", @{typ nat})
|
|
319 |
in |
|
320 |
(Variable.variant_frees ctxt0 [] frees, |
|
321 |
Variable.variant_frees ctxt1 [] frees) |
|
322 |
end" |
|
323 |
"([(\"x\", \"nat\"), (\"xa\", \"nat\")], |
|
324 |
[(\"xa\", \"nat\"), (\"xb\", \"nat\")])"} |
|
325 |
||
| 496 | 326 |
As you can see, if we create the fresh variables with the context @{text ctxt0},
|
| 495 | 327 |
then we obtain @{text "x"} and @{text "xa"}; but in @{text ctxt1} we obtain @{text "xa"}
|
328 |
and @{text "xb"} avoiding @{text x}, which was fixed in this context.
|
|
329 |
||
| 496 | 330 |
Often one has the problem that given some terms, create fresh variables |
331 |
avoiding any variable occurring in those terms. For this you can use the |
|
| 495 | 332 |
function @{ML_ind declare_term in Variable} from the structure @{ML_struct Variable}.
|
333 |
||
334 |
@{ML_response_fake [gray, display]
|
|
335 |
"let |
|
336 |
val ctxt1 = Variable.declare_term @{term \"(x, xa)\"} @{context}
|
|
337 |
val frees = replicate 2 (\"x\", @{typ nat})
|
|
338 |
in |
|
339 |
Variable.variant_frees ctxt1 [] frees |
|
340 |
end" |
|
341 |
"[(\"xb\", \"nat\"), (\"xc\", \"nat\")]"} |
|
342 |
||
| 496 | 343 |
The result is @{text xb} and @{text xc} for the names of the fresh
|
| 498 | 344 |
variables, since @{text x} and @{text xa} occur in the term we declared.
|
345 |
Note that @{ML_ind declare_term in Variable} does not fix the
|
|
346 |
variables; it just makes them ``known'' to the context. You can see |
|
347 |
that if you print out a declared term. |
|
348 |
||
349 |
\begin{isabelle}
|
|
350 |
\begin{graybox}
|
|
351 |
@{ML "let
|
|
352 |
val trm = @{term \"P x y z\"}
|
|
353 |
val ctxt1 = Variable.declare_term trm @{context}
|
|
354 |
in |
|
355 |
pwriteln (pretty_term ctxt1 trm) |
|
356 |
end"}\\ |
|
357 |
\setlength{\fboxsep}{0mm}
|
|
358 |
@{text ">"}~\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text P}}}~%
|
|
359 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text x}}}~%
|
|
360 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text y}}}~%
|
|
361 |
\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text z}}}
|
|
362 |
\end{graybox}
|
|
363 |
\end{isabelle}
|
|
364 |
||
365 |
All variables are highligted, indicating that they are not |
|
366 |
fixed. However, declaring a term is helpful when parsing terms using |
|
367 |
the function @{ML_ind read_term in Syntax} from the structure
|
|
368 |
@{ML_struct Syntax}. Consider the following code:
|
|
| 495 | 369 |
|
370 |
@{ML_response_fake [gray, display]
|
|
371 |
"let |
|
372 |
val ctxt0 = @{context}
|
|
373 |
val ctxt1 = Variable.declare_term @{term \"x::nat\"} ctxt0
|
|
374 |
in |
|
375 |
(Syntax.read_term ctxt0 \"x\", |
|
376 |
Syntax.read_term ctxt1 \"x\") |
|
377 |
end" |
|
378 |
"(Free (\"x\", \"'a\"), Free (\"x\", \"nat\"))"} |
|
379 |
||
| 496 | 380 |
Parsing the string in the context @{text "ctxt0"} results in a free variable
|
381 |
with a default polymorphic type, but in case of @{text "ctxt1"} we obtain a
|
|
| 495 | 382 |
free variable of type @{typ nat} as previously declared. Which
|
| 496 | 383 |
type the parsed term receives depends upon the last declaration that |
384 |
is made, as the next example illustrates. |
|
| 495 | 385 |
|
386 |
@{ML_response_fake [gray, display]
|
|
387 |
"let |
|
388 |
val ctxt1 = Variable.declare_term @{term \"x::nat\"} @{context}
|
|
389 |
val ctxt2 = Variable.declare_term @{term \"x::int\"} ctxt1
|
|
390 |
in |
|
391 |
(Syntax.read_term ctxt1 \"x\", |
|
392 |
Syntax.read_term ctxt2 \"x\") |
|
393 |
end" |
|
394 |
"(Free (\"x\", \"nat\"), Free (\"x\", \"int\"))"} |
|
395 |
||
| 499 | 396 |
The most useful feature of contexts is that one can export, or transfer, |
397 |
terms and theorems between them. We show this first for terms. |
|
| 497 | 398 |
|
399 |
\begin{isabelle}
|
|
400 |
\begin{graybox}
|
|
401 |
\begin{linenos}
|
|
402 |
@{ML "let
|
|
403 |
val ctxt0 = @{context}
|
|
404 |
val (_, ctxt1) = Variable.add_fixes [\"x\", \"y\", \"z\"] ctxt0 |
|
405 |
val foo_trm = @{term \"P x y z\"}
|
|
406 |
in |
|
407 |
singleton (Variable.export_terms ctxt1 ctxt0) foo_trm |
|
408 |
|> pretty_term ctxt0 |
|
409 |
|> pwriteln |
|
410 |
end"} |
|
411 |
\end{linenos}
|
|
412 |
\setlength{\fboxsep}{0mm}
|
|
413 |
@{text ">"}~\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text P}}}~%
|
|
414 |
@{text "?x ?y ?z"}
|
|
415 |
\end{graybox}
|
|
416 |
\end{isabelle}
|
|
417 |
||
| 498 | 418 |
In Line 3 we fix the variables @{term x}, @{term y} and @{term z} in
|
419 |
context @{text ctxt1}. The function @{ML_ind export_terms in
|
|
| 500 | 420 |
Variable} from the structure @{ML_struct Variable} can be used to transfer
|
| 498 | 421 |
terms between contexts. Transferring means to turn all (free) |
422 |
variables that are fixed in one context, but not in the other, into |
|
423 |
schematic variables. In our example, we are transferring the term |
|
424 |
@{text "P x y z"} from context @{text "ctxt1"} to @{text "ctxt0"},
|
|
425 |
which means @{term x}, @{term y} and @{term z} become schematic
|
|
| 500 | 426 |
variables (as can be seen by the leading question marks in the result). |
427 |
Note that the variable @{text P} stays a free variable, since it not fixed in
|
|
| 498 | 428 |
@{text ctxt1}; it is even highlighed, because @{text "ctxt0"} does
|
429 |
not know about it. Note also that in Line 6 we had to use the |
|
430 |
function @{ML_ind singleton}, because the function @{ML_ind
|
|
431 |
export_terms in Variable} normally works over lists of terms. |
|
432 |
||
433 |
The case of transferring theorems is even more useful. The reason is |
|
434 |
that the generalisation of fixed variables to schematic variables is |
|
| 499 | 435 |
not trivial if done manually. For illustration purposes we use in the |
436 |
following code the function @{ML_ind make_thm in Skip_Proof} from the
|
|
| 500 | 437 |
structure @{ML_struct Skip_Proof}. This function will turn an arbitray
|
438 |
term, in our case @{term "P x y z x y z"}, into a theorem (disregarding
|
|
439 |
whether it is actually provable). |
|
| 498 | 440 |
|
441 |
@{ML_response_fake [display, gray]
|
|
442 |
"let |
|
443 |
val thy = @{theory}
|
|
444 |
val ctxt0 = @{context}
|
|
| 499 | 445 |
val (_, ctxt1) = Variable.add_fixes [\"P\", \"x\", \"y\", \"z\"] ctxt0 |
| 498 | 446 |
val foo_thm = Skip_Proof.make_thm thy @{prop \"P x y z x y z\"}
|
447 |
in |
|
448 |
singleton (Proof_Context.export ctxt1 ctxt0) foo_thm |
|
449 |
end" |
|
| 499 | 450 |
"?P ?x ?y ?z ?x ?y ?z"} |
451 |
||
| 502 | 452 |
Since we fixed all variables in @{text ctxt1}, in the exported
|
453 |
result all of them are schematic. The great point of contexts is |
|
454 |
that exporting from one to another is not just restricted to |
|
455 |
variables, but also works with assumptions. For this we can use the |
|
456 |
function @{ML_ind export in Assumption} from the structure
|
|
| 500 | 457 |
@{ML_struct Assumption}. Consider the following code.
|
458 |
||
459 |
@{ML_response_fake [display, gray, linenos]
|
|
460 |
"let |
|
461 |
val ctxt0 = @{context}
|
|
462 |
val ([eq], ctxt1) = Assumption.add_assumes [@{cprop \"x \<equiv> y\"}] ctxt0
|
|
463 |
val eq' = Thm.symmetric eq |
|
464 |
in |
|
465 |
Assumption.export false ctxt1 ctxt0 eq' |
|
466 |
end" |
|
467 |
"x \<equiv> y \<Longrightarrow> y \<equiv> x"} |
|
468 |
||
469 |
The function @{ML_ind add_assumes in Assumption} from the structure
|
|
470 |
@{ML_struct Assumption} adds the assumption \mbox{@{text "x \<equiv> y"}}
|
|
471 |
to the context @{text ctxt1} (Line 3). This function expects a list
|
|
472 |
of @{ML_type cterm}s and returns them as theorems, together with the
|
|
473 |
new context in which they are ``assumed''. In Line 4 we use the |
|
474 |
function @{ML_ind symmetric in Thm} from the structure @{ML_struct
|
|
475 |
Thm} in order to obtain the symmetric version of the assumed |
|
476 |
meta-equality. Now exporting the theorem @{text "eq'"} from @{text
|
|
477 |
ctxt1} to @{text ctxt0} means @{term "y \<equiv> x"} will be prefixed with
|
|
478 |
the assumed theorem. The boolean flag in @{ML_ind export in
|
|
479 |
Assumption} indicates whether the assumptions should be marked with |
|
480 |
the goal marker (see Section~\ref{sec:basictactics}). In normal
|
|
481 |
circumstances this is not necessary and so should be set to @{ML
|
|
482 |
false}. The result of the export is then the theorem \mbox{@{term
|
|
483 |
"x \<equiv> y \<Longrightarrow> y \<equiv> x"}}. As can be seen this is an easy way for obtaing |
|
484 |
simple theorems. We will explain this in more detail in |
|
485 |
Section~\ref{sec:structured}.
|
|
486 |
||
487 |
The function @{ML_ind export in Proof_Context} from the structure
|
|
488 |
@{ML_struct Proof_Context} combines both export functions from
|
|
489 |
@{ML_struct Variable} and @{ML_struct Assumption}. This can be seen
|
|
490 |
in the following example. |
|
491 |
||
492 |
@{ML_response_fake [display, gray]
|
|
493 |
"let |
|
494 |
val ctxt0 = @{context}
|
|
495 |
val ((fvs, [eq]), ctxt1) = ctxt0 |
|
496 |
|> Variable.add_fixes [\"x\", \"y\"] |
|
497 |
||>> Assumption.add_assumes [@{cprop \"x \<equiv> y\"}]
|
|
498 |
val eq' = Thm.symmetric eq |
|
499 |
in |
|
500 |
Proof_Context.export ctxt1 ctxt0 [eq'] |
|
501 |
end" |
|
502 |
"[?x \<equiv> ?y \<Longrightarrow> ?y \<equiv> ?x]"} |
|
| 495 | 503 |
*} |
504 |
||
| 496 | 505 |
|
| 493 | 506 |
|
| 495 | 507 |
text {*
|
| 493 | 508 |
|
509 |
*} |
|
510 |
||
| 492 | 511 |
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
512 |
(* |
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
513 |
ML %grayML{*Proof_Context.debug := true*}
|
|
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
514 |
ML %grayML{*Proof_Context.verbose := true*}
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
515 |
*) |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
516 |
|
| 487 | 517 |
(* |
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
518 |
lemma "True" |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
519 |
proof - |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
520 |
{ -- "\<And>x. _"
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
521 |
fix x |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
522 |
have "B x" sorry |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
523 |
thm this |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
524 |
} |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
525 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
526 |
thm this |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
527 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
528 |
{ -- "A \<Longrightarrow> _"
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
529 |
assume A |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
530 |
have B sorry |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
531 |
thm this |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
532 |
} |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
533 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
534 |
thm this |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
535 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
536 |
{ -- "\<And>x. x = _ \<Longrightarrow> _"
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
537 |
def x \<equiv> a |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
538 |
have "B x" sorry |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
539 |
} |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
540 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
541 |
thm this |
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
542 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
543 |
oops |
| 487 | 544 |
*) |
| 413 | 545 |
|
| 502 | 546 |
section {* Local Theories and Local Setups\label{sec:local} (TBD) *}
|
|
341
62dea749d5ed
more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents:
340
diff
changeset
|
547 |
|
| 394 | 548 |
text {*
|
| 400 | 549 |
In contrast to an ordinary theory, which simply consists of a type |
550 |
signature, as well as tables for constants, axioms and theorems, a local |
|
551 |
theory contains additional context information, such as locally fixed |
|
552 |
variables and local assumptions that may be used by the package. The type |
|
553 |
@{ML_type local_theory} is identical to the type of \emph{proof contexts}
|
|
554 |
@{ML_type "Proof.context"}, although not every proof context constitutes a
|
|
555 |
valid local theory. |
|
556 |
||
557 |
@{ML "Context.>> o Context.map_theory"}
|
|
| 394 | 558 |
@{ML_ind "Local_Theory.declaration"}
|
|
486
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
559 |
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
560 |
A similar command is \isacommand{local\_setup}, which expects a function
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
561 |
of type @{ML_type "local_theory -> local_theory"}. Later on we will also
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
562 |
use the commands \isacommand{method\_setup} for installing methods in the
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
563 |
current theory and \isacommand{simproc\_setup} for adding new simprocs to
|
|
45cfd2ece7bd
a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents:
485
diff
changeset
|
564 |
the current simpset. |
| 394 | 565 |
*} |
|
318
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
566 |
|
|
336
a12bb28fe2bd
polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents:
335
diff
changeset
|
567 |
|
| 394 | 568 |
section {* Morphisms (TBD) *}
|
569 |
||
570 |
text {*
|
|
571 |
Morphisms are arbitrary transformations over terms, types, theorems and bindings. |
|
572 |
They can be constructed using the function @{ML_ind morphism in Morphism},
|
|
573 |
which expects a record with functions of type |
|
574 |
||
575 |
\begin{isabelle}
|
|
576 |
\begin{tabular}{rl}
|
|
577 |
@{text "binding:"} & @{text "binding -> binding"}\\
|
|
578 |
@{text "typ:"} & @{text "typ -> typ"}\\
|
|
579 |
@{text "term:"} & @{text "term -> term"}\\
|
|
580 |
@{text "fact:"} & @{text "thm list -> thm list"}
|
|
581 |
\end{tabular}
|
|
582 |
\end{isabelle}
|
|
583 |
||
584 |
The simplest morphism is the @{ML_ind identity in Morphism}-morphism defined as
|
|
585 |
*} |
|
586 |
||
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
587 |
ML %grayML{*val identity = Morphism.morphism {binding = [], typ = [], term = [], fact = []}*}
|
| 394 | 588 |
|
589 |
text {*
|
|
590 |
Morphisms can be composed with the function @{ML_ind "$>" in Morphism}
|
|
591 |
*} |
|
592 |
||
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
593 |
ML %grayML{*fun trm_phi (Free (x, T)) = Var ((x, 0), T)
|
| 394 | 594 |
| trm_phi (Abs (x, T, t)) = Abs (x, T, trm_phi t) |
595 |
| trm_phi (t $ s) = (trm_phi t) $ (trm_phi s) |
|
596 |
| trm_phi t = t*} |
|
597 |
||
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
598 |
ML %grayML{*val phi = Morphism.term_morphism trm_phi*}
|
| 394 | 599 |
|
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
514
diff
changeset
|
600 |
ML %grayML{*Morphism.term phi @{term "P x y"}*}
|
| 394 | 601 |
|
602 |
text {*
|
|
603 |
@{ML_ind term_morphism in Morphism}
|
|
604 |
||
605 |
@{ML_ind term in Morphism},
|
|
606 |
@{ML_ind thm in Morphism}
|
|
607 |
||
608 |
\begin{readmore}
|
|
609 |
Morphisms are implemented in the file @{ML_file "Pure/morphism.ML"}.
|
|
610 |
\end{readmore}
|
|
611 |
*} |
|
|
318
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
612 |
|
|
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
613 |
section {* Misc (TBD) *}
|
|
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
614 |
|
|
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
615 |
ML {*Datatype.get_info @{theory} "List.list"*}
|
|
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
616 |
|
|
319
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
617 |
text {*
|
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
618 |
FIXME: association lists: |
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
619 |
@{ML_file "Pure/General/alist.ML"}
|
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
620 |
|
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
621 |
FIXME: calling the ML-compiler |
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
622 |
|
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
623 |
*} |
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
318
diff
changeset
|
624 |
|
| 414 | 625 |
section {* What Is In an Isabelle Name? (TBD) *}
|
626 |
||
627 |
text {*
|
|
628 |
On the ML-level of Isabelle, you often have to work with qualified names. |
|
629 |
These are strings with some additional information, such as positional |
|
630 |
information and qualifiers. Such qualified names can be generated with the |
|
631 |
antiquotation @{text "@{binding \<dots>}"}. For example
|
|
632 |
||
633 |
@{ML_response [display,gray]
|
|
634 |
"@{binding \"name\"}"
|
|
635 |
"name"} |
|
636 |
||
637 |
An example where a qualified name is needed is the function |
|
638 |
@{ML_ind define in Local_Theory}. This function is used below to define
|
|
639 |
the constant @{term "TrueConj"} as the conjunction @{term "True \<and> True"}.
|
|
640 |
*} |
|
641 |
||
642 |
local_setup %gray {*
|
|
643 |
Local_Theory.define ((@{binding "TrueConj"}, NoSyn),
|
|
|
514
7e25716c3744
updated to outer syntax / parser changes
Christian Urban <urbanc@in.tum.de>
parents:
506
diff
changeset
|
644 |
((@{binding "TrueConj_def"}, []), @{term "True \<and> True"})) #> snd *}
|
| 414 | 645 |
|
646 |
text {*
|
|
647 |
Now querying the definition you obtain: |
|
648 |
||
649 |
\begin{isabelle}
|
|
650 |
\isacommand{thm}~@{text "TrueConj_def"}\\
|
|
651 |
@{text "> "}~@{thm TrueConj_def}
|
|
652 |
\end{isabelle}
|
|
653 |
||
654 |
\begin{readmore}
|
|
655 |
The basic operations on bindings are implemented in |
|
656 |
@{ML_file "Pure/General/binding.ML"}.
|
|
657 |
\end{readmore}
|
|
658 |
||
659 |
\footnote{\bf FIXME give a better example why bindings are important}
|
|
660 |
\footnote{\bf FIXME give a pointer to \isacommand{local\_setup}; if not, then explain
|
|
661 |
why @{ML snd} is needed.}
|
|
662 |
\footnote{\bf FIXME: There should probably a separate section on binding, long-names
|
|
663 |
and sign.} |
|
664 |
||
665 |
*} |
|
666 |
||
|
318
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
667 |
|
| 360 | 668 |
ML {* Sign.intern_type @{theory} "list" *}
|
669 |
ML {* Sign.intern_const @{theory} "prod_fun" *}
|
|
670 |
||
| 414 | 671 |
text {*
|
672 |
\footnote{\bf FIXME: Explain the following better; maybe put in a separate
|
|
673 |
section and link with the comment in the antiquotation section.} |
|
674 |
||
675 |
Occasionally you have to calculate what the ``base'' name of a given |
|
| 462 | 676 |
constant is. For this you can use the function @{ML_ind Long_Name.base_name}. For example:
|
| 414 | 677 |
|
| 462 | 678 |
@{ML_response [display,gray] "Long_Name.base_name \"List.list.Nil\"" "\"Nil\""}
|
| 414 | 679 |
|
680 |
\begin{readmore}
|
|
681 |
Functions about naming are implemented in @{ML_file "Pure/General/name_space.ML"};
|
|
682 |
functions about signatures in @{ML_file "Pure/sign.ML"}.
|
|
683 |
\end{readmore}
|
|
684 |
*} |
|
|
387
5dcee4d751ad
completed the unification section
Christian Urban <urbanc@in.tum.de>
parents:
386
diff
changeset
|
685 |
|
|
5dcee4d751ad
completed the unification section
Christian Urban <urbanc@in.tum.de>
parents:
386
diff
changeset
|
686 |
text {*
|
|
5dcee4d751ad
completed the unification section
Christian Urban <urbanc@in.tum.de>
parents:
386
diff
changeset
|
687 |
@{ML_ind "Binding.name_of"} returns the string without markup
|
| 394 | 688 |
|
689 |
@{ML_ind "Binding.conceal"}
|
|
|
387
5dcee4d751ad
completed the unification section
Christian Urban <urbanc@in.tum.de>
parents:
386
diff
changeset
|
690 |
*} |
|
5dcee4d751ad
completed the unification section
Christian Urban <urbanc@in.tum.de>
parents:
386
diff
changeset
|
691 |
|
|
388
0b337dedc306
added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents:
387
diff
changeset
|
692 |
section {* Concurrency (TBD) *}
|
|
0b337dedc306
added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents:
387
diff
changeset
|
693 |
|
|
0b337dedc306
added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents:
387
diff
changeset
|
694 |
text {*
|
|
0b337dedc306
added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents:
387
diff
changeset
|
695 |
@{ML_ind prove_future in Goal}
|
|
0b337dedc306
added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents:
387
diff
changeset
|
696 |
@{ML_ind future_result in Goal}
|
|
0b337dedc306
added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents:
387
diff
changeset
|
697 |
@{ML_ind fork_pri in Future}
|
|
0b337dedc306
added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents:
387
diff
changeset
|
698 |
*} |
|
387
5dcee4d751ad
completed the unification section
Christian Urban <urbanc@in.tum.de>
parents:
386
diff
changeset
|
699 |
|
| 396 | 700 |
section {* Parse and Print Translations (TBD) *}
|
701 |
||
|
349
9e374cd891e1
updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents:
348
diff
changeset
|
702 |
section {* Summary *}
|
|
9e374cd891e1
updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents:
348
diff
changeset
|
703 |
|
|
9e374cd891e1
updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents:
348
diff
changeset
|
704 |
text {*
|
|
395
2c392f61f400
spilt the Essential's chapter
Christian Urban <urbanc@in.tum.de>
parents:
394
diff
changeset
|
705 |
TBD |
|
349
9e374cd891e1
updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents:
348
diff
changeset
|
706 |
*} |
|
318
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
707 |
|
|
efb5fff99c96
split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
708 |
end |