doc-src/IsarImplementation/Thy/document/Prelim.tex
author wenzelm
Sun, 08 Nov 2009 21:00:05 +0100
changeset 33526 a08e6c1cbc04
parent 33174 1f2051f41335
child 35001 31f8d9eaceff
permissions -rw-r--r--
updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm@30296
     1
%
wenzelm@30296
     2
\begin{isabellebody}%
wenzelm@30296
     3
\def\isabellecontext{Prelim}%
wenzelm@30296
     4
%
wenzelm@30296
     5
\isadelimtheory
wenzelm@30296
     6
%
wenzelm@30296
     7
\endisadelimtheory
wenzelm@30296
     8
%
wenzelm@30296
     9
\isatagtheory
wenzelm@30296
    10
\isacommand{theory}\isamarkupfalse%
wenzelm@30296
    11
\ Prelim\isanewline
wenzelm@30296
    12
\isakeyword{imports}\ Base\isanewline
wenzelm@30296
    13
\isakeyword{begin}%
wenzelm@30296
    14
\endisatagtheory
wenzelm@30296
    15
{\isafoldtheory}%
wenzelm@30296
    16
%
wenzelm@30296
    17
\isadelimtheory
wenzelm@30296
    18
%
wenzelm@30296
    19
\endisadelimtheory
wenzelm@30296
    20
%
wenzelm@30296
    21
\isamarkupchapter{Preliminaries%
wenzelm@30296
    22
}
wenzelm@30296
    23
\isamarkuptrue%
wenzelm@30296
    24
%
wenzelm@30296
    25
\isamarkupsection{Contexts \label{sec:context}%
wenzelm@30296
    26
}
wenzelm@30296
    27
\isamarkuptrue%
wenzelm@30296
    28
%
wenzelm@30296
    29
\begin{isamarkuptext}%
wenzelm@30296
    30
A logical context represents the background that is required for
wenzelm@30296
    31
  formulating statements and composing proofs.  It acts as a medium to
wenzelm@30296
    32
  produce formal content, depending on earlier material (declarations,
wenzelm@30296
    33
  results etc.).
wenzelm@30296
    34
wenzelm@30296
    35
  For example, derivations within the Isabelle/Pure logic can be
wenzelm@30296
    36
  described as a judgment \isa{{\isasymGamma}\ {\isasymturnstile}\isactrlsub {\isasymTheta}\ {\isasymphi}}, which means that a
wenzelm@30296
    37
  proposition \isa{{\isasymphi}} is derivable from hypotheses \isa{{\isasymGamma}}
wenzelm@30296
    38
  within the theory \isa{{\isasymTheta}}.  There are logical reasons for
wenzelm@30296
    39
  keeping \isa{{\isasymTheta}} and \isa{{\isasymGamma}} separate: theories can be
wenzelm@30296
    40
  liberal about supporting type constructors and schematic
wenzelm@30296
    41
  polymorphism of constants and axioms, while the inner calculus of
wenzelm@30296
    42
  \isa{{\isasymGamma}\ {\isasymturnstile}\ {\isasymphi}} is strictly limited to Simple Type Theory (with
wenzelm@30296
    43
  fixed type variables in the assumptions).
wenzelm@30296
    44
wenzelm@30296
    45
  \medskip Contexts and derivations are linked by the following key
wenzelm@30296
    46
  principles:
wenzelm@30296
    47
wenzelm@30296
    48
  \begin{itemize}
wenzelm@30296
    49
wenzelm@30296
    50
  \item Transfer: monotonicity of derivations admits results to be
wenzelm@30296
    51
  transferred into a \emph{larger} context, i.e.\ \isa{{\isasymGamma}\ {\isasymturnstile}\isactrlsub {\isasymTheta}\ {\isasymphi}} implies \isa{{\isasymGamma}{\isacharprime}\ {\isasymturnstile}\isactrlsub {\isasymTheta}\isactrlsub {\isacharprime}\ {\isasymphi}} for contexts \isa{{\isasymTheta}{\isacharprime}\ {\isasymsupseteq}\ {\isasymTheta}} and \isa{{\isasymGamma}{\isacharprime}\ {\isasymsupseteq}\ {\isasymGamma}}.
wenzelm@30296
    52
wenzelm@30296
    53
  \item Export: discharge of hypotheses admits results to be exported
wenzelm@30296
    54
  into a \emph{smaller} context, i.e.\ \isa{{\isasymGamma}{\isacharprime}\ {\isasymturnstile}\isactrlsub {\isasymTheta}\ {\isasymphi}}
wenzelm@30296
    55
  implies \isa{{\isasymGamma}\ {\isasymturnstile}\isactrlsub {\isasymTheta}\ {\isasymDelta}\ {\isasymLongrightarrow}\ {\isasymphi}} where \isa{{\isasymGamma}{\isacharprime}\ {\isasymsupseteq}\ {\isasymGamma}} and
wenzelm@30296
    56
  \isa{{\isasymDelta}\ {\isacharequal}\ {\isasymGamma}{\isacharprime}\ {\isacharminus}\ {\isasymGamma}}.  Note that \isa{{\isasymTheta}} remains unchanged here,
wenzelm@30296
    57
  only the \isa{{\isasymGamma}} part is affected.
wenzelm@30296
    58
wenzelm@30296
    59
  \end{itemize}
wenzelm@30296
    60
wenzelm@30296
    61
  \medskip By modeling the main characteristics of the primitive
wenzelm@30296
    62
  \isa{{\isasymTheta}} and \isa{{\isasymGamma}} above, and abstracting over any
wenzelm@30296
    63
  particular logical content, we arrive at the fundamental notions of
wenzelm@30296
    64
  \emph{theory context} and \emph{proof context} in Isabelle/Isar.
wenzelm@30296
    65
  These implement a certain policy to manage arbitrary \emph{context
wenzelm@30296
    66
  data}.  There is a strongly-typed mechanism to declare new kinds of
wenzelm@30296
    67
  data at compile time.
wenzelm@30296
    68
wenzelm@30296
    69
  The internal bootstrap process of Isabelle/Pure eventually reaches a
wenzelm@30296
    70
  stage where certain data slots provide the logical content of \isa{{\isasymTheta}} and \isa{{\isasymGamma}} sketched above, but this does not stop there!
wenzelm@30296
    71
  Various additional data slots support all kinds of mechanisms that
wenzelm@30296
    72
  are not necessarily part of the core logic.
wenzelm@30296
    73
wenzelm@30296
    74
  For example, there would be data for canonical introduction and
wenzelm@30296
    75
  elimination rules for arbitrary operators (depending on the
wenzelm@30296
    76
  object-logic and application), which enables users to perform
wenzelm@30296
    77
  standard proof steps implicitly (cf.\ the \isa{rule} method
wenzelm@30296
    78
  \cite{isabelle-isar-ref}).
wenzelm@30296
    79
wenzelm@30296
    80
  \medskip Thus Isabelle/Isar is able to bring forth more and more
wenzelm@30296
    81
  concepts successively.  In particular, an object-logic like
wenzelm@30296
    82
  Isabelle/HOL continues the Isabelle/Pure setup by adding specific
wenzelm@30296
    83
  components for automated reasoning (classical reasoner, tableau
wenzelm@30296
    84
  prover, structured induction etc.) and derived specification
wenzelm@30296
    85
  mechanisms (inductive predicates, recursive functions etc.).  All of
wenzelm@30296
    86
  this is ultimately based on the generic data management by theory
wenzelm@30296
    87
  and proof contexts introduced here.%
wenzelm@30296
    88
\end{isamarkuptext}%
wenzelm@30296
    89
\isamarkuptrue%
wenzelm@30296
    90
%
wenzelm@30296
    91
\isamarkupsubsection{Theory context \label{sec:context-theory}%
wenzelm@30296
    92
}
wenzelm@30296
    93
\isamarkuptrue%
wenzelm@30296
    94
%
wenzelm@30296
    95
\begin{isamarkuptext}%
wenzelm@30296
    96
A \emph{theory} is a data container with explicit name and unique
wenzelm@30296
    97
  identifier.  Theories are related by a (nominal) sub-theory
wenzelm@30296
    98
  relation, which corresponds to the dependency graph of the original
wenzelm@30296
    99
  construction; each theory is derived from a certain sub-graph of
wenzelm@30296
   100
  ancestor theories.
wenzelm@30296
   101
wenzelm@30296
   102
  The \isa{merge} operation produces the least upper bound of two
wenzelm@30296
   103
  theories, which actually degenerates into absorption of one theory
wenzelm@30296
   104
  into the other (due to the nominal sub-theory relation).
wenzelm@30296
   105
wenzelm@30296
   106
  The \isa{begin} operation starts a new theory by importing
wenzelm@30296
   107
  several parent theories and entering a special \isa{draft} mode,
wenzelm@30296
   108
  which is sustained until the final \isa{end} operation.  A draft
wenzelm@30296
   109
  theory acts like a linear type, where updates invalidate earlier
wenzelm@30296
   110
  versions.  An invalidated draft is called ``stale''.
wenzelm@30296
   111
wenzelm@30296
   112
  The \isa{checkpoint} operation produces an intermediate stepping
wenzelm@30296
   113
  stone that will survive the next update: both the original and the
wenzelm@30296
   114
  changed theory remain valid and are related by the sub-theory
wenzelm@30296
   115
  relation.  Checkpointing essentially recovers purely functional
wenzelm@30296
   116
  theory values, at the expense of some extra internal bookkeeping.
wenzelm@30296
   117
wenzelm@30296
   118
  The \isa{copy} operation produces an auxiliary version that has
wenzelm@30296
   119
  the same data content, but is unrelated to the original: updates of
wenzelm@30296
   120
  the copy do not affect the original, neither does the sub-theory
wenzelm@30296
   121
  relation hold.
wenzelm@30296
   122
wenzelm@30296
   123
  \medskip The example in \figref{fig:ex-theory} below shows a theory
wenzelm@30296
   124
  graph derived from \isa{Pure}, with theory \isa{Length}
wenzelm@30296
   125
  importing \isa{Nat} and \isa{List}.  The body of \isa{Length} consists of a sequence of updates, working mostly on
wenzelm@30296
   126
  drafts.  Intermediate checkpoints may occur as well, due to the
wenzelm@30296
   127
  history mechanism provided by the Isar top-level, cf.\
wenzelm@30296
   128
  \secref{sec:isar-toplevel}.
wenzelm@30296
   129
wenzelm@30296
   130
  \begin{figure}[htb]
wenzelm@30296
   131
  \begin{center}
wenzelm@30296
   132
  \begin{tabular}{rcccl}
wenzelm@30296
   133
        &            & \isa{Pure} \\
wenzelm@30296
   134
        &            & \isa{{\isasymdown}} \\
wenzelm@30296
   135
        &            & \isa{FOL} \\
wenzelm@30296
   136
        & $\swarrow$ &              & $\searrow$ & \\
wenzelm@30296
   137
  \isa{Nat} &    &              &            & \isa{List} \\
wenzelm@30296
   138
        & $\searrow$ &              & $\swarrow$ \\
wenzelm@30296
   139
        &            & \isa{Length} \\
wenzelm@30296
   140
        &            & \multicolumn{3}{l}{~~\hyperlink{keyword.imports}{\mbox{\isa{\isakeyword{imports}}}}} \\
wenzelm@30296
   141
        &            & \multicolumn{3}{l}{~~\hyperlink{keyword.begin}{\mbox{\isa{\isakeyword{begin}}}}} \\
wenzelm@30296
   142
        &            & $\vdots$~~ \\
wenzelm@30296
   143
        &            & \isa{{\isasymbullet}}~~ \\
wenzelm@30296
   144
        &            & $\vdots$~~ \\
wenzelm@30296
   145
        &            & \isa{{\isasymbullet}}~~ \\
wenzelm@30296
   146
        &            & $\vdots$~~ \\
wenzelm@30296
   147
        &            & \multicolumn{3}{l}{~~\hyperlink{command.end}{\mbox{\isa{\isacommand{end}}}}} \\
wenzelm@30296
   148
  \end{tabular}
wenzelm@30296
   149
  \caption{A theory definition depending on ancestors}\label{fig:ex-theory}
wenzelm@30296
   150
  \end{center}
wenzelm@30296
   151
  \end{figure}
wenzelm@30296
   152
wenzelm@30296
   153
  \medskip There is a separate notion of \emph{theory reference} for
wenzelm@30296
   154
  maintaining a live link to an evolving theory context: updates on
wenzelm@30296
   155
  drafts are propagated automatically.  Dynamic updating stops after
wenzelm@30296
   156
  an explicit \isa{end} only.
wenzelm@30296
   157
wenzelm@30296
   158
  Derived entities may store a theory reference in order to indicate
wenzelm@30296
   159
  the context they belong to.  This implicitly assumes monotonic
wenzelm@30296
   160
  reasoning, because the referenced context may become larger without
wenzelm@30296
   161
  further notice.%
wenzelm@30296
   162
\end{isamarkuptext}%
wenzelm@30296
   163
\isamarkuptrue%
wenzelm@30296
   164
%
wenzelm@30296
   165
\isadelimmlref
wenzelm@30296
   166
%
wenzelm@30296
   167
\endisadelimmlref
wenzelm@30296
   168
%
wenzelm@30296
   169
\isatagmlref
wenzelm@30296
   170
%
wenzelm@30296
   171
\begin{isamarkuptext}%
wenzelm@30296
   172
\begin{mldecls}
wenzelm@30296
   173
  \indexdef{}{ML type}{theory}\verb|type theory| \\
wenzelm@30296
   174
  \indexdef{}{ML}{Theory.subthy}\verb|Theory.subthy: theory * theory -> bool| \\
wenzelm@30296
   175
  \indexdef{}{ML}{Theory.merge}\verb|Theory.merge: theory * theory -> theory| \\
wenzelm@30296
   176
  \indexdef{}{ML}{Theory.checkpoint}\verb|Theory.checkpoint: theory -> theory| \\
wenzelm@30296
   177
  \indexdef{}{ML}{Theory.copy}\verb|Theory.copy: theory -> theory| \\
wenzelm@30296
   178
  \end{mldecls}
wenzelm@30296
   179
  \begin{mldecls}
wenzelm@30296
   180
  \indexdef{}{ML type}{theory\_ref}\verb|type theory_ref| \\
wenzelm@30296
   181
  \indexdef{}{ML}{Theory.deref}\verb|Theory.deref: theory_ref -> theory| \\
wenzelm@30296
   182
  \indexdef{}{ML}{Theory.check\_thy}\verb|Theory.check_thy: theory -> theory_ref| \\
wenzelm@30296
   183
  \end{mldecls}
wenzelm@30296
   184
wenzelm@30296
   185
  \begin{description}
wenzelm@30296
   186
wenzelm@30296
   187
  \item \verb|theory| represents theory contexts.  This is
wenzelm@30296
   188
  essentially a linear type!  Most operations destroy the original
wenzelm@30296
   189
  version, which then becomes ``stale''.
wenzelm@30296
   190
wenzelm@30296
   191
  \item \verb|Theory.subthy|~\isa{{\isacharparenleft}thy\isactrlsub {\isadigit{1}}{\isacharcomma}\ thy\isactrlsub {\isadigit{2}}{\isacharparenright}}
wenzelm@30296
   192
  compares theories according to the inherent graph structure of the
wenzelm@30296
   193
  construction.  This sub-theory relation is a nominal approximation
wenzelm@30296
   194
  of inclusion (\isa{{\isasymsubseteq}}) of the corresponding content.
wenzelm@30296
   195
wenzelm@30296
   196
  \item \verb|Theory.merge|~\isa{{\isacharparenleft}thy\isactrlsub {\isadigit{1}}{\isacharcomma}\ thy\isactrlsub {\isadigit{2}}{\isacharparenright}}
wenzelm@30296
   197
  absorbs one theory into the other.  This fails for unrelated
wenzelm@30296
   198
  theories!
wenzelm@30296
   199
wenzelm@30296
   200
  \item \verb|Theory.checkpoint|~\isa{thy} produces a safe
wenzelm@30296
   201
  stepping stone in the linear development of \isa{thy}.  The next
wenzelm@30296
   202
  update will result in two related, valid theories.
wenzelm@30296
   203
wenzelm@33526
   204
  \item \verb|Theory.copy|~\isa{thy} produces a variant of \isa{thy} with the same data.  The result is not related to the
wenzelm@33526
   205
  original; the original is unchanged.
wenzelm@30296
   206
wenzelm@30296
   207
  \item \verb|theory_ref| represents a sliding reference to an
wenzelm@30296
   208
  always valid theory; updates on the original are propagated
wenzelm@30296
   209
  automatically.
wenzelm@30296
   210
wenzelm@30296
   211
  \item \verb|Theory.deref|~\isa{thy{\isacharunderscore}ref} turns a \verb|theory_ref| into an \verb|theory| value.  As the referenced
wenzelm@30296
   212
  theory evolves monotonically over time, later invocations of \verb|Theory.deref| may refer to a larger context.
wenzelm@30296
   213
wenzelm@30296
   214
  \item \verb|Theory.check_thy|~\isa{thy} produces a \verb|theory_ref| from a valid \verb|theory| value.
wenzelm@30296
   215
wenzelm@30296
   216
  \end{description}%
wenzelm@30296
   217
\end{isamarkuptext}%
wenzelm@30296
   218
\isamarkuptrue%
wenzelm@30296
   219
%
wenzelm@30296
   220
\endisatagmlref
wenzelm@30296
   221
{\isafoldmlref}%
wenzelm@30296
   222
%
wenzelm@30296
   223
\isadelimmlref
wenzelm@30296
   224
%
wenzelm@30296
   225
\endisadelimmlref
wenzelm@30296
   226
%
wenzelm@30296
   227
\isamarkupsubsection{Proof context \label{sec:context-proof}%
wenzelm@30296
   228
}
wenzelm@30296
   229
\isamarkuptrue%
wenzelm@30296
   230
%
wenzelm@30296
   231
\begin{isamarkuptext}%
wenzelm@30296
   232
A proof context is a container for pure data with a back-reference
wenzelm@30296
   233
  to the theory it belongs to.  The \isa{init} operation creates a
wenzelm@30296
   234
  proof context from a given theory.  Modifications to draft theories
wenzelm@30296
   235
  are propagated to the proof context as usual, but there is also an
wenzelm@30296
   236
  explicit \isa{transfer} operation to force resynchronization
wenzelm@30296
   237
  with more substantial updates to the underlying theory.  The actual
wenzelm@30296
   238
  context data does not require any special bookkeeping, thanks to the
wenzelm@30296
   239
  lack of destructive features.
wenzelm@30296
   240
wenzelm@30296
   241
  Entities derived in a proof context need to record inherent logical
wenzelm@30296
   242
  requirements explicitly, since there is no separate context
wenzelm@30296
   243
  identification as for theories.  For example, hypotheses used in
wenzelm@30296
   244
  primitive derivations (cf.\ \secref{sec:thms}) are recorded
wenzelm@30296
   245
  separately within the sequent \isa{{\isasymGamma}\ {\isasymturnstile}\ {\isasymphi}}, just to make double
wenzelm@30296
   246
  sure.  Results could still leak into an alien proof context due to
wenzelm@30296
   247
  programming errors, but Isabelle/Isar includes some extra validity
wenzelm@30296
   248
  checks in critical positions, notably at the end of a sub-proof.
wenzelm@30296
   249
wenzelm@30296
   250
  Proof contexts may be manipulated arbitrarily, although the common
wenzelm@30296
   251
  discipline is to follow block structure as a mental model: a given
wenzelm@30296
   252
  context is extended consecutively, and results are exported back
wenzelm@30296
   253
  into the original context.  Note that the Isar proof states model
wenzelm@30296
   254
  block-structured reasoning explicitly, using a stack of proof
wenzelm@30296
   255
  contexts internally.%
wenzelm@30296
   256
\end{isamarkuptext}%
wenzelm@30296
   257
\isamarkuptrue%
wenzelm@30296
   258
%
wenzelm@30296
   259
\isadelimmlref
wenzelm@30296
   260
%
wenzelm@30296
   261
\endisadelimmlref
wenzelm@30296
   262
%
wenzelm@30296
   263
\isatagmlref
wenzelm@30296
   264
%
wenzelm@30296
   265
\begin{isamarkuptext}%
wenzelm@30296
   266
\begin{mldecls}
wenzelm@30296
   267
  \indexdef{}{ML type}{Proof.context}\verb|type Proof.context| \\
wenzelm@30296
   268
  \indexdef{}{ML}{ProofContext.init}\verb|ProofContext.init: theory -> Proof.context| \\
wenzelm@30296
   269
  \indexdef{}{ML}{ProofContext.theory\_of}\verb|ProofContext.theory_of: Proof.context -> theory| \\
wenzelm@30296
   270
  \indexdef{}{ML}{ProofContext.transfer}\verb|ProofContext.transfer: theory -> Proof.context -> Proof.context| \\
wenzelm@30296
   271
  \end{mldecls}
wenzelm@30296
   272
wenzelm@30296
   273
  \begin{description}
wenzelm@30296
   274
wenzelm@30296
   275
  \item \verb|Proof.context| represents proof contexts.  Elements
wenzelm@30296
   276
  of this type are essentially pure values, with a sliding reference
wenzelm@30296
   277
  to the background theory.
wenzelm@30296
   278
wenzelm@30296
   279
  \item \verb|ProofContext.init|~\isa{thy} produces a proof context
wenzelm@30296
   280
  derived from \isa{thy}, initializing all data.
wenzelm@30296
   281
wenzelm@30296
   282
  \item \verb|ProofContext.theory_of|~\isa{ctxt} selects the
wenzelm@30296
   283
  background theory from \isa{ctxt}, dereferencing its internal
wenzelm@30296
   284
  \verb|theory_ref|.
wenzelm@30296
   285
wenzelm@30296
   286
  \item \verb|ProofContext.transfer|~\isa{thy\ ctxt} promotes the
wenzelm@30296
   287
  background theory of \isa{ctxt} to the super theory \isa{thy}.
wenzelm@30296
   288
wenzelm@30296
   289
  \end{description}%
wenzelm@30296
   290
\end{isamarkuptext}%
wenzelm@30296
   291
\isamarkuptrue%
wenzelm@30296
   292
%
wenzelm@30296
   293
\endisatagmlref
wenzelm@30296
   294
{\isafoldmlref}%
wenzelm@30296
   295
%
wenzelm@30296
   296
\isadelimmlref
wenzelm@30296
   297
%
wenzelm@30296
   298
\endisadelimmlref
wenzelm@30296
   299
%
wenzelm@30296
   300
\isamarkupsubsection{Generic contexts \label{sec:generic-context}%
wenzelm@30296
   301
}
wenzelm@30296
   302
\isamarkuptrue%
wenzelm@30296
   303
%
wenzelm@30296
   304
\begin{isamarkuptext}%
wenzelm@30296
   305
A generic context is the disjoint sum of either a theory or proof
wenzelm@30296
   306
  context.  Occasionally, this enables uniform treatment of generic
wenzelm@30296
   307
  context data, typically extra-logical information.  Operations on
wenzelm@30296
   308
  generic contexts include the usual injections, partial selections,
wenzelm@30296
   309
  and combinators for lifting operations on either component of the
wenzelm@30296
   310
  disjoint sum.
wenzelm@30296
   311
wenzelm@30296
   312
  Moreover, there are total operations \isa{theory{\isacharunderscore}of} and \isa{proof{\isacharunderscore}of} to convert a generic context into either kind: a theory
wenzelm@30296
   313
  can always be selected from the sum, while a proof context might
wenzelm@30296
   314
  have to be constructed by an ad-hoc \isa{init} operation.%
wenzelm@30296
   315
\end{isamarkuptext}%
wenzelm@30296
   316
\isamarkuptrue%
wenzelm@30296
   317
%
wenzelm@30296
   318
\isadelimmlref
wenzelm@30296
   319
%
wenzelm@30296
   320
\endisadelimmlref
wenzelm@30296
   321
%
wenzelm@30296
   322
\isatagmlref
wenzelm@30296
   323
%
wenzelm@30296
   324
\begin{isamarkuptext}%
wenzelm@30296
   325
\begin{mldecls}
wenzelm@30296
   326
  \indexdef{}{ML type}{Context.generic}\verb|type Context.generic| \\
wenzelm@30296
   327
  \indexdef{}{ML}{Context.theory\_of}\verb|Context.theory_of: Context.generic -> theory| \\
wenzelm@30296
   328
  \indexdef{}{ML}{Context.proof\_of}\verb|Context.proof_of: Context.generic -> Proof.context| \\
wenzelm@30296
   329
  \end{mldecls}
wenzelm@30296
   330
wenzelm@30296
   331
  \begin{description}
wenzelm@30296
   332
wenzelm@30296
   333
  \item \verb|Context.generic| is the direct sum of \verb|theory| and \verb|Proof.context|, with the datatype
wenzelm@30296
   334
  constructors \verb|Context.Theory| and \verb|Context.Proof|.
wenzelm@30296
   335
wenzelm@30296
   336
  \item \verb|Context.theory_of|~\isa{context} always produces a
wenzelm@30296
   337
  theory from the generic \isa{context}, using \verb|ProofContext.theory_of| as required.
wenzelm@30296
   338
wenzelm@30296
   339
  \item \verb|Context.proof_of|~\isa{context} always produces a
wenzelm@30296
   340
  proof context from the generic \isa{context}, using \verb|ProofContext.init| as required (note that this re-initializes the
wenzelm@30296
   341
  context data with each invocation).
wenzelm@30296
   342
wenzelm@30296
   343
  \end{description}%
wenzelm@30296
   344
\end{isamarkuptext}%
wenzelm@30296
   345
\isamarkuptrue%
wenzelm@30296
   346
%
wenzelm@30296
   347
\endisatagmlref
wenzelm@30296
   348
{\isafoldmlref}%
wenzelm@30296
   349
%
wenzelm@30296
   350
\isadelimmlref
wenzelm@30296
   351
%
wenzelm@30296
   352
\endisadelimmlref
wenzelm@30296
   353
%
wenzelm@30296
   354
\isamarkupsubsection{Context data \label{sec:context-data}%
wenzelm@30296
   355
}
wenzelm@30296
   356
\isamarkuptrue%
wenzelm@30296
   357
%
wenzelm@30296
   358
\begin{isamarkuptext}%
wenzelm@33526
   359
The main purpose of theory and proof contexts is to manage
wenzelm@33526
   360
  arbitrary (pure) data.  New data types can be declared incrementally
wenzelm@33526
   361
  at compile time.  There are separate declaration mechanisms for any
wenzelm@33526
   362
  of the three kinds of contexts: theory, proof, generic.
wenzelm@30296
   363
wenzelm@33526
   364
  \paragraph{Theory data} declarations need to implement the following
wenzelm@33526
   365
  SML signature:
wenzelm@30296
   366
wenzelm@30296
   367
  \medskip
wenzelm@30296
   368
  \begin{tabular}{ll}
wenzelm@30296
   369
  \isa{{\isasymtype}\ T} & representing type \\
wenzelm@30296
   370
  \isa{{\isasymval}\ empty{\isacharcolon}\ T} & empty default value \\
wenzelm@30296
   371
  \isa{{\isasymval}\ extend{\isacharcolon}\ T\ {\isasymrightarrow}\ T} & re-initialize on import \\
wenzelm@30296
   372
  \isa{{\isasymval}\ merge{\isacharcolon}\ T\ {\isasymtimes}\ T\ {\isasymrightarrow}\ T} & join on import \\
wenzelm@30296
   373
  \end{tabular}
wenzelm@30296
   374
  \medskip
wenzelm@30296
   375
wenzelm@30296
   376
  \noindent The \isa{empty} value acts as initial default for
wenzelm@33526
   377
  \emph{any} theory that does not declare actual data content; \isa{extend} is acts like a unitary version of \isa{merge}.
wenzelm@30296
   378
wenzelm@33526
   379
  \paragraph{Proof context data} declarations need to implement the
wenzelm@33526
   380
  following SML signature:
wenzelm@30296
   381
wenzelm@30296
   382
  \medskip
wenzelm@30296
   383
  \begin{tabular}{ll}
wenzelm@30296
   384
  \isa{{\isasymtype}\ T} & representing type \\
wenzelm@30296
   385
  \isa{{\isasymval}\ init{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} & produce initial value \\
wenzelm@30296
   386
  \end{tabular}
wenzelm@30296
   387
  \medskip
wenzelm@30296
   388
wenzelm@30296
   389
  \noindent The \isa{init} operation is supposed to produce a pure
wenzelm@30296
   390
  value from the given background theory.
wenzelm@30296
   391
wenzelm@30296
   392
  \paragraph{Generic data} provides a hybrid interface for both theory
wenzelm@33526
   393
  and proof data.  The \isa{init} operation for proof contexts is
wenzelm@33526
   394
  predefined to select the current data value from the background
wenzelm@33526
   395
  theory.
wenzelm@30296
   396
wenzelm@30296
   397
  \bigskip A data declaration of type \isa{T} results in the
wenzelm@30296
   398
  following interface:
wenzelm@30296
   399
wenzelm@30296
   400
  \medskip
wenzelm@30296
   401
  \begin{tabular}{ll}
wenzelm@30296
   402
  \isa{get{\isacharcolon}\ context\ {\isasymrightarrow}\ T} \\
wenzelm@30296
   403
  \isa{put{\isacharcolon}\ T\ {\isasymrightarrow}\ context\ {\isasymrightarrow}\ context} \\
wenzelm@30296
   404
  \isa{map{\isacharcolon}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ context\ {\isasymrightarrow}\ context} \\
wenzelm@30296
   405
  \end{tabular}
wenzelm@30296
   406
  \medskip
wenzelm@30296
   407
wenzelm@33526
   408
  \noindent These other operations provide access for the particular
wenzelm@33526
   409
  kind of context (theory, proof, or generic context).  Note that this
wenzelm@33526
   410
  is a safe interface: there is no other way to access the
wenzelm@33526
   411
  corresponding data slot of a context.  By keeping these operations
wenzelm@33526
   412
  private, a component may maintain abstract values authentically,
wenzelm@33526
   413
  without other components interfering.%
wenzelm@30296
   414
\end{isamarkuptext}%
wenzelm@30296
   415
\isamarkuptrue%
wenzelm@30296
   416
%
wenzelm@30296
   417
\isadelimmlref
wenzelm@30296
   418
%
wenzelm@30296
   419
\endisadelimmlref
wenzelm@30296
   420
%
wenzelm@30296
   421
\isatagmlref
wenzelm@30296
   422
%
wenzelm@30296
   423
\begin{isamarkuptext}%
wenzelm@30296
   424
\begin{mldecls}
wenzelm@33526
   425
  \indexdef{}{ML functor}{Theory\_Data}\verb|functor Theory_Data| \\
wenzelm@33526
   426
  \indexdef{}{ML functor}{Proof\_Data}\verb|functor Proof_Data| \\
wenzelm@33526
   427
  \indexdef{}{ML functor}{Generic\_Data}\verb|functor Generic_Data| \\
wenzelm@30296
   428
  \end{mldecls}
wenzelm@30296
   429
wenzelm@30296
   430
  \begin{description}
wenzelm@30296
   431
wenzelm@33526
   432
  \item \verb|Theory_Data|\isa{{\isacharparenleft}spec{\isacharparenright}} declares data for
wenzelm@30296
   433
  type \verb|theory| according to the specification provided as
wenzelm@30296
   434
  argument structure.  The resulting structure provides data init and
wenzelm@30296
   435
  access operations as described above.
wenzelm@30296
   436
wenzelm@33526
   437
  \item \verb|Proof_Data|\isa{{\isacharparenleft}spec{\isacharparenright}} is analogous to
wenzelm@33526
   438
  \verb|Theory_Data| for type \verb|Proof.context|.
wenzelm@30296
   439
wenzelm@33526
   440
  \item \verb|Generic_Data|\isa{{\isacharparenleft}spec{\isacharparenright}} is analogous to
wenzelm@33526
   441
  \verb|Theory_Data| for type \verb|Context.generic|.
wenzelm@30296
   442
wenzelm@30296
   443
  \end{description}%
wenzelm@30296
   444
\end{isamarkuptext}%
wenzelm@30296
   445
\isamarkuptrue%
wenzelm@30296
   446
%
wenzelm@30296
   447
\endisatagmlref
wenzelm@30296
   448
{\isafoldmlref}%
wenzelm@30296
   449
%
wenzelm@30296
   450
\isadelimmlref
wenzelm@30296
   451
%
wenzelm@30296
   452
\endisadelimmlref
wenzelm@30296
   453
%
wenzelm@30296
   454
\isamarkupsection{Names \label{sec:names}%
wenzelm@30296
   455
}
wenzelm@30296
   456
\isamarkuptrue%
wenzelm@30296
   457
%
wenzelm@30296
   458
\begin{isamarkuptext}%
wenzelm@30296
   459
In principle, a name is just a string, but there are various
wenzelm@30296
   460
  convention for encoding additional structure.  For example, ``\isa{Foo{\isachardot}bar{\isachardot}baz}'' is considered as a qualified name consisting of
wenzelm@30296
   461
  three basic name components.  The individual constituents of a name
wenzelm@30296
   462
  may have further substructure, e.g.\ the string
wenzelm@30296
   463
  ``\verb,\,\verb,<alpha>,'' encodes as a single symbol.%
wenzelm@30296
   464
\end{isamarkuptext}%
wenzelm@30296
   465
\isamarkuptrue%
wenzelm@30296
   466
%
wenzelm@30296
   467
\isamarkupsubsection{Strings of symbols%
wenzelm@30296
   468
}
wenzelm@30296
   469
\isamarkuptrue%
wenzelm@30296
   470
%
wenzelm@30296
   471
\begin{isamarkuptext}%
wenzelm@30296
   472
A \emph{symbol} constitutes the smallest textual unit in Isabelle
wenzelm@30296
   473
  --- raw characters are normally not encountered at all.  Isabelle
wenzelm@30296
   474
  strings consist of a sequence of symbols, represented as a packed
wenzelm@30296
   475
  string or a list of strings.  Each symbol is in itself a small
wenzelm@30296
   476
  string, which has either one of the following forms:
wenzelm@30296
   477
wenzelm@30296
   478
  \begin{enumerate}
wenzelm@30296
   479
wenzelm@30296
   480
  \item a single ASCII character ``\isa{c}'', for example
wenzelm@30296
   481
  ``\verb,a,'',
wenzelm@30296
   482
wenzelm@30296
   483
  \item a regular symbol ``\verb,\,\verb,<,\isa{ident}\verb,>,'',
wenzelm@30296
   484
  for example ``\verb,\,\verb,<alpha>,'',
wenzelm@30296
   485
wenzelm@30296
   486
  \item a control symbol ``\verb,\,\verb,<^,\isa{ident}\verb,>,'',
wenzelm@30296
   487
  for example ``\verb,\,\verb,<^bold>,'',
wenzelm@30296
   488
wenzelm@30296
   489
  \item a raw symbol ``\verb,\,\verb,<^raw:,\isa{text}\verb,>,''
wenzelm@30296
   490
  where \isa{text} constists of printable characters excluding
wenzelm@30296
   491
  ``\verb,.,'' and ``\verb,>,'', for example
wenzelm@30296
   492
  ``\verb,\,\verb,<^raw:$\sum_{i = 1}^n$>,'',
wenzelm@30296
   493
wenzelm@30296
   494
  \item a numbered raw control symbol ``\verb,\,\verb,<^raw,\isa{n}\verb,>, where \isa{n} consists of digits, for example
wenzelm@30296
   495
  ``\verb,\,\verb,<^raw42>,''.
wenzelm@30296
   496
wenzelm@30296
   497
  \end{enumerate}
wenzelm@30296
   498
wenzelm@30296
   499
  \noindent The \isa{ident} syntax for symbol names is \isa{letter\ {\isacharparenleft}letter\ {\isacharbar}\ digit{\isacharparenright}\isactrlsup {\isacharasterisk}}, where \isa{letter\ {\isacharequal}\ A{\isachardot}{\isachardot}Za{\isachardot}{\isachardot}z} and \isa{digit\ {\isacharequal}\ {\isadigit{0}}{\isachardot}{\isachardot}{\isadigit{9}}}.  There are infinitely many
wenzelm@30296
   500
  regular symbols and control symbols, but a fixed collection of
wenzelm@30296
   501
  standard symbols is treated specifically.  For example,
wenzelm@30296
   502
  ``\verb,\,\verb,<alpha>,'' is classified as a letter, which means it
wenzelm@30296
   503
  may occur within regular Isabelle identifiers.
wenzelm@30296
   504
wenzelm@30296
   505
  Since the character set underlying Isabelle symbols is 7-bit ASCII
wenzelm@30296
   506
  and 8-bit characters are passed through transparently, Isabelle may
wenzelm@30296
   507
  also process Unicode/UCS data in UTF-8 encoding.  Unicode provides
wenzelm@30296
   508
  its own collection of mathematical symbols, but there is no built-in
wenzelm@30296
   509
  link to the standard collection of Isabelle.
wenzelm@30296
   510
wenzelm@30296
   511
  \medskip Output of Isabelle symbols depends on the print mode
wenzelm@30296
   512
  (\secref{print-mode}).  For example, the standard {\LaTeX} setup of
wenzelm@30296
   513
  the Isabelle document preparation system would present
wenzelm@30296
   514
  ``\verb,\,\verb,<alpha>,'' as \isa{{\isasymalpha}}, and
wenzelm@30296
   515
  ``\verb,\,\verb,<^bold>,\verb,\,\verb,<alpha>,'' as \isa{\isactrlbold {\isasymalpha}}.%
wenzelm@30296
   516
\end{isamarkuptext}%
wenzelm@30296
   517
\isamarkuptrue%
wenzelm@30296
   518
%
wenzelm@30296
   519
\isadelimmlref
wenzelm@30296
   520
%
wenzelm@30296
   521
\endisadelimmlref
wenzelm@30296
   522
%
wenzelm@30296
   523
\isatagmlref
wenzelm@30296
   524
%
wenzelm@30296
   525
\begin{isamarkuptext}%
wenzelm@30296
   526
\begin{mldecls}
wenzelm@30296
   527
  \indexdef{}{ML type}{Symbol.symbol}\verb|type Symbol.symbol| \\
wenzelm@30296
   528
  \indexdef{}{ML}{Symbol.explode}\verb|Symbol.explode: string -> Symbol.symbol list| \\
wenzelm@30296
   529
  \indexdef{}{ML}{Symbol.is\_letter}\verb|Symbol.is_letter: Symbol.symbol -> bool| \\
wenzelm@30296
   530
  \indexdef{}{ML}{Symbol.is\_digit}\verb|Symbol.is_digit: Symbol.symbol -> bool| \\
wenzelm@30296
   531
  \indexdef{}{ML}{Symbol.is\_quasi}\verb|Symbol.is_quasi: Symbol.symbol -> bool| \\
wenzelm@30296
   532
  \indexdef{}{ML}{Symbol.is\_blank}\verb|Symbol.is_blank: Symbol.symbol -> bool| \\
wenzelm@30296
   533
  \end{mldecls}
wenzelm@30296
   534
  \begin{mldecls}
wenzelm@30296
   535
  \indexdef{}{ML type}{Symbol.sym}\verb|type Symbol.sym| \\
wenzelm@30296
   536
  \indexdef{}{ML}{Symbol.decode}\verb|Symbol.decode: Symbol.symbol -> Symbol.sym| \\
wenzelm@30296
   537
  \end{mldecls}
wenzelm@30296
   538
wenzelm@30296
   539
  \begin{description}
wenzelm@30296
   540
wenzelm@30296
   541
  \item \verb|Symbol.symbol| represents individual Isabelle
wenzelm@30296
   542
  symbols; this is an alias for \verb|string|.
wenzelm@30296
   543
wenzelm@30296
   544
  \item \verb|Symbol.explode|~\isa{str} produces a symbol list
wenzelm@30296
   545
  from the packed form.  This function supercedes \verb|String.explode| for virtually all purposes of manipulating text in
wenzelm@30296
   546
  Isabelle!
wenzelm@30296
   547
wenzelm@30296
   548
  \item \verb|Symbol.is_letter|, \verb|Symbol.is_digit|, \verb|Symbol.is_quasi|, \verb|Symbol.is_blank| classify standard
wenzelm@30296
   549
  symbols according to fixed syntactic conventions of Isabelle, cf.\
wenzelm@30296
   550
  \cite{isabelle-isar-ref}.
wenzelm@30296
   551
wenzelm@30296
   552
  \item \verb|Symbol.sym| is a concrete datatype that represents
wenzelm@30296
   553
  the different kinds of symbols explicitly, with constructors \verb|Symbol.Char|, \verb|Symbol.Sym|, \verb|Symbol.Ctrl|, \verb|Symbol.Raw|.
wenzelm@30296
   554
wenzelm@30296
   555
  \item \verb|Symbol.decode| converts the string representation of a
wenzelm@30296
   556
  symbol into the datatype version.
wenzelm@30296
   557
wenzelm@30296
   558
  \end{description}%
wenzelm@30296
   559
\end{isamarkuptext}%
wenzelm@30296
   560
\isamarkuptrue%
wenzelm@30296
   561
%
wenzelm@30296
   562
\endisatagmlref
wenzelm@30296
   563
{\isafoldmlref}%
wenzelm@30296
   564
%
wenzelm@30296
   565
\isadelimmlref
wenzelm@30296
   566
%
wenzelm@30296
   567
\endisadelimmlref
wenzelm@30296
   568
%
wenzelm@30296
   569
\isamarkupsubsection{Basic names \label{sec:basic-names}%
wenzelm@30296
   570
}
wenzelm@30296
   571
\isamarkuptrue%
wenzelm@30296
   572
%
wenzelm@30296
   573
\begin{isamarkuptext}%
wenzelm@30296
   574
A \emph{basic name} essentially consists of a single Isabelle
wenzelm@30296
   575
  identifier.  There are conventions to mark separate classes of basic
wenzelm@30296
   576
  names, by attaching a suffix of underscores: one underscore means
wenzelm@30296
   577
  \emph{internal name}, two underscores means \emph{Skolem name},
wenzelm@30296
   578
  three underscores means \emph{internal Skolem name}.
wenzelm@30296
   579
wenzelm@30296
   580
  For example, the basic name \isa{foo} has the internal version
wenzelm@30296
   581
  \isa{foo{\isacharunderscore}}, with Skolem versions \isa{foo{\isacharunderscore}{\isacharunderscore}} and \isa{foo{\isacharunderscore}{\isacharunderscore}{\isacharunderscore}}, respectively.
wenzelm@30296
   582
wenzelm@30296
   583
  These special versions provide copies of the basic name space, apart
wenzelm@30296
   584
  from anything that normally appears in the user text.  For example,
wenzelm@30296
   585
  system generated variables in Isar proof contexts are usually marked
wenzelm@30296
   586
  as internal, which prevents mysterious name references like \isa{xaa} to appear in the text.
wenzelm@30296
   587
wenzelm@30296
   588
  \medskip Manipulating binding scopes often requires on-the-fly
wenzelm@30296
   589
  renamings.  A \emph{name context} contains a collection of already
wenzelm@30296
   590
  used names.  The \isa{declare} operation adds names to the
wenzelm@30296
   591
  context.
wenzelm@30296
   592
wenzelm@30296
   593
  The \isa{invents} operation derives a number of fresh names from
wenzelm@30296
   594
  a given starting point.  For example, the first three names derived
wenzelm@30296
   595
  from \isa{a} are \isa{a}, \isa{b}, \isa{c}.
wenzelm@30296
   596
wenzelm@30296
   597
  The \isa{variants} operation produces fresh names by
wenzelm@30296
   598
  incrementing tentative names as base-26 numbers (with digits \isa{a{\isachardot}{\isachardot}z}) until all clashes are resolved.  For example, name \isa{foo} results in variants \isa{fooa}, \isa{foob}, \isa{fooc}, \dots, \isa{fooaa}, \isa{fooab} etc.; each renaming
wenzelm@30296
   599
  step picks the next unused variant from this sequence.%
wenzelm@30296
   600
\end{isamarkuptext}%
wenzelm@30296
   601
\isamarkuptrue%
wenzelm@30296
   602
%
wenzelm@30296
   603
\isadelimmlref
wenzelm@30296
   604
%
wenzelm@30296
   605
\endisadelimmlref
wenzelm@30296
   606
%
wenzelm@30296
   607
\isatagmlref
wenzelm@30296
   608
%
wenzelm@30296
   609
\begin{isamarkuptext}%
wenzelm@30296
   610
\begin{mldecls}
wenzelm@30296
   611
  \indexdef{}{ML}{Name.internal}\verb|Name.internal: string -> string| \\
wenzelm@30296
   612
  \indexdef{}{ML}{Name.skolem}\verb|Name.skolem: string -> string| \\
wenzelm@30296
   613
  \end{mldecls}
wenzelm@30296
   614
  \begin{mldecls}
wenzelm@30296
   615
  \indexdef{}{ML type}{Name.context}\verb|type Name.context| \\
wenzelm@30296
   616
  \indexdef{}{ML}{Name.context}\verb|Name.context: Name.context| \\
wenzelm@30296
   617
  \indexdef{}{ML}{Name.declare}\verb|Name.declare: string -> Name.context -> Name.context| \\
wenzelm@30296
   618
  \indexdef{}{ML}{Name.invents}\verb|Name.invents: Name.context -> string -> int -> string list| \\
wenzelm@30296
   619
  \indexdef{}{ML}{Name.variants}\verb|Name.variants: string list -> Name.context -> string list * Name.context| \\
wenzelm@30296
   620
  \end{mldecls}
wenzelm@30296
   621
wenzelm@30296
   622
  \begin{description}
wenzelm@30296
   623
wenzelm@30296
   624
  \item \verb|Name.internal|~\isa{name} produces an internal name
wenzelm@30296
   625
  by adding one underscore.
wenzelm@30296
   626
wenzelm@30296
   627
  \item \verb|Name.skolem|~\isa{name} produces a Skolem name by
wenzelm@30296
   628
  adding two underscores.
wenzelm@30296
   629
wenzelm@30296
   630
  \item \verb|Name.context| represents the context of already used
wenzelm@30296
   631
  names; the initial value is \verb|Name.context|.
wenzelm@30296
   632
wenzelm@30296
   633
  \item \verb|Name.declare|~\isa{name} enters a used name into the
wenzelm@30296
   634
  context.
wenzelm@30296
   635
wenzelm@30296
   636
  \item \verb|Name.invents|~\isa{context\ name\ n} produces \isa{n} fresh names derived from \isa{name}.
wenzelm@30296
   637
wenzelm@30296
   638
  \item \verb|Name.variants|~\isa{names\ context} produces fresh
wenzelm@30296
   639
  variants of \isa{names}; the result is entered into the context.
wenzelm@30296
   640
wenzelm@30296
   641
  \end{description}%
wenzelm@30296
   642
\end{isamarkuptext}%
wenzelm@30296
   643
\isamarkuptrue%
wenzelm@30296
   644
%
wenzelm@30296
   645
\endisatagmlref
wenzelm@30296
   646
{\isafoldmlref}%
wenzelm@30296
   647
%
wenzelm@30296
   648
\isadelimmlref
wenzelm@30296
   649
%
wenzelm@30296
   650
\endisadelimmlref
wenzelm@30296
   651
%
wenzelm@30296
   652
\isamarkupsubsection{Indexed names%
wenzelm@30296
   653
}
wenzelm@30296
   654
\isamarkuptrue%
wenzelm@30296
   655
%
wenzelm@30296
   656
\begin{isamarkuptext}%
wenzelm@30296
   657
An \emph{indexed name} (or \isa{indexname}) is a pair of a basic
wenzelm@30296
   658
  name and a natural number.  This representation allows efficient
wenzelm@30296
   659
  renaming by incrementing the second component only.  The canonical
wenzelm@30296
   660
  way to rename two collections of indexnames apart from each other is
wenzelm@30296
   661
  this: determine the maximum index \isa{maxidx} of the first
wenzelm@30296
   662
  collection, then increment all indexes of the second collection by
wenzelm@30296
   663
  \isa{maxidx\ {\isacharplus}\ {\isadigit{1}}}; the maximum index of an empty collection is
wenzelm@30296
   664
  \isa{{\isacharminus}{\isadigit{1}}}.
wenzelm@30296
   665
wenzelm@30296
   666
  Occasionally, basic names and indexed names are injected into the
wenzelm@30296
   667
  same pair type: the (improper) indexname \isa{{\isacharparenleft}x{\isacharcomma}\ {\isacharminus}{\isadigit{1}}{\isacharparenright}} is used
wenzelm@30296
   668
  to encode basic names.
wenzelm@30296
   669
wenzelm@30296
   670
  \medskip Isabelle syntax observes the following rules for
wenzelm@30296
   671
  representing an indexname \isa{{\isacharparenleft}x{\isacharcomma}\ i{\isacharparenright}} as a packed string:
wenzelm@30296
   672
wenzelm@30296
   673
  \begin{itemize}
wenzelm@30296
   674
wenzelm@30296
   675
  \item \isa{{\isacharquery}x} if \isa{x} does not end with a digit and \isa{i\ {\isacharequal}\ {\isadigit{0}}},
wenzelm@30296
   676
wenzelm@30296
   677
  \item \isa{{\isacharquery}xi} if \isa{x} does not end with a digit,
wenzelm@30296
   678
wenzelm@30296
   679
  \item \isa{{\isacharquery}x{\isachardot}i} otherwise.
wenzelm@30296
   680
wenzelm@30296
   681
  \end{itemize}
wenzelm@30296
   682
wenzelm@30296
   683
  Indexnames may acquire large index numbers over time.  Results are
wenzelm@30296
   684
  normalized towards \isa{{\isadigit{0}}} at certain checkpoints, notably at
wenzelm@30296
   685
  the end of a proof.  This works by producing variants of the
wenzelm@30296
   686
  corresponding basic name components.  For example, the collection
wenzelm@30296
   687
  \isa{{\isacharquery}x{\isadigit{1}}{\isacharcomma}\ {\isacharquery}x{\isadigit{7}}{\isacharcomma}\ {\isacharquery}x{\isadigit{4}}{\isadigit{2}}} becomes \isa{{\isacharquery}x{\isacharcomma}\ {\isacharquery}xa{\isacharcomma}\ {\isacharquery}xb}.%
wenzelm@30296
   688
\end{isamarkuptext}%
wenzelm@30296
   689
\isamarkuptrue%
wenzelm@30296
   690
%
wenzelm@30296
   691
\isadelimmlref
wenzelm@30296
   692
%
wenzelm@30296
   693
\endisadelimmlref
wenzelm@30296
   694
%
wenzelm@30296
   695
\isatagmlref
wenzelm@30296
   696
%
wenzelm@30296
   697
\begin{isamarkuptext}%
wenzelm@30296
   698
\begin{mldecls}
wenzelm@30296
   699
  \indexdef{}{ML type}{indexname}\verb|type indexname| \\
wenzelm@30296
   700
  \end{mldecls}
wenzelm@30296
   701
wenzelm@30296
   702
  \begin{description}
wenzelm@30296
   703
wenzelm@30296
   704
  \item \verb|indexname| represents indexed names.  This is an
wenzelm@30296
   705
  abbreviation for \verb|string * int|.  The second component is
wenzelm@30296
   706
  usually non-negative, except for situations where \isa{{\isacharparenleft}x{\isacharcomma}\ {\isacharminus}{\isadigit{1}}{\isacharparenright}}
wenzelm@30296
   707
  is used to embed basic names into this type.
wenzelm@30296
   708
wenzelm@30296
   709
  \end{description}%
wenzelm@30296
   710
\end{isamarkuptext}%
wenzelm@30296
   711
\isamarkuptrue%
wenzelm@30296
   712
%
wenzelm@30296
   713
\endisatagmlref
wenzelm@30296
   714
{\isafoldmlref}%
wenzelm@30296
   715
%
wenzelm@30296
   716
\isadelimmlref
wenzelm@30296
   717
%
wenzelm@30296
   718
\endisadelimmlref
wenzelm@30296
   719
%
wenzelm@30296
   720
\isamarkupsubsection{Qualified names and name spaces%
wenzelm@30296
   721
}
wenzelm@30296
   722
\isamarkuptrue%
wenzelm@30296
   723
%
wenzelm@30296
   724
\begin{isamarkuptext}%
wenzelm@30296
   725
A \emph{qualified name} consists of a non-empty sequence of basic
wenzelm@30296
   726
  name components.  The packed representation uses a dot as separator,
wenzelm@30296
   727
  as in ``\isa{A{\isachardot}b{\isachardot}c}''.  The last component is called \emph{base}
wenzelm@30296
   728
  name, the remaining prefix \emph{qualifier} (which may be empty).
wenzelm@30296
   729
  The idea of qualified names is to encode nested structures by
wenzelm@30296
   730
  recording the access paths as qualifiers.  For example, an item
wenzelm@30296
   731
  named ``\isa{A{\isachardot}b{\isachardot}c}'' may be understood as a local entity \isa{c}, within a local structure \isa{b}, within a global
wenzelm@30296
   732
  structure \isa{A}.  Typically, name space hierarchies consist of
wenzelm@30296
   733
  1--2 levels of qualification, but this need not be always so.
wenzelm@30296
   734
wenzelm@30296
   735
  The empty name is commonly used as an indication of unnamed
wenzelm@30296
   736
  entities, whenever this makes any sense.  The basic operations on
wenzelm@30296
   737
  qualified names are smart enough to pass through such improper names
wenzelm@30296
   738
  unchanged.
wenzelm@30296
   739
wenzelm@30296
   740
  \medskip A \isa{naming} policy tells how to turn a name
wenzelm@30296
   741
  specification into a fully qualified internal name (by the \isa{full} operation), and how fully qualified names may be accessed
wenzelm@30296
   742
  externally.  For example, the default naming policy is to prefix an
wenzelm@30296
   743
  implicit path: \isa{full\ x} produces \isa{path{\isachardot}x}, and the
wenzelm@30296
   744
  standard accesses for \isa{path{\isachardot}x} include both \isa{x} and
wenzelm@30296
   745
  \isa{path{\isachardot}x}.  Normally, the naming is implicit in the theory or
wenzelm@30296
   746
  proof context; there are separate versions of the corresponding.
wenzelm@30296
   747
wenzelm@30296
   748
  \medskip A \isa{name\ space} manages a collection of fully
wenzelm@30296
   749
  internalized names, together with a mapping between external names
wenzelm@30296
   750
  and internal names (in both directions).  The corresponding \isa{intern} and \isa{extern} operations are mostly used for
wenzelm@30296
   751
  parsing and printing only!  The \isa{declare} operation augments
wenzelm@30296
   752
  a name space according to the accesses determined by the naming
wenzelm@30296
   753
  policy.
wenzelm@30296
   754
wenzelm@30296
   755
  \medskip As a general principle, there is a separate name space for
wenzelm@30296
   756
  each kind of formal entity, e.g.\ logical constant, type
wenzelm@30296
   757
  constructor, type class, theorem.  It is usually clear from the
wenzelm@30296
   758
  occurrence in concrete syntax (or from the scope) which kind of
wenzelm@30296
   759
  entity a name refers to.  For example, the very same name \isa{c} may be used uniformly for a constant, type constructor, and
wenzelm@30296
   760
  type class.
wenzelm@30296
   761
wenzelm@30296
   762
  There are common schemes to name theorems systematically, according
wenzelm@30296
   763
  to the name of the main logical entity involved, e.g.\ \isa{c{\isachardot}intro} for a canonical theorem related to constant \isa{c}.
wenzelm@30296
   764
  This technique of mapping names from one space into another requires
wenzelm@30296
   765
  some care in order to avoid conflicts.  In particular, theorem names
wenzelm@30296
   766
  derived from a type constructor or type class are better suffixed in
wenzelm@30296
   767
  addition to the usual qualification, e.g.\ \isa{c{\isacharunderscore}type{\isachardot}intro}
wenzelm@30296
   768
  and \isa{c{\isacharunderscore}class{\isachardot}intro} for theorems related to type \isa{c}
wenzelm@30296
   769
  and class \isa{c}, respectively.%
wenzelm@30296
   770
\end{isamarkuptext}%
wenzelm@30296
   771
\isamarkuptrue%
wenzelm@30296
   772
%
wenzelm@30296
   773
\isadelimmlref
wenzelm@30296
   774
%
wenzelm@30296
   775
\endisadelimmlref
wenzelm@30296
   776
%
wenzelm@30296
   777
\isatagmlref
wenzelm@30296
   778
%
wenzelm@30296
   779
\begin{isamarkuptext}%
wenzelm@30296
   780
\begin{mldecls}
wenzelm@30365
   781
  \indexdef{}{ML}{Long\_Name.base\_name}\verb|Long_Name.base_name: string -> string| \\
wenzelm@30365
   782
  \indexdef{}{ML}{Long\_Name.qualifier}\verb|Long_Name.qualifier: string -> string| \\
wenzelm@30365
   783
  \indexdef{}{ML}{Long\_Name.append}\verb|Long_Name.append: string -> string -> string| \\
wenzelm@30365
   784
  \indexdef{}{ML}{Long\_Name.implode}\verb|Long_Name.implode: string list -> string| \\
wenzelm@30365
   785
  \indexdef{}{ML}{Long\_Name.explode}\verb|Long_Name.explode: string -> string list| \\
wenzelm@30296
   786
  \end{mldecls}
wenzelm@30296
   787
  \begin{mldecls}
haftmann@33174
   788
  \indexdef{}{ML type}{Name\_Space.naming}\verb|type Name_Space.naming| \\
haftmann@33174
   789
  \indexdef{}{ML}{Name\_Space.default\_naming}\verb|Name_Space.default_naming: Name_Space.naming| \\
haftmann@33174
   790
  \indexdef{}{ML}{Name\_Space.add\_path}\verb|Name_Space.add_path: string -> Name_Space.naming -> Name_Space.naming| \\
haftmann@33174
   791
  \indexdef{}{ML}{Name\_Space.full\_name}\verb|Name_Space.full_name: Name_Space.naming -> binding -> string| \\
wenzelm@30296
   792
  \end{mldecls}
wenzelm@30296
   793
  \begin{mldecls}
haftmann@33174
   794
  \indexdef{}{ML type}{Name\_Space.T}\verb|type Name_Space.T| \\
haftmann@33174
   795
  \indexdef{}{ML}{Name\_Space.empty}\verb|Name_Space.empty: string -> Name_Space.T| \\
haftmann@33174
   796
  \indexdef{}{ML}{Name\_Space.merge}\verb|Name_Space.merge: Name_Space.T * Name_Space.T -> Name_Space.T| \\
haftmann@33174
   797
  \indexdef{}{ML}{Name\_Space.declare}\verb|Name_Space.declare: bool -> Name_Space.naming -> binding -> Name_Space.T ->|\isasep\isanewline%
haftmann@33174
   798
\verb|  string * Name_Space.T| \\
haftmann@33174
   799
  \indexdef{}{ML}{Name\_Space.intern}\verb|Name_Space.intern: Name_Space.T -> string -> string| \\
haftmann@33174
   800
  \indexdef{}{ML}{Name\_Space.extern}\verb|Name_Space.extern: Name_Space.T -> string -> string| \\
wenzelm@30296
   801
  \end{mldecls}
wenzelm@30296
   802
wenzelm@30296
   803
  \begin{description}
wenzelm@30296
   804
wenzelm@30365
   805
  \item \verb|Long_Name.base_name|~\isa{name} returns the base name of a
wenzelm@30296
   806
  qualified name.
wenzelm@30296
   807
wenzelm@30365
   808
  \item \verb|Long_Name.qualifier|~\isa{name} returns the qualifier
wenzelm@30296
   809
  of a qualified name.
wenzelm@30296
   810
wenzelm@30365
   811
  \item \verb|Long_Name.append|~\isa{name\isactrlisub {\isadigit{1}}\ name\isactrlisub {\isadigit{2}}}
wenzelm@30296
   812
  appends two qualified names.
wenzelm@30296
   813
wenzelm@30365
   814
  \item \verb|Long_Name.implode|~\isa{names} and \verb|Long_Name.explode|~\isa{name} convert between the packed string
wenzelm@30296
   815
  representation and the explicit list form of qualified names.
wenzelm@30296
   816
haftmann@33174
   817
  \item \verb|Name_Space.naming| represents the abstract concept of
wenzelm@30296
   818
  a naming policy.
wenzelm@30296
   819
haftmann@33174
   820
  \item \verb|Name_Space.default_naming| is the default naming policy.
wenzelm@30296
   821
  In a theory context, this is usually augmented by a path prefix
wenzelm@30296
   822
  consisting of the theory name.
wenzelm@30296
   823
haftmann@33174
   824
  \item \verb|Name_Space.add_path|~\isa{path\ naming} augments the
wenzelm@30296
   825
  naming policy by extending its path component.
wenzelm@30296
   826
haftmann@33174
   827
  \item \verb|Name_Space.full_name|~\isa{naming\ binding} turns a
wenzelm@30296
   828
  name binding (usually a basic name) into the fully qualified
wenzelm@30296
   829
  internal name, according to the given naming policy.
wenzelm@30296
   830
haftmann@33174
   831
  \item \verb|Name_Space.T| represents name spaces.
wenzelm@30296
   832
haftmann@33174
   833
  \item \verb|Name_Space.empty|~\isa{kind} and \verb|Name_Space.merge|~\isa{{\isacharparenleft}space\isactrlisub {\isadigit{1}}{\isacharcomma}\ space\isactrlisub {\isadigit{2}}{\isacharparenright}} are the canonical operations for
wenzelm@30296
   834
  maintaining name spaces according to theory data management
haftmann@33174
   835
  (\secref{sec:context-data}); \isa{kind} is a formal comment
haftmann@33174
   836
  to characterize the purpose of a name space.
wenzelm@30296
   837
haftmann@33174
   838
  \item \verb|Name_Space.declare|~\isa{strict\ naming\ bindings\ space} enters a name binding as fully qualified internal name into
haftmann@33174
   839
  the name space, with external accesses determined by the naming
haftmann@33174
   840
  policy.
wenzelm@30296
   841
haftmann@33174
   842
  \item \verb|Name_Space.intern|~\isa{space\ name} internalizes a
wenzelm@30296
   843
  (partially qualified) external name.
wenzelm@30296
   844
wenzelm@30296
   845
  This operation is mostly for parsing!  Note that fully qualified
haftmann@33174
   846
  names stemming from declarations are produced via \verb|Name_Space.full_name| and \verb|Name_Space.declare|
wenzelm@30296
   847
  (or their derivatives for \verb|theory| and
wenzelm@30296
   848
  \verb|Proof.context|).
wenzelm@30296
   849
haftmann@33174
   850
  \item \verb|Name_Space.extern|~\isa{space\ name} externalizes a
wenzelm@30296
   851
  (fully qualified) internal name.
wenzelm@30296
   852
wenzelm@30296
   853
  This operation is mostly for printing!  User code should not rely on
wenzelm@30296
   854
  the precise result too much.
wenzelm@30296
   855
wenzelm@30296
   856
  \end{description}%
wenzelm@30296
   857
\end{isamarkuptext}%
wenzelm@30296
   858
\isamarkuptrue%
wenzelm@30296
   859
%
wenzelm@30296
   860
\endisatagmlref
wenzelm@30296
   861
{\isafoldmlref}%
wenzelm@30296
   862
%
wenzelm@30296
   863
\isadelimmlref
wenzelm@30296
   864
%
wenzelm@30296
   865
\endisadelimmlref
wenzelm@30296
   866
%
wenzelm@30296
   867
\isadelimtheory
wenzelm@30296
   868
%
wenzelm@30296
   869
\endisadelimtheory
wenzelm@30296
   870
%
wenzelm@30296
   871
\isatagtheory
wenzelm@30296
   872
\isacommand{end}\isamarkupfalse%
wenzelm@30296
   873
%
wenzelm@30296
   874
\endisatagtheory
wenzelm@30296
   875
{\isafoldtheory}%
wenzelm@30296
   876
%
wenzelm@30296
   877
\isadelimtheory
wenzelm@30296
   878
%
wenzelm@30296
   879
\endisadelimtheory
wenzelm@30296
   880
\isanewline
wenzelm@30296
   881
\end{isabellebody}%
wenzelm@30296
   882
%%% Local Variables:
wenzelm@30296
   883
%%% mode: latex
wenzelm@30296
   884
%%% TeX-master: "root"
wenzelm@30296
   885
%%% End: