author | wenzelm |
Fri, 16 Oct 2015 14:53:26 +0200 | |
changeset 61458 | 987533262fc2 |
parent 61439 | 2bf52eec4e8a |
child 61477 | e467ae7aa808 |
permissions | -rw-r--r-- |
29755 | 1 |
theory Prelim |
2 |
imports Base |
|
3 |
begin |
|
18537 | 4 |
|
58618 | 5 |
chapter \<open>Preliminaries\<close> |
18537 | 6 |
|
58618 | 7 |
section \<open>Contexts \label{sec:context}\<close> |
18537 | 8 |
|
58618 | 9 |
text \<open> |
20451 | 10 |
A logical context represents the background that is required for |
11 |
formulating statements and composing proofs. It acts as a medium to |
|
12 |
produce formal content, depending on earlier material (declarations, |
|
13 |
results etc.). |
|
18537 | 14 |
|
20451 | 15 |
For example, derivations within the Isabelle/Pure logic can be |
16 |
described as a judgment @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<phi>"}, which means that a |
|
20429 | 17 |
proposition @{text "\<phi>"} is derivable from hypotheses @{text "\<Gamma>"} |
18 |
within the theory @{text "\<Theta>"}. There are logical reasons for |
|
20451 | 19 |
keeping @{text "\<Theta>"} and @{text "\<Gamma>"} separate: theories can be |
20 |
liberal about supporting type constructors and schematic |
|
21 |
polymorphism of constants and axioms, while the inner calculus of |
|
22 |
@{text "\<Gamma> \<turnstile> \<phi>"} is strictly limited to Simple Type Theory (with |
|
23 |
fixed type variables in the assumptions). |
|
18537 | 24 |
|
61416 | 25 |
\<^medskip> |
26 |
Contexts and derivations are linked by the following key |
|
20429 | 27 |
principles: |
28 |
||
61416 | 29 |
\<^item> Transfer: monotonicity of derivations admits results to be |
20451 | 30 |
transferred into a \emph{larger} context, i.e.\ @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> |
31 |
\<phi>"} implies @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta>\<^sub>' \<phi>"} for contexts @{text "\<Theta>' |
|
32 |
\<supseteq> \<Theta>"} and @{text "\<Gamma>' \<supseteq> \<Gamma>"}. |
|
18537 | 33 |
|
61416 | 34 |
\<^item> Export: discharge of hypotheses admits results to be exported |
20451 | 35 |
into a \emph{smaller} context, i.e.\ @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta> \<phi>"} |
36 |
implies @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<Delta> \<Longrightarrow> \<phi>"} where @{text "\<Gamma>' \<supseteq> \<Gamma>"} and |
|
37 |
@{text "\<Delta> = \<Gamma>' - \<Gamma>"}. Note that @{text "\<Theta>"} remains unchanged here, |
|
38 |
only the @{text "\<Gamma>"} part is affected. |
|
20429 | 39 |
|
18537 | 40 |
|
61416 | 41 |
\<^medskip> |
42 |
By modeling the main characteristics of the primitive |
|
20451 | 43 |
@{text "\<Theta>"} and @{text "\<Gamma>"} above, and abstracting over any |
44 |
particular logical content, we arrive at the fundamental notions of |
|
45 |
\emph{theory context} and \emph{proof context} in Isabelle/Isar. |
|
46 |
These implement a certain policy to manage arbitrary \emph{context |
|
47 |
data}. There is a strongly-typed mechanism to declare new kinds of |
|
20429 | 48 |
data at compile time. |
18537 | 49 |
|
20451 | 50 |
The internal bootstrap process of Isabelle/Pure eventually reaches a |
51 |
stage where certain data slots provide the logical content of @{text |
|
52 |
"\<Theta>"} and @{text "\<Gamma>"} sketched above, but this does not stop there! |
|
53 |
Various additional data slots support all kinds of mechanisms that |
|
54 |
are not necessarily part of the core logic. |
|
18537 | 55 |
|
20429 | 56 |
For example, there would be data for canonical introduction and |
57 |
elimination rules for arbitrary operators (depending on the |
|
58 |
object-logic and application), which enables users to perform |
|
20451 | 59 |
standard proof steps implicitly (cf.\ the @{text "rule"} method |
58555 | 60 |
@{cite "isabelle-isar-ref"}). |
18537 | 61 |
|
61416 | 62 |
\<^medskip> |
63 |
Thus Isabelle/Isar is able to bring forth more and more |
|
20451 | 64 |
concepts successively. In particular, an object-logic like |
65 |
Isabelle/HOL continues the Isabelle/Pure setup by adding specific |
|
66 |
components for automated reasoning (classical reasoner, tableau |
|
67 |
prover, structured induction etc.) and derived specification |
|
68 |
mechanisms (inductive predicates, recursive functions etc.). All of |
|
69 |
this is ultimately based on the generic data management by theory |
|
70 |
and proof contexts introduced here. |
|
58618 | 71 |
\<close> |
18537 | 72 |
|
73 |
||
58618 | 74 |
subsection \<open>Theory context \label{sec:context-theory}\<close> |
18537 | 75 |
|
58618 | 76 |
text \<open>A \emph{theory} is a data container with explicit name and |
34921 | 77 |
unique identifier. Theories are related by a (nominal) sub-theory |
20451 | 78 |
relation, which corresponds to the dependency graph of the original |
79 |
construction; each theory is derived from a certain sub-graph of |
|
34921 | 80 |
ancestor theories. To this end, the system maintains a set of |
81 |
symbolic ``identification stamps'' within each theory. |
|
18537 | 82 |
|
61046
6b97896d4946
tuned documentation -- merge is implicitly performed by the system;
wenzelm
parents:
60948
diff
changeset
|
83 |
The @{text "begin"} operation starts a new theory by importing several |
6b97896d4946
tuned documentation -- merge is implicitly performed by the system;
wenzelm
parents:
60948
diff
changeset
|
84 |
parent theories (with merged contents) and entering a special mode of |
6b97896d4946
tuned documentation -- merge is implicitly performed by the system;
wenzelm
parents:
60948
diff
changeset
|
85 |
nameless incremental updates, until the final @{text "end"} operation is |
34921 | 86 |
performed. |
87 |
||
61416 | 88 |
\<^medskip> |
89 |
The example in \figref{fig:ex-theory} below shows a theory |
|
20451 | 90 |
graph derived from @{text "Pure"}, with theory @{text "Length"} |
91 |
importing @{text "Nat"} and @{text "List"}. The body of @{text |
|
52788 | 92 |
"Length"} consists of a sequence of updates, resulting in locally a |
93 |
linear sub-theory relation for each intermediate step. |
|
20447 | 94 |
|
95 |
\begin{figure}[htb] |
|
96 |
\begin{center} |
|
20429 | 97 |
\begin{tabular}{rcccl} |
20447 | 98 |
& & @{text "Pure"} \\ |
99 |
& & @{text "\<down>"} \\ |
|
100 |
& & @{text "FOL"} \\ |
|
18537 | 101 |
& $\swarrow$ & & $\searrow$ & \\ |
21852 | 102 |
@{text "Nat"} & & & & @{text "List"} \\ |
18537 | 103 |
& $\searrow$ & & $\swarrow$ \\ |
20447 | 104 |
& & @{text "Length"} \\ |
26864 | 105 |
& & \multicolumn{3}{l}{~~@{keyword "begin"}} \\ |
18537 | 106 |
& & $\vdots$~~ \\ |
26864 | 107 |
& & \multicolumn{3}{l}{~~@{command "end"}} \\ |
20429 | 108 |
\end{tabular} |
20451 | 109 |
\caption{A theory definition depending on ancestors}\label{fig:ex-theory} |
20447 | 110 |
\end{center} |
111 |
\end{figure} |
|
20451 | 112 |
|
61416 | 113 |
\<^medskip> |
114 |
Derived formal entities may retain a reference to the |
|
52788 | 115 |
background theory in order to indicate the formal context from which |
116 |
they were produced. This provides an immutable certificate of the |
|
58618 | 117 |
background theory.\<close> |
18537 | 118 |
|
58618 | 119 |
text %mlref \<open> |
20447 | 120 |
\begin{mldecls} |
121 |
@{index_ML_type theory} \\ |
|
60948 | 122 |
@{index_ML Context.eq_thy: "theory * theory -> bool"} \\ |
123 |
@{index_ML Context.subthy: "theory * theory -> bool"} \\ |
|
48930 | 124 |
@{index_ML Theory.begin_theory: "string * Position.T -> theory list -> theory"} \\ |
39837 | 125 |
@{index_ML Theory.parents_of: "theory -> theory list"} \\ |
126 |
@{index_ML Theory.ancestors_of: "theory -> theory list"} \\ |
|
20547 | 127 |
\end{mldecls} |
20447 | 128 |
|
61439 | 129 |
\<^descr> Type @{ML_type theory} represents theory contexts. |
20447 | 130 |
|
61439 | 131 |
\<^descr> @{ML "Context.eq_thy"}~@{text "(thy\<^sub>1, thy\<^sub>2)"} check strict |
39837 | 132 |
identity of two theories. |
133 |
||
61439 | 134 |
\<^descr> @{ML "Context.subthy"}~@{text "(thy\<^sub>1, thy\<^sub>2)"} compares theories |
34921 | 135 |
according to the intrinsic graph structure of the construction. |
136 |
This sub-theory relation is a nominal approximation of inclusion |
|
137 |
(@{text "\<subseteq>"}) of the corresponding content (according to the |
|
138 |
semantics of the ML modules that implement the data). |
|
20447 | 139 |
|
61439 | 140 |
\<^descr> @{ML "Theory.begin_theory"}~@{text "name parents"} constructs |
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39821
diff
changeset
|
141 |
a new theory based on the given parents. This ML function is |
34921 | 142 |
normally not invoked directly. |
20447 | 143 |
|
61439 | 144 |
\<^descr> @{ML "Theory.parents_of"}~@{text "thy"} returns the direct |
39837 | 145 |
ancestors of @{text thy}. |
146 |
||
61439 | 147 |
\<^descr> @{ML "Theory.ancestors_of"}~@{text "thy"} returns all |
39837 | 148 |
ancestors of @{text thy} (not including @{text thy} itself). |
58618 | 149 |
\<close> |
20430 | 150 |
|
58618 | 151 |
text %mlantiq \<open> |
39832 | 152 |
\begin{matharray}{rcl} |
153 |
@{ML_antiquotation_def "theory"} & : & @{text ML_antiquotation} \\ |
|
51686 | 154 |
@{ML_antiquotation_def "theory_context"} & : & @{text ML_antiquotation} \\ |
39832 | 155 |
\end{matharray} |
156 |
||
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
53015
diff
changeset
|
157 |
@{rail \<open> |
42510
b9c106763325
use @{rail} antiquotation (with some nested markup);
wenzelm
parents:
42401
diff
changeset
|
158 |
@@{ML_antiquotation theory} nameref? |
51686 | 159 |
; |
160 |
@@{ML_antiquotation theory_context} nameref |
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
53015
diff
changeset
|
161 |
\<close>} |
39832 | 162 |
|
61439 | 163 |
\<^descr> @{text "@{theory}"} refers to the background theory of the |
39832 | 164 |
current context --- as abstract value. |
165 |
||
61439 | 166 |
\<^descr> @{text "@{theory A}"} refers to an explicitly named ancestor |
39832 | 167 |
theory @{text "A"} of the background theory of the current context |
168 |
--- as abstract value. |
|
169 |
||
61439 | 170 |
\<^descr> @{text "@{theory_context A}"} is similar to @{text "@{theory |
51686 | 171 |
A}"}, but presents the result as initial @{ML_type Proof.context} |
172 |
(see also @{ML Proof_Context.init_global}). |
|
58618 | 173 |
\<close> |
39832 | 174 |
|
18537 | 175 |
|
58618 | 176 |
subsection \<open>Proof context \label{sec:context-proof}\<close> |
18537 | 177 |
|
58618 | 178 |
text \<open>A proof context is a container for pure data that refers to |
52788 | 179 |
the theory from which it is derived. The @{text "init"} operation |
180 |
creates a proof context from a given theory. There is an explicit |
|
181 |
@{text "transfer"} operation to force resynchronization with updates |
|
182 |
to the background theory -- this is rarely required in practice. |
|
20429 | 183 |
|
34921 | 184 |
Entities derived in a proof context need to record logical |
20447 | 185 |
requirements explicitly, since there is no separate context |
34921 | 186 |
identification or symbolic inclusion as for theories. For example, |
187 |
hypotheses used in primitive derivations (cf.\ \secref{sec:thms}) |
|
188 |
are recorded separately within the sequent @{text "\<Gamma> \<turnstile> \<phi>"}, just to |
|
189 |
make double sure. Results could still leak into an alien proof |
|
190 |
context due to programming errors, but Isabelle/Isar includes some |
|
191 |
extra validity checks in critical positions, notably at the end of a |
|
192 |
sub-proof. |
|
20429 | 193 |
|
20451 | 194 |
Proof contexts may be manipulated arbitrarily, although the common |
195 |
discipline is to follow block structure as a mental model: a given |
|
196 |
context is extended consecutively, and results are exported back |
|
34921 | 197 |
into the original context. Note that an Isar proof state models |
20451 | 198 |
block-structured reasoning explicitly, using a stack of proof |
34921 | 199 |
contexts internally. For various technical reasons, the background |
200 |
theory of an Isar proof state must not be changed while the proof is |
|
201 |
still under construction! |
|
58618 | 202 |
\<close> |
18537 | 203 |
|
58618 | 204 |
text %mlref \<open> |
20449 | 205 |
\begin{mldecls} |
206 |
@{index_ML_type Proof.context} \\ |
|
42361 | 207 |
@{index_ML Proof_Context.init_global: "theory -> Proof.context"} \\ |
208 |
@{index_ML Proof_Context.theory_of: "Proof.context -> theory"} \\ |
|
209 |
@{index_ML Proof_Context.transfer: "theory -> Proof.context -> Proof.context"} \\ |
|
20449 | 210 |
\end{mldecls} |
211 |
||
61439 | 212 |
\<^descr> Type @{ML_type Proof.context} represents proof contexts. |
20449 | 213 |
|
61439 | 214 |
\<^descr> @{ML Proof_Context.init_global}~@{text "thy"} produces a proof |
52788 | 215 |
context derived from @{text "thy"}, initializing all data. |
20449 | 216 |
|
61439 | 217 |
\<^descr> @{ML Proof_Context.theory_of}~@{text "ctxt"} selects the |
52788 | 218 |
background theory from @{text "ctxt"}. |
20449 | 219 |
|
61439 | 220 |
\<^descr> @{ML Proof_Context.transfer}~@{text "thy ctxt"} promotes the |
20449 | 221 |
background theory of @{text "ctxt"} to the super theory @{text |
222 |
"thy"}. |
|
58618 | 223 |
\<close> |
20449 | 224 |
|
58618 | 225 |
text %mlantiq \<open> |
39832 | 226 |
\begin{matharray}{rcl} |
227 |
@{ML_antiquotation_def "context"} & : & @{text ML_antiquotation} \\ |
|
228 |
\end{matharray} |
|
229 |
||
61439 | 230 |
\<^descr> @{text "@{context}"} refers to \emph{the} context at |
39832 | 231 |
compile-time --- as abstract value. Independently of (local) theory |
232 |
or proof mode, this always produces a meaningful result. |
|
233 |
||
234 |
This is probably the most common antiquotation in interactive |
|
235 |
experimentation with ML inside Isar. |
|
58618 | 236 |
\<close> |
39832 | 237 |
|
20430 | 238 |
|
58618 | 239 |
subsection \<open>Generic contexts \label{sec:generic-context}\<close> |
20429 | 240 |
|
58618 | 241 |
text \<open> |
20449 | 242 |
A generic context is the disjoint sum of either a theory or proof |
20451 | 243 |
context. Occasionally, this enables uniform treatment of generic |
20450 | 244 |
context data, typically extra-logical information. Operations on |
20449 | 245 |
generic contexts include the usual injections, partial selections, |
246 |
and combinators for lifting operations on either component of the |
|
247 |
disjoint sum. |
|
248 |
||
249 |
Moreover, there are total operations @{text "theory_of"} and @{text |
|
250 |
"proof_of"} to convert a generic context into either kind: a theory |
|
20451 | 251 |
can always be selected from the sum, while a proof context might |
34921 | 252 |
have to be constructed by an ad-hoc @{text "init"} operation, which |
253 |
incurs a small runtime overhead. |
|
58618 | 254 |
\<close> |
20430 | 255 |
|
58618 | 256 |
text %mlref \<open> |
20449 | 257 |
\begin{mldecls} |
258 |
@{index_ML_type Context.generic} \\ |
|
259 |
@{index_ML Context.theory_of: "Context.generic -> theory"} \\ |
|
260 |
@{index_ML Context.proof_of: "Context.generic -> Proof.context"} \\ |
|
261 |
\end{mldecls} |
|
262 |
||
61439 | 263 |
\<^descr> Type @{ML_type Context.generic} is the direct sum of @{ML_type |
20451 | 264 |
"theory"} and @{ML_type "Proof.context"}, with the datatype |
265 |
constructors @{ML "Context.Theory"} and @{ML "Context.Proof"}. |
|
20449 | 266 |
|
61439 | 267 |
\<^descr> @{ML Context.theory_of}~@{text "context"} always produces a |
20449 | 268 |
theory from the generic @{text "context"}, using @{ML |
42361 | 269 |
"Proof_Context.theory_of"} as required. |
20449 | 270 |
|
61439 | 271 |
\<^descr> @{ML Context.proof_of}~@{text "context"} always produces a |
20449 | 272 |
proof context from the generic @{text "context"}, using @{ML |
42361 | 273 |
"Proof_Context.init_global"} as required (note that this re-initializes the |
20451 | 274 |
context data with each invocation). |
58618 | 275 |
\<close> |
20437 | 276 |
|
20476 | 277 |
|
58618 | 278 |
subsection \<open>Context data \label{sec:context-data}\<close> |
20447 | 279 |
|
58618 | 280 |
text \<open>The main purpose of theory and proof contexts is to manage |
33524
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
281 |
arbitrary (pure) data. New data types can be declared incrementally |
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
282 |
at compile time. There are separate declaration mechanisms for any |
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
283 |
of the three kinds of contexts: theory, proof, generic. |
20449 | 284 |
|
33524
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
285 |
\paragraph{Theory data} declarations need to implement the following |
57421 | 286 |
ML signature: |
20449 | 287 |
|
61416 | 288 |
\<^medskip> |
20449 | 289 |
\begin{tabular}{ll} |
22869 | 290 |
@{text "\<type> T"} & representing type \\ |
291 |
@{text "\<val> empty: T"} & empty default value \\ |
|
292 |
@{text "\<val> extend: T \<rightarrow> T"} & re-initialize on import \\ |
|
293 |
@{text "\<val> merge: T \<times> T \<rightarrow> T"} & join on import \\ |
|
20449 | 294 |
\end{tabular} |
61416 | 295 |
\<^medskip> |
20449 | 296 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
297 |
The @{text "empty"} value acts as initial default for \emph{any} |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
298 |
theory that does not declare actual data content; @{text "extend"} |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
299 |
is acts like a unitary version of @{text "merge"}. |
20449 | 300 |
|
34921 | 301 |
Implementing @{text "merge"} can be tricky. The general idea is |
302 |
that @{text "merge (data\<^sub>1, data\<^sub>2)"} inserts those parts of @{text |
|
303 |
"data\<^sub>2"} into @{text "data\<^sub>1"} that are not yet present, while |
|
304 |
keeping the general order of things. The @{ML Library.merge} |
|
305 |
function on plain lists may serve as canonical template. |
|
306 |
||
307 |
Particularly note that shared parts of the data must not be |
|
308 |
duplicated by naive concatenation, or a theory graph that is like a |
|
309 |
chain of diamonds would cause an exponential blowup! |
|
310 |
||
33524
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
311 |
\paragraph{Proof context data} declarations need to implement the |
57421 | 312 |
following ML signature: |
20449 | 313 |
|
61416 | 314 |
\<^medskip> |
20449 | 315 |
\begin{tabular}{ll} |
22869 | 316 |
@{text "\<type> T"} & representing type \\ |
317 |
@{text "\<val> init: theory \<rightarrow> T"} & produce initial value \\ |
|
20449 | 318 |
\end{tabular} |
61416 | 319 |
\<^medskip> |
20449 | 320 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
321 |
The @{text "init"} operation is supposed to produce a pure value |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
322 |
from the given background theory and should be somehow |
34921 | 323 |
``immediate''. Whenever a proof context is initialized, which |
324 |
happens frequently, the the system invokes the @{text "init"} |
|
39821 | 325 |
operation of \emph{all} theory data slots ever declared. This also |
326 |
means that one needs to be economic about the total number of proof |
|
327 |
data declarations in the system, i.e.\ each ML module should declare |
|
328 |
at most one, sometimes two data slots for its internal use. |
|
329 |
Repeated data declarations to simulate a record type should be |
|
330 |
avoided! |
|
20449 | 331 |
|
20451 | 332 |
\paragraph{Generic data} provides a hybrid interface for both theory |
33524
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
333 |
and proof data. The @{text "init"} operation for proof contexts is |
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
334 |
predefined to select the current data value from the background |
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
335 |
theory. |
20449 | 336 |
|
61416 | 337 |
\<^bigskip> |
338 |
Any of the above data declarations over type @{text "T"} |
|
39821 | 339 |
result in an ML structure with the following signature: |
20449 | 340 |
|
61416 | 341 |
\<^medskip> |
20449 | 342 |
\begin{tabular}{ll} |
343 |
@{text "get: context \<rightarrow> T"} \\ |
|
344 |
@{text "put: T \<rightarrow> context \<rightarrow> context"} \\ |
|
345 |
@{text "map: (T \<rightarrow> T) \<rightarrow> context \<rightarrow> context"} \\ |
|
346 |
\end{tabular} |
|
61416 | 347 |
\<^medskip> |
20449 | 348 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
349 |
These other operations provide exclusive access for the particular |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
350 |
kind of context (theory, proof, or generic context). This interface |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
351 |
observes the ML discipline for types and scopes: there is no other |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
352 |
way to access the corresponding data slot of a context. By keeping |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
353 |
these operations private, an Isabelle/ML module may maintain |
58618 | 354 |
abstract values authentically.\<close> |
20447 | 355 |
|
58618 | 356 |
text %mlref \<open> |
20450 | 357 |
\begin{mldecls} |
33524
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
358 |
@{index_ML_functor Theory_Data} \\ |
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
359 |
@{index_ML_functor Proof_Data} \\ |
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
360 |
@{index_ML_functor Generic_Data} \\ |
20450 | 361 |
\end{mldecls} |
362 |
||
61439 | 363 |
\<^descr> @{ML_functor Theory_Data}@{text "(spec)"} declares data for |
20450 | 364 |
type @{ML_type theory} according to the specification provided as |
20451 | 365 |
argument structure. The resulting structure provides data init and |
366 |
access operations as described above. |
|
20450 | 367 |
|
61439 | 368 |
\<^descr> @{ML_functor Proof_Data}@{text "(spec)"} is analogous to |
33524
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
369 |
@{ML_functor Theory_Data} for type @{ML_type Proof.context}. |
20450 | 370 |
|
61439 | 371 |
\<^descr> @{ML_functor Generic_Data}@{text "(spec)"} is analogous to |
33524
a08e6c1cbc04
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents:
33174
diff
changeset
|
372 |
@{ML_functor Theory_Data} for type @{ML_type Context.generic}. |
58618 | 373 |
\<close> |
20450 | 374 |
|
58618 | 375 |
text %mlex \<open> |
34928 | 376 |
The following artificial example demonstrates theory |
377 |
data: we maintain a set of terms that are supposed to be wellformed |
|
378 |
wrt.\ the enclosing theory. The public interface is as follows: |
|
58618 | 379 |
\<close> |
34928 | 380 |
|
58618 | 381 |
ML \<open> |
34928 | 382 |
signature WELLFORMED_TERMS = |
383 |
sig |
|
384 |
val get: theory -> term list |
|
385 |
val add: term -> theory -> theory |
|
386 |
end; |
|
58618 | 387 |
\<close> |
34928 | 388 |
|
58618 | 389 |
text \<open>The implementation uses private theory data internally, and |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
390 |
only exposes an operation that involves explicit argument checking |
58618 | 391 |
wrt.\ the given theory.\<close> |
34928 | 392 |
|
58618 | 393 |
ML \<open> |
34928 | 394 |
structure Wellformed_Terms: WELLFORMED_TERMS = |
395 |
struct |
|
396 |
||
397 |
structure Terms = Theory_Data |
|
398 |
( |
|
39687 | 399 |
type T = term Ord_List.T; |
34928 | 400 |
val empty = []; |
401 |
val extend = I; |
|
402 |
fun merge (ts1, ts2) = |
|
39687 | 403 |
Ord_List.union Term_Ord.fast_term_ord ts1 ts2; |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
404 |
); |
34928 | 405 |
|
406 |
val get = Terms.get; |
|
407 |
||
408 |
fun add raw_t thy = |
|
39821 | 409 |
let |
410 |
val t = Sign.cert_term thy raw_t; |
|
411 |
in |
|
412 |
Terms.map (Ord_List.insert Term_Ord.fast_term_ord t) thy |
|
413 |
end; |
|
34928 | 414 |
|
415 |
end; |
|
58618 | 416 |
\<close> |
34928 | 417 |
|
58618 | 418 |
text \<open>Type @{ML_type "term Ord_List.T"} is used for reasonably |
39864 | 419 |
efficient representation of a set of terms: all operations are |
420 |
linear in the number of stored elements. Here we assume that users |
|
421 |
of this module do not care about the declaration order, since that |
|
422 |
data structure forces its own arrangement of elements. |
|
34928 | 423 |
|
40153 | 424 |
Observe how the @{ML_text merge} operation joins the data slots of |
39687 | 425 |
the two constituents: @{ML Ord_List.union} prevents duplication of |
34928 | 426 |
common data from different branches, thus avoiding the danger of |
39821 | 427 |
exponential blowup. Plain list append etc.\ must never be used for |
428 |
theory data merges! |
|
34928 | 429 |
|
61416 | 430 |
\<^medskip> |
431 |
Our intended invariant is achieved as follows: |
|
34928 | 432 |
|
61416 | 433 |
\<^enum> @{ML Wellformed_Terms.add} only admits terms that have passed |
34928 | 434 |
the @{ML Sign.cert_term} check of the given theory at that point. |
435 |
||
61416 | 436 |
\<^enum> Wellformedness in the sense of @{ML Sign.cert_term} is |
34928 | 437 |
monotonic wrt.\ the sub-theory relation. So our data can move |
438 |
upwards in the hierarchy (via extension or merges), and maintain |
|
439 |
wellformedness without further checks. |
|
440 |
||
441 |
||
442 |
Note that all basic operations of the inference kernel (which |
|
443 |
includes @{ML Sign.cert_term}) observe this monotonicity principle, |
|
444 |
but other user-space tools don't. For example, fully-featured |
|
445 |
type-inference via @{ML Syntax.check_term} (cf.\ |
|
446 |
\secref{sec:term-check}) is not necessarily monotonic wrt.\ the |
|
447 |
background theory, since constraints of term constants can be |
|
39821 | 448 |
modified by later declarations, for example. |
34928 | 449 |
|
450 |
In most cases, user-space context data does not have to take such |
|
451 |
invariants too seriously. The situation is different in the |
|
452 |
implementation of the inference kernel itself, which uses the very |
|
453 |
same data mechanisms for types, constants, axioms etc. |
|
58618 | 454 |
\<close> |
34928 | 455 |
|
20447 | 456 |
|
58618 | 457 |
subsection \<open>Configuration options \label{sec:config-options}\<close> |
39865 | 458 |
|
58618 | 459 |
text \<open>A \emph{configuration option} is a named optional value of |
39865 | 460 |
some basic type (Boolean, integer, string) that is stored in the |
461 |
context. It is a simple application of general context data |
|
462 |
(\secref{sec:context-data}) that is sufficiently common to justify |
|
463 |
customized setup, which includes some concrete declarations for |
|
464 |
end-users using existing notation for attributes (cf.\ |
|
465 |
\secref{sec:attributes}). |
|
466 |
||
467 |
For example, the predefined configuration option @{attribute |
|
468 |
show_types} controls output of explicit type constraints for |
|
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39866
diff
changeset
|
469 |
variables in printed terms (cf.\ \secref{sec:read-print}). Its |
39865 | 470 |
value can be modified within Isar text like this: |
58618 | 471 |
\<close> |
39865 | 472 |
|
59902 | 473 |
experiment |
474 |
begin |
|
475 |
||
39865 | 476 |
declare [[show_types = false]] |
58618 | 477 |
-- \<open>declaration within (local) theory context\<close> |
39865 | 478 |
|
40964 | 479 |
notepad |
480 |
begin |
|
39865 | 481 |
note [[show_types = true]] |
58618 | 482 |
-- \<open>declaration within proof (forward mode)\<close> |
39865 | 483 |
term x |
484 |
||
485 |
have "x = x" |
|
486 |
using [[show_types = false]] |
|
58618 | 487 |
-- \<open>declaration within proof (backward mode)\<close> |
39865 | 488 |
.. |
40964 | 489 |
end |
39865 | 490 |
|
59902 | 491 |
end |
492 |
||
58618 | 493 |
text \<open>Configuration options that are not set explicitly hold a |
39865 | 494 |
default value that can depend on the application context. This |
495 |
allows to retrieve the value from another slot within the context, |
|
496 |
or fall back on a global preference mechanism, for example. |
|
497 |
||
498 |
The operations to declare configuration options and get/map their |
|
499 |
values are modeled as direct replacements for historic global |
|
500 |
references, only that the context is made explicit. This allows |
|
501 |
easy configuration of tools, without relying on the execution order |
|
58618 | 502 |
as required for old-style mutable references.\<close> |
39865 | 503 |
|
58618 | 504 |
text %mlref \<open> |
39865 | 505 |
\begin{mldecls} |
506 |
@{index_ML Config.get: "Proof.context -> 'a Config.T -> 'a"} \\ |
|
507 |
@{index_ML Config.map: "'a Config.T -> ('a -> 'a) -> Proof.context -> Proof.context"} \\ |
|
42616
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
508 |
@{index_ML Attrib.setup_config_bool: "binding -> (Context.generic -> bool) -> |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
509 |
bool Config.T"} \\ |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
510 |
@{index_ML Attrib.setup_config_int: "binding -> (Context.generic -> int) -> |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
511 |
int Config.T"} \\ |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
512 |
@{index_ML Attrib.setup_config_real: "binding -> (Context.generic -> real) -> |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
513 |
real Config.T"} \\ |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
514 |
@{index_ML Attrib.setup_config_string: "binding -> (Context.generic -> string) -> |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
515 |
string Config.T"} \\ |
39865 | 516 |
\end{mldecls} |
517 |
||
61439 | 518 |
\<^descr> @{ML Config.get}~@{text "ctxt config"} gets the value of |
39865 | 519 |
@{text "config"} in the given context. |
520 |
||
61439 | 521 |
\<^descr> @{ML Config.map}~@{text "config f ctxt"} updates the context |
39865 | 522 |
by updating the value of @{text "config"}. |
523 |
||
61439 | 524 |
\<^descr> @{text "config ="}~@{ML Attrib.setup_config_bool}~@{text "name |
42616
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
525 |
default"} creates a named configuration option of type @{ML_type |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
526 |
bool}, with the given @{text "default"} depending on the application |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
527 |
context. The resulting @{text "config"} can be used to get/map its |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
528 |
value in a given context. There is an implicit update of the |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
529 |
background theory that registers the option as attribute with some |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
530 |
concrete syntax. |
39865 | 531 |
|
61439 | 532 |
\<^descr> @{ML Attrib.config_int}, @{ML Attrib.config_real}, and @{ML |
40291 | 533 |
Attrib.config_string} work like @{ML Attrib.config_bool}, but for |
534 |
types @{ML_type int} and @{ML_type string}, respectively. |
|
58618 | 535 |
\<close> |
39865 | 536 |
|
58618 | 537 |
text %mlex \<open>The following example shows how to declare and use a |
39865 | 538 |
Boolean configuration option called @{text "my_flag"} with constant |
58618 | 539 |
default value @{ML false}.\<close> |
39865 | 540 |
|
58618 | 541 |
ML \<open> |
42616
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
542 |
val my_flag = |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
543 |
Attrib.setup_config_bool @{binding my_flag} (K false) |
58618 | 544 |
\<close> |
39865 | 545 |
|
58618 | 546 |
text \<open>Now the user can refer to @{attribute my_flag} in |
40126 | 547 |
declarations, while ML tools can retrieve the current value from the |
58618 | 548 |
context via @{ML Config.get}.\<close> |
39865 | 549 |
|
58618 | 550 |
ML_val \<open>@{assert} (Config.get @{context} my_flag = false)\<close> |
39865 | 551 |
|
552 |
declare [[my_flag = true]] |
|
553 |
||
58618 | 554 |
ML_val \<open>@{assert} (Config.get @{context} my_flag = true)\<close> |
39865 | 555 |
|
40964 | 556 |
notepad |
557 |
begin |
|
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
558 |
{ |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
559 |
note [[my_flag = false]] |
58618 | 560 |
ML_val \<open>@{assert} (Config.get @{context} my_flag = false)\<close> |
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
561 |
} |
58618 | 562 |
ML_val \<open>@{assert} (Config.get @{context} my_flag = true)\<close> |
40964 | 563 |
end |
39865 | 564 |
|
58618 | 565 |
text \<open>Here is another example involving ML type @{ML_type real} |
566 |
(floating-point numbers).\<close> |
|
40291 | 567 |
|
58618 | 568 |
ML \<open> |
42616
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
569 |
val airspeed_velocity = |
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents:
42510
diff
changeset
|
570 |
Attrib.setup_config_real @{binding airspeed_velocity} (K 0.0) |
58618 | 571 |
\<close> |
40291 | 572 |
|
40296 | 573 |
declare [[airspeed_velocity = 10]] |
40291 | 574 |
declare [[airspeed_velocity = 9.9]] |
575 |
||
39865 | 576 |
|
58618 | 577 |
section \<open>Names \label{sec:names}\<close> |
20451 | 578 |
|
58618 | 579 |
text \<open>In principle, a name is just a string, but there are various |
34925 | 580 |
conventions for representing additional structure. For example, |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
581 |
``@{text "Foo.bar.baz"}'' is considered as a long name consisting of |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
582 |
qualifier @{text "Foo.bar"} and base name @{text "baz"}. The |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
583 |
individual constituents of a name may have further substructure, |
58723 | 584 |
e.g.\ the string ``@{verbatim \<alpha>}'' encodes as a single |
52421
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
51686
diff
changeset
|
585 |
symbol (\secref{sec:symbols}). |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
586 |
|
61416 | 587 |
\<^medskip> |
588 |
Subsequently, we shall introduce specific categories of |
|
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
589 |
names. Roughly speaking these correspond to logical entities as |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
590 |
follows: |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
591 |
|
61416 | 592 |
\<^item> Basic names (\secref{sec:basic-name}): free and bound |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
593 |
variables. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
594 |
|
61416 | 595 |
\<^item> Indexed names (\secref{sec:indexname}): schematic variables. |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
596 |
|
61416 | 597 |
\<^item> Long names (\secref{sec:long-name}): constants of any kind |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
598 |
(type constructors, term constants, other concepts defined in user |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
599 |
space). Such entities are typically managed via name spaces |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
600 |
(\secref{sec:name-space}). |
58618 | 601 |
\<close> |
20437 | 602 |
|
603 |
||
58618 | 604 |
subsection \<open>Basic names \label{sec:basic-name}\<close> |
20476 | 605 |
|
58618 | 606 |
text \<open> |
20476 | 607 |
A \emph{basic name} essentially consists of a single Isabelle |
608 |
identifier. There are conventions to mark separate classes of basic |
|
29761 | 609 |
names, by attaching a suffix of underscores: one underscore means |
610 |
\emph{internal name}, two underscores means \emph{Skolem name}, |
|
611 |
three underscores means \emph{internal Skolem name}. |
|
20476 | 612 |
|
613 |
For example, the basic name @{text "foo"} has the internal version |
|
614 |
@{text "foo_"}, with Skolem versions @{text "foo__"} and @{text |
|
615 |
"foo___"}, respectively. |
|
616 |
||
20488 | 617 |
These special versions provide copies of the basic name space, apart |
618 |
from anything that normally appears in the user text. For example, |
|
619 |
system generated variables in Isar proof contexts are usually marked |
|
34926 | 620 |
as internal, which prevents mysterious names like @{text "xaa"} to |
621 |
appear in human-readable text. |
|
20476 | 622 |
|
61416 | 623 |
\<^medskip> |
624 |
Manipulating binding scopes often requires on-the-fly |
|
20488 | 625 |
renamings. A \emph{name context} contains a collection of already |
626 |
used names. The @{text "declare"} operation adds names to the |
|
627 |
context. |
|
20476 | 628 |
|
20488 | 629 |
The @{text "invents"} operation derives a number of fresh names from |
630 |
a given starting point. For example, the first three names derived |
|
631 |
from @{text "a"} are @{text "a"}, @{text "b"}, @{text "c"}. |
|
20476 | 632 |
|
633 |
The @{text "variants"} operation produces fresh names by |
|
20488 | 634 |
incrementing tentative names as base-26 numbers (with digits @{text |
635 |
"a..z"}) until all clashes are resolved. For example, name @{text |
|
636 |
"foo"} results in variants @{text "fooa"}, @{text "foob"}, @{text |
|
637 |
"fooc"}, \dots, @{text "fooaa"}, @{text "fooab"} etc.; each renaming |
|
638 |
step picks the next unused variant from this sequence. |
|
58618 | 639 |
\<close> |
20476 | 640 |
|
58618 | 641 |
text %mlref \<open> |
20476 | 642 |
\begin{mldecls} |
643 |
@{index_ML Name.internal: "string -> string"} \\ |
|
20547 | 644 |
@{index_ML Name.skolem: "string -> string"} \\ |
645 |
\end{mldecls} |
|
646 |
\begin{mldecls} |
|
20476 | 647 |
@{index_ML_type Name.context} \\ |
648 |
@{index_ML Name.context: Name.context} \\ |
|
649 |
@{index_ML Name.declare: "string -> Name.context -> Name.context"} \\ |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
650 |
@{index_ML Name.invent: "Name.context -> string -> int -> string list"} \\ |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42616
diff
changeset
|
651 |
@{index_ML Name.variant: "string -> Name.context -> string * Name.context"} \\ |
20476 | 652 |
\end{mldecls} |
34926 | 653 |
\begin{mldecls} |
654 |
@{index_ML Variable.names_of: "Proof.context -> Name.context"} \\ |
|
655 |
\end{mldecls} |
|
20476 | 656 |
|
61439 | 657 |
\<^descr> @{ML Name.internal}~@{text "name"} produces an internal name |
20476 | 658 |
by adding one underscore. |
659 |
||
61439 | 660 |
\<^descr> @{ML Name.skolem}~@{text "name"} produces a Skolem name by |
20476 | 661 |
adding two underscores. |
662 |
||
61439 | 663 |
\<^descr> Type @{ML_type Name.context} represents the context of already |
39864 | 664 |
used names; the initial value is @{ML "Name.context"}. |
20476 | 665 |
|
61439 | 666 |
\<^descr> @{ML Name.declare}~@{text "name"} enters a used name into the |
20488 | 667 |
context. |
20437 | 668 |
|
61439 | 669 |
\<^descr> @{ML Name.invent}~@{text "context name n"} produces @{text |
20488 | 670 |
"n"} fresh names derived from @{text "name"}. |
671 |
||
61439 | 672 |
\<^descr> @{ML Name.variant}~@{text "name context"} produces a fresh |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42616
diff
changeset
|
673 |
variant of @{text "name"}; the result is declared to the context. |
20476 | 674 |
|
61439 | 675 |
\<^descr> @{ML Variable.names_of}~@{text "ctxt"} retrieves the context |
34926 | 676 |
of declared type and term variable names. Projecting a proof |
677 |
context down to a primitive name context is occasionally useful when |
|
678 |
invoking lower-level operations. Regular management of ``fresh |
|
55837 | 679 |
variables'' is done by suitable operations of structure @{ML_structure |
34926 | 680 |
Variable}, which is also able to provide an official status of |
681 |
``locally fixed variable'' within the logical environment (cf.\ |
|
682 |
\secref{sec:variables}). |
|
58618 | 683 |
\<close> |
20476 | 684 |
|
58618 | 685 |
text %mlex \<open>The following simple examples demonstrate how to produce |
686 |
fresh names from the initial @{ML Name.context}.\<close> |
|
39857 | 687 |
|
59902 | 688 |
ML_val \<open> |
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
689 |
val list1 = Name.invent Name.context "a" 5; |
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
690 |
@{assert} (list1 = ["a", "b", "c", "d", "e"]); |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
691 |
|
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
692 |
val list2 = |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42616
diff
changeset
|
693 |
#1 (fold_map Name.variant ["x", "x", "a", "a", "'a", "'a"] Name.context); |
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
694 |
@{assert} (list2 = ["x", "xa", "a", "aa", "'a", "'aa"]); |
58618 | 695 |
\<close> |
39857 | 696 |
|
61416 | 697 |
text \<open> |
698 |
\<^medskip> |
|
699 |
The same works relatively to the formal context as follows.\<close> |
|
39857 | 700 |
|
59902 | 701 |
experiment fixes a b c :: 'a |
39857 | 702 |
begin |
703 |
||
59902 | 704 |
ML_val \<open> |
39857 | 705 |
val names = Variable.names_of @{context}; |
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
706 |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
707 |
val list1 = Name.invent names "a" 5; |
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
708 |
@{assert} (list1 = ["d", "e", "f", "g", "h"]); |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
709 |
|
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
710 |
val list2 = |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42616
diff
changeset
|
711 |
#1 (fold_map Name.variant ["x", "x", "a", "a", "'a", "'a"] names); |
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39865
diff
changeset
|
712 |
@{assert} (list2 = ["x", "xa", "aa", "ab", "'aa", "'ab"]); |
58618 | 713 |
\<close> |
39857 | 714 |
|
715 |
end |
|
716 |
||
20476 | 717 |
|
58618 | 718 |
subsection \<open>Indexed names \label{sec:indexname}\<close> |
20476 | 719 |
|
58618 | 720 |
text \<open> |
20476 | 721 |
An \emph{indexed name} (or @{text "indexname"}) is a pair of a basic |
20488 | 722 |
name and a natural number. This representation allows efficient |
723 |
renaming by incrementing the second component only. The canonical |
|
724 |
way to rename two collections of indexnames apart from each other is |
|
725 |
this: determine the maximum index @{text "maxidx"} of the first |
|
726 |
collection, then increment all indexes of the second collection by |
|
727 |
@{text "maxidx + 1"}; the maximum index of an empty collection is |
|
728 |
@{text "-1"}. |
|
20476 | 729 |
|
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
730 |
Occasionally, basic names are injected into the same pair type of |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
731 |
indexed names: then @{text "(x, -1)"} is used to encode the basic |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
732 |
name @{text "x"}. |
20488 | 733 |
|
61416 | 734 |
\<^medskip> |
735 |
Isabelle syntax observes the following rules for |
|
20488 | 736 |
representing an indexname @{text "(x, i)"} as a packed string: |
20476 | 737 |
|
61416 | 738 |
\<^item> @{text "?x"} if @{text "x"} does not end with a digit and @{text "i = 0"}, |
20476 | 739 |
|
61416 | 740 |
\<^item> @{text "?xi"} if @{text "x"} does not end with a digit, |
20476 | 741 |
|
61416 | 742 |
\<^item> @{text "?x.i"} otherwise. |
20476 | 743 |
|
20470 | 744 |
|
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
745 |
Indexnames may acquire large index numbers after several maxidx |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
746 |
shifts have been applied. Results are usually normalized towards |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
747 |
@{text "0"} at certain checkpoints, notably at the end of a proof. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
748 |
This works by producing variants of the corresponding basic name |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
749 |
components. For example, the collection @{text "?x1, ?x7, ?x42"} |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
750 |
becomes @{text "?x, ?xa, ?xb"}. |
58618 | 751 |
\<close> |
20476 | 752 |
|
58618 | 753 |
text %mlref \<open> |
20476 | 754 |
\begin{mldecls} |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
755 |
@{index_ML_type indexname: "string * int"} \\ |
20476 | 756 |
\end{mldecls} |
757 |
||
61439 | 758 |
\<^descr> Type @{ML_type indexname} represents indexed names. This is |
39864 | 759 |
an abbreviation for @{ML_type "string * int"}. The second component |
760 |
is usually non-negative, except for situations where @{text "(x, |
|
761 |
-1)"} is used to inject basic names into this type. Other negative |
|
34926 | 762 |
indexes should not be used. |
58618 | 763 |
\<close> |
20476 | 764 |
|
765 |
||
58618 | 766 |
subsection \<open>Long names \label{sec:long-name}\<close> |
20476 | 767 |
|
58618 | 768 |
text \<open>A \emph{long name} consists of a sequence of non-empty name |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
769 |
components. The packed representation uses a dot as separator, as |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
770 |
in ``@{text "A.b.c"}''. The last component is called \emph{base |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
771 |
name}, the remaining prefix is called \emph{qualifier} (which may be |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
772 |
empty). The qualifier can be understood as the access path to the |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
773 |
named entity while passing through some nested block-structure, |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
774 |
although our free-form long names do not really enforce any strict |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
775 |
discipline. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
776 |
|
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
777 |
For example, an item named ``@{text "A.b.c"}'' may be understood as |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
778 |
a local entity @{text "c"}, within a local structure @{text "b"}, |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
779 |
within a global structure @{text "A"}. In practice, long names |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
780 |
usually represent 1--3 levels of qualification. User ML code should |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
781 |
not make any assumptions about the particular structure of long |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
782 |
names! |
20437 | 783 |
|
20476 | 784 |
The empty name is commonly used as an indication of unnamed |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
785 |
entities, or entities that are not entered into the corresponding |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
786 |
name space, whenever this makes any sense. The basic operations on |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
787 |
long names map empty names again to empty names. |
58618 | 788 |
\<close> |
20437 | 789 |
|
58618 | 790 |
text %mlref \<open> |
20476 | 791 |
\begin{mldecls} |
30365 | 792 |
@{index_ML Long_Name.base_name: "string -> string"} \\ |
793 |
@{index_ML Long_Name.qualifier: "string -> string"} \\ |
|
794 |
@{index_ML Long_Name.append: "string -> string -> string"} \\ |
|
795 |
@{index_ML Long_Name.implode: "string list -> string"} \\ |
|
796 |
@{index_ML Long_Name.explode: "string -> string list"} \\ |
|
20547 | 797 |
\end{mldecls} |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
798 |
|
61439 | 799 |
\<^descr> @{ML Long_Name.base_name}~@{text "name"} returns the base name |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
800 |
of a long name. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
801 |
|
61439 | 802 |
\<^descr> @{ML Long_Name.qualifier}~@{text "name"} returns the qualifier |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
803 |
of a long name. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
804 |
|
61439 | 805 |
\<^descr> @{ML Long_Name.append}~@{text "name\<^sub>1 name\<^sub>2"} appends two long |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
806 |
names. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
807 |
|
61439 | 808 |
\<^descr> @{ML Long_Name.implode}~@{text "names"} and @{ML |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
809 |
Long_Name.explode}~@{text "name"} convert between the packed string |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
810 |
representation and the explicit list form of long names. |
58618 | 811 |
\<close> |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
812 |
|
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
813 |
|
58618 | 814 |
subsection \<open>Name spaces \label{sec:name-space}\<close> |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
815 |
|
58618 | 816 |
text \<open>A @{text "name space"} manages a collection of long names, |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
817 |
together with a mapping between partially qualified external names |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
818 |
and fully qualified internal names (in both directions). Note that |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
819 |
the corresponding @{text "intern"} and @{text "extern"} operations |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
820 |
are mostly used for parsing and printing only! The @{text |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
821 |
"declare"} operation augments a name space according to the accesses |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
822 |
determined by a given binding, and a naming policy from the context. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
823 |
|
61416 | 824 |
\<^medskip> |
825 |
A @{text "binding"} specifies details about the prospective |
|
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
826 |
long name of a newly introduced formal entity. It consists of a |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
827 |
base name, prefixes for qualification (separate ones for system |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
828 |
infrastructure and user-space mechanisms), a slot for the original |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
829 |
source position, and some additional flags. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
830 |
|
61416 | 831 |
\<^medskip> |
832 |
A @{text "naming"} provides some additional details for |
|
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
833 |
producing a long name from a binding. Normally, the naming is |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
834 |
implicit in the theory or proof context. The @{text "full"} |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
835 |
operation (and its variants for different context types) produces a |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
836 |
fully qualified internal name to be entered into a name space. The |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
837 |
main equation of this ``chemical reaction'' when binding new |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
838 |
entities in a context is as follows: |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
839 |
|
61416 | 840 |
\<^medskip> |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
841 |
\begin{tabular}{l} |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
842 |
@{text "binding + naming \<longrightarrow> long name + name space accesses"} |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
843 |
\end{tabular} |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
844 |
|
61416 | 845 |
\<^bigskip> |
846 |
As a general principle, there is a separate name space for |
|
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
847 |
each kind of formal entity, e.g.\ fact, logical constant, type |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
848 |
constructor, type class. It is usually clear from the occurrence in |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
849 |
concrete syntax (or from the scope) which kind of entity a name |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
850 |
refers to. For example, the very same name @{text "c"} may be used |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
851 |
uniformly for a constant, type constructor, and type class. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
852 |
|
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
853 |
There are common schemes to name derived entities systematically |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
854 |
according to the name of the main logical entity involved, e.g.\ |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
855 |
fact @{text "c.intro"} for a canonical introduction rule related to |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
856 |
constant @{text "c"}. This technique of mapping names from one |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
857 |
space into another requires some care in order to avoid conflicts. |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
858 |
In particular, theorem names derived from a type constructor or type |
39839 | 859 |
class should get an additional suffix in addition to the usual |
860 |
qualification. This leads to the following conventions for derived |
|
861 |
names: |
|
862 |
||
61416 | 863 |
\<^medskip> |
39839 | 864 |
\begin{tabular}{ll} |
865 |
logical entity & fact name \\\hline |
|
866 |
constant @{text "c"} & @{text "c.intro"} \\ |
|
867 |
type @{text "c"} & @{text "c_type.intro"} \\ |
|
868 |
class @{text "c"} & @{text "c_class.intro"} \\ |
|
869 |
\end{tabular} |
|
58618 | 870 |
\<close> |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
871 |
|
58618 | 872 |
text %mlref \<open> |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
873 |
\begin{mldecls} |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
874 |
@{index_ML_type binding} \\ |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
875 |
@{index_ML Binding.empty: binding} \\ |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
876 |
@{index_ML Binding.name: "string -> binding"} \\ |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
877 |
@{index_ML Binding.qualify: "bool -> string -> binding -> binding"} \\ |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
878 |
@{index_ML Binding.prefix: "bool -> string -> binding -> binding"} \\ |
59859 | 879 |
@{index_ML Binding.concealed: "binding -> binding"} \\ |
43547
f3a8476285c6
clarified Binding.pretty/print: no quotes, only markup -- Binding.str_of is rendered obsolete;
wenzelm
parents:
43329
diff
changeset
|
880 |
@{index_ML Binding.print: "binding -> string"} \\ |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
881 |
\end{mldecls} |
20547 | 882 |
\begin{mldecls} |
33174 | 883 |
@{index_ML_type Name_Space.naming} \\ |
58668 | 884 |
@{index_ML Name_Space.global_naming: Name_Space.naming} \\ |
33174 | 885 |
@{index_ML Name_Space.add_path: "string -> Name_Space.naming -> Name_Space.naming"} \\ |
886 |
@{index_ML Name_Space.full_name: "Name_Space.naming -> binding -> string"} \\ |
|
20547 | 887 |
\end{mldecls} |
888 |
\begin{mldecls} |
|
33174 | 889 |
@{index_ML_type Name_Space.T} \\ |
890 |
@{index_ML Name_Space.empty: "string -> Name_Space.T"} \\ |
|
891 |
@{index_ML Name_Space.merge: "Name_Space.T * Name_Space.T -> Name_Space.T"} \\ |
|
47174 | 892 |
@{index_ML Name_Space.declare: "Context.generic -> bool -> |
893 |
binding -> Name_Space.T -> string * Name_Space.T"} \\ |
|
33174 | 894 |
@{index_ML Name_Space.intern: "Name_Space.T -> string -> string"} \\ |
42358
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
wenzelm
parents:
40964
diff
changeset
|
895 |
@{index_ML Name_Space.extern: "Proof.context -> Name_Space.T -> string -> string"} \\ |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
896 |
@{index_ML Name_Space.is_concealed: "Name_Space.T -> string -> bool"} |
20476 | 897 |
\end{mldecls} |
20437 | 898 |
|
61439 | 899 |
\<^descr> Type @{ML_type binding} represents the abstract concept of |
39864 | 900 |
name bindings. |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
901 |
|
61439 | 902 |
\<^descr> @{ML Binding.empty} is the empty binding. |
20476 | 903 |
|
61439 | 904 |
\<^descr> @{ML Binding.name}~@{text "name"} produces a binding with base |
39832 | 905 |
name @{text "name"}. Note that this lacks proper source position |
906 |
information; see also the ML antiquotation @{ML_antiquotation |
|
907 |
binding}. |
|
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
908 |
|
61439 | 909 |
\<^descr> @{ML Binding.qualify}~@{text "mandatory name binding"} |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
910 |
prefixes qualifier @{text "name"} to @{text "binding"}. The @{text |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
911 |
"mandatory"} flag tells if this name component always needs to be |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
912 |
given in name space accesses --- this is mostly @{text "false"} in |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
913 |
practice. Note that this part of qualification is typically used in |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
914 |
derived specification mechanisms. |
20437 | 915 |
|
61439 | 916 |
\<^descr> @{ML Binding.prefix} is similar to @{ML Binding.qualify}, but |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
917 |
affects the system prefix. This part of extra qualification is |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
918 |
typically used in the infrastructure for modular specifications, |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
919 |
notably ``local theory targets'' (see also \chref{ch:local-theory}). |
20437 | 920 |
|
61439 | 921 |
\<^descr> @{ML Binding.concealed}~@{text "binding"} indicates that the |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
922 |
binding shall refer to an entity that serves foundational purposes |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
923 |
only. This flag helps to mark implementation details of |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
924 |
specification mechanism etc. Other tools should not depend on the |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
925 |
particulars of concealed entities (cf.\ @{ML |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
926 |
Name_Space.is_concealed}). |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
927 |
|
61439 | 928 |
\<^descr> @{ML Binding.print}~@{text "binding"} produces a string |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
929 |
representation for human-readable output, together with some formal |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
930 |
markup that might get used in GUI front-ends, for example. |
20476 | 931 |
|
61439 | 932 |
\<^descr> Type @{ML_type Name_Space.naming} represents the abstract |
39864 | 933 |
concept of a naming policy. |
20437 | 934 |
|
61439 | 935 |
\<^descr> @{ML Name_Space.global_naming} is the default naming policy: it is |
58668 | 936 |
global and lacks any path prefix. In a regular theory context this is |
937 |
augmented by a path prefix consisting of the theory name. |
|
20476 | 938 |
|
61439 | 939 |
\<^descr> @{ML Name_Space.add_path}~@{text "path naming"} augments the |
20488 | 940 |
naming policy by extending its path component. |
20437 | 941 |
|
61439 | 942 |
\<^descr> @{ML Name_Space.full_name}~@{text "naming binding"} turns a |
30281
9ad15d8ed311
renamed NameSpace.base to NameSpace.base_name (in accordance with "full_name");
wenzelm
parents:
30272
diff
changeset
|
943 |
name binding (usually a basic name) into the fully qualified |
29008 | 944 |
internal name, according to the given naming policy. |
20476 | 945 |
|
61439 | 946 |
\<^descr> Type @{ML_type Name_Space.T} represents name spaces. |
20476 | 947 |
|
61439 | 948 |
\<^descr> @{ML Name_Space.empty}~@{text "kind"} and @{ML Name_Space.merge}~@{text |
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52788
diff
changeset
|
949 |
"(space\<^sub>1, space\<^sub>2)"} are the canonical operations for |
20488 | 950 |
maintaining name spaces according to theory data management |
33174 | 951 |
(\secref{sec:context-data}); @{text "kind"} is a formal comment |
952 |
to characterize the purpose of a name space. |
|
20437 | 953 |
|
61439 | 954 |
\<^descr> @{ML Name_Space.declare}~@{text "context strict binding |
33174 | 955 |
space"} enters a name binding as fully qualified internal name into |
47174 | 956 |
the name space, using the naming of the context. |
20476 | 957 |
|
61439 | 958 |
\<^descr> @{ML Name_Space.intern}~@{text "space name"} internalizes a |
20476 | 959 |
(partially qualified) external name. |
20437 | 960 |
|
20488 | 961 |
This operation is mostly for parsing! Note that fully qualified |
20476 | 962 |
names stemming from declarations are produced via @{ML |
33174 | 963 |
"Name_Space.full_name"} and @{ML "Name_Space.declare"} |
29008 | 964 |
(or their derivatives for @{ML_type theory} and |
20488 | 965 |
@{ML_type Proof.context}). |
20437 | 966 |
|
61439 | 967 |
\<^descr> @{ML Name_Space.extern}~@{text "ctxt space name"} externalizes a |
20476 | 968 |
(fully qualified) internal name. |
969 |
||
30281
9ad15d8ed311
renamed NameSpace.base to NameSpace.base_name (in accordance with "full_name");
wenzelm
parents:
30272
diff
changeset
|
970 |
This operation is mostly for printing! User code should not rely on |
9ad15d8ed311
renamed NameSpace.base to NameSpace.base_name (in accordance with "full_name");
wenzelm
parents:
30272
diff
changeset
|
971 |
the precise result too much. |
20476 | 972 |
|
61439 | 973 |
\<^descr> @{ML Name_Space.is_concealed}~@{text "space name"} indicates |
34927
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
974 |
whether @{text "name"} refers to a strictly private entity that |
c4c02ac736a6
more details on long names, binding/naming, name space;
wenzelm
parents:
34926
diff
changeset
|
975 |
other tools are supposed to ignore! |
58618 | 976 |
\<close> |
30272 | 977 |
|
58618 | 978 |
text %mlantiq \<open> |
39832 | 979 |
\begin{matharray}{rcl} |
980 |
@{ML_antiquotation_def "binding"} & : & @{text ML_antiquotation} \\ |
|
981 |
\end{matharray} |
|
982 |
||
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
53015
diff
changeset
|
983 |
@{rail \<open> |
42510
b9c106763325
use @{rail} antiquotation (with some nested markup);
wenzelm
parents:
42401
diff
changeset
|
984 |
@@{ML_antiquotation binding} name |
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
53015
diff
changeset
|
985 |
\<close>} |
39832 | 986 |
|
61439 | 987 |
\<^descr> @{text "@{binding name}"} produces a binding with base name |
39832 | 988 |
@{text "name"} and the source position taken from the concrete |
989 |
syntax of this antiquotation. In many situations this is more |
|
990 |
appropriate than the more basic @{ML Binding.name} function. |
|
58618 | 991 |
\<close> |
39832 | 992 |
|
58618 | 993 |
text %mlex \<open>The following example yields the source position of some |
40126 | 994 |
concrete binding inlined into the text: |
58618 | 995 |
\<close> |
39833 | 996 |
|
59902 | 997 |
ML_val \<open>Binding.pos_of @{binding here}\<close> |
39833 | 998 |
|
61416 | 999 |
text \<open> |
1000 |
\<^medskip> |
|
1001 |
That position can be also printed in a message as follows:\<close> |
|
39833 | 1002 |
|
58742 | 1003 |
ML_command |
1004 |
\<open>writeln |
|
1005 |
("Look here" ^ Position.here (Binding.pos_of @{binding here}))\<close> |
|
39833 | 1006 |
|
58618 | 1007 |
text \<open>This illustrates a key virtue of formalized bindings as |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39857
diff
changeset
|
1008 |
opposed to raw specifications of base names: the system can use this |
40126 | 1009 |
additional information for feedback given to the user (error |
56071 | 1010 |
messages etc.). |
1011 |
||
61416 | 1012 |
\<^medskip> |
1013 |
The following example refers to its source position |
|
56071 | 1014 |
directly, which is occasionally useful for experimentation and |
58618 | 1015 |
diagnostic purposes:\<close> |
56071 | 1016 |
|
58742 | 1017 |
ML_command \<open>warning ("Look here" ^ Position.here @{here})\<close> |
39833 | 1018 |
|
18537 | 1019 |
end |