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