doc-src/IsarImplementation/Thy/Prelim.thy
author wenzelm
Mon Oct 18 16:23:55 2010 +0100 (2010-10-18)
changeset 39865 a724b90f951e
parent 39864 f3b4fde34cd1
child 39866 5ec01d5acd0c
permissions -rw-r--r--
more on "Configuration options";
wenzelm@29755
     1
theory Prelim
wenzelm@29755
     2
imports Base
wenzelm@29755
     3
begin
wenzelm@18537
     4
wenzelm@18537
     5
chapter {* Preliminaries *}
wenzelm@18537
     6
wenzelm@20429
     7
section {* Contexts \label{sec:context} *}
wenzelm@18537
     8
wenzelm@20429
     9
text {*
wenzelm@20451
    10
  A logical context represents the background that is required for
wenzelm@20451
    11
  formulating statements and composing proofs.  It acts as a medium to
wenzelm@20451
    12
  produce formal content, depending on earlier material (declarations,
wenzelm@20451
    13
  results etc.).
wenzelm@18537
    14
wenzelm@20451
    15
  For example, derivations within the Isabelle/Pure logic can be
wenzelm@20451
    16
  described as a judgment @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<phi>"}, which means that a
wenzelm@20429
    17
  proposition @{text "\<phi>"} is derivable from hypotheses @{text "\<Gamma>"}
wenzelm@20429
    18
  within the theory @{text "\<Theta>"}.  There are logical reasons for
wenzelm@20451
    19
  keeping @{text "\<Theta>"} and @{text "\<Gamma>"} separate: theories can be
wenzelm@20451
    20
  liberal about supporting type constructors and schematic
wenzelm@20451
    21
  polymorphism of constants and axioms, while the inner calculus of
wenzelm@20451
    22
  @{text "\<Gamma> \<turnstile> \<phi>"} is strictly limited to Simple Type Theory (with
wenzelm@20451
    23
  fixed type variables in the assumptions).
wenzelm@18537
    24
wenzelm@20429
    25
  \medskip Contexts and derivations are linked by the following key
wenzelm@20429
    26
  principles:
wenzelm@20429
    27
wenzelm@20429
    28
  \begin{itemize}
wenzelm@20429
    29
wenzelm@20429
    30
  \item Transfer: monotonicity of derivations admits results to be
wenzelm@20451
    31
  transferred into a \emph{larger} context, i.e.\ @{text "\<Gamma> \<turnstile>\<^sub>\<Theta>
wenzelm@20451
    32
  \<phi>"} implies @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta>\<^sub>' \<phi>"} for contexts @{text "\<Theta>'
wenzelm@20451
    33
  \<supseteq> \<Theta>"} and @{text "\<Gamma>' \<supseteq> \<Gamma>"}.
wenzelm@18537
    34
wenzelm@20429
    35
  \item Export: discharge of hypotheses admits results to be exported
wenzelm@20451
    36
  into a \emph{smaller} context, i.e.\ @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta> \<phi>"}
wenzelm@20451
    37
  implies @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<Delta> \<Longrightarrow> \<phi>"} where @{text "\<Gamma>' \<supseteq> \<Gamma>"} and
wenzelm@20451
    38
  @{text "\<Delta> = \<Gamma>' - \<Gamma>"}.  Note that @{text "\<Theta>"} remains unchanged here,
wenzelm@20451
    39
  only the @{text "\<Gamma>"} part is affected.
wenzelm@20429
    40
wenzelm@20429
    41
  \end{itemize}
wenzelm@18537
    42
wenzelm@20451
    43
  \medskip By modeling the main characteristics of the primitive
wenzelm@20451
    44
  @{text "\<Theta>"} and @{text "\<Gamma>"} above, and abstracting over any
wenzelm@20451
    45
  particular logical content, we arrive at the fundamental notions of
wenzelm@20451
    46
  \emph{theory context} and \emph{proof context} in Isabelle/Isar.
wenzelm@20451
    47
  These implement a certain policy to manage arbitrary \emph{context
wenzelm@20451
    48
  data}.  There is a strongly-typed mechanism to declare new kinds of
wenzelm@20429
    49
  data at compile time.
wenzelm@18537
    50
wenzelm@20451
    51
  The internal bootstrap process of Isabelle/Pure eventually reaches a
wenzelm@20451
    52
  stage where certain data slots provide the logical content of @{text
wenzelm@20451
    53
  "\<Theta>"} and @{text "\<Gamma>"} sketched above, but this does not stop there!
wenzelm@20451
    54
  Various additional data slots support all kinds of mechanisms that
wenzelm@20451
    55
  are not necessarily part of the core logic.
wenzelm@18537
    56
wenzelm@20429
    57
  For example, there would be data for canonical introduction and
wenzelm@20429
    58
  elimination rules for arbitrary operators (depending on the
wenzelm@20429
    59
  object-logic and application), which enables users to perform
wenzelm@20451
    60
  standard proof steps implicitly (cf.\ the @{text "rule"} method
wenzelm@20451
    61
  \cite{isabelle-isar-ref}).
wenzelm@18537
    62
wenzelm@20451
    63
  \medskip Thus Isabelle/Isar is able to bring forth more and more
wenzelm@20451
    64
  concepts successively.  In particular, an object-logic like
wenzelm@20451
    65
  Isabelle/HOL continues the Isabelle/Pure setup by adding specific
wenzelm@20451
    66
  components for automated reasoning (classical reasoner, tableau
wenzelm@20451
    67
  prover, structured induction etc.) and derived specification
wenzelm@20451
    68
  mechanisms (inductive predicates, recursive functions etc.).  All of
wenzelm@20451
    69
  this is ultimately based on the generic data management by theory
wenzelm@20451
    70
  and proof contexts introduced here.
wenzelm@18537
    71
*}
wenzelm@18537
    72
wenzelm@18537
    73
wenzelm@18537
    74
subsection {* Theory context \label{sec:context-theory} *}
wenzelm@18537
    75
wenzelm@34921
    76
text {* A \emph{theory} is a data container with explicit name and
wenzelm@34921
    77
  unique identifier.  Theories are related by a (nominal) sub-theory
wenzelm@20451
    78
  relation, which corresponds to the dependency graph of the original
wenzelm@20451
    79
  construction; each theory is derived from a certain sub-graph of
wenzelm@34921
    80
  ancestor theories.  To this end, the system maintains a set of
wenzelm@34921
    81
  symbolic ``identification stamps'' within each theory.
wenzelm@18537
    82
wenzelm@34921
    83
  In order to avoid the full-scale overhead of explicit sub-theory
wenzelm@34921
    84
  identification of arbitrary intermediate stages, a theory is
wenzelm@34921
    85
  switched into @{text "draft"} mode under certain circumstances.  A
wenzelm@34921
    86
  draft theory acts like a linear type, where updates invalidate
wenzelm@34921
    87
  earlier versions.  An invalidated draft is called \emph{stale}.
wenzelm@20429
    88
wenzelm@34921
    89
  The @{text "checkpoint"} operation produces a safe stepping stone
wenzelm@34921
    90
  that will survive the next update without becoming stale: both the
wenzelm@34921
    91
  old and the new theory remain valid and are related by the
wenzelm@34921
    92
  sub-theory relation.  Checkpointing essentially recovers purely
wenzelm@34921
    93
  functional theory values, at the expense of some extra internal
wenzelm@34921
    94
  bookkeeping.
wenzelm@20447
    95
wenzelm@20447
    96
  The @{text "copy"} operation produces an auxiliary version that has
wenzelm@20447
    97
  the same data content, but is unrelated to the original: updates of
wenzelm@20447
    98
  the copy do not affect the original, neither does the sub-theory
wenzelm@20447
    99
  relation hold.
wenzelm@20429
   100
wenzelm@34921
   101
  The @{text "merge"} operation produces the least upper bound of two
wenzelm@34921
   102
  theories, which actually degenerates into absorption of one theory
wenzelm@34921
   103
  into the other (according to the nominal sub-theory relation).
wenzelm@34921
   104
wenzelm@34921
   105
  The @{text "begin"} operation starts a new theory by importing
wenzelm@34921
   106
  several parent theories and entering a special mode of nameless
wenzelm@34921
   107
  incremental updates, until the final @{text "end"} operation is
wenzelm@34921
   108
  performed.
wenzelm@34921
   109
wenzelm@20447
   110
  \medskip The example in \figref{fig:ex-theory} below shows a theory
wenzelm@20451
   111
  graph derived from @{text "Pure"}, with theory @{text "Length"}
wenzelm@20451
   112
  importing @{text "Nat"} and @{text "List"}.  The body of @{text
wenzelm@20451
   113
  "Length"} consists of a sequence of updates, working mostly on
wenzelm@34921
   114
  drafts internally, while transaction boundaries of Isar top-level
wenzelm@34921
   115
  commands (\secref{sec:isar-toplevel}) are guaranteed to be safe
wenzelm@34921
   116
  checkpoints.
wenzelm@20447
   117
wenzelm@20447
   118
  \begin{figure}[htb]
wenzelm@20447
   119
  \begin{center}
wenzelm@20429
   120
  \begin{tabular}{rcccl}
wenzelm@20447
   121
        &            & @{text "Pure"} \\
wenzelm@20447
   122
        &            & @{text "\<down>"} \\
wenzelm@20447
   123
        &            & @{text "FOL"} \\
wenzelm@18537
   124
        & $\swarrow$ &              & $\searrow$ & \\
wenzelm@21852
   125
  @{text "Nat"} &    &              &            & @{text "List"} \\
wenzelm@18537
   126
        & $\searrow$ &              & $\swarrow$ \\
wenzelm@20447
   127
        &            & @{text "Length"} \\
wenzelm@26864
   128
        &            & \multicolumn{3}{l}{~~@{keyword "imports"}} \\
wenzelm@26864
   129
        &            & \multicolumn{3}{l}{~~@{keyword "begin"}} \\
wenzelm@18537
   130
        &            & $\vdots$~~ \\
wenzelm@20447
   131
        &            & @{text "\<bullet>"}~~ \\
wenzelm@20447
   132
        &            & $\vdots$~~ \\
wenzelm@20447
   133
        &            & @{text "\<bullet>"}~~ \\
wenzelm@20447
   134
        &            & $\vdots$~~ \\
wenzelm@26864
   135
        &            & \multicolumn{3}{l}{~~@{command "end"}} \\
wenzelm@20429
   136
  \end{tabular}
wenzelm@20451
   137
  \caption{A theory definition depending on ancestors}\label{fig:ex-theory}
wenzelm@20447
   138
  \end{center}
wenzelm@20447
   139
  \end{figure}
wenzelm@20451
   140
wenzelm@20451
   141
  \medskip There is a separate notion of \emph{theory reference} for
wenzelm@20451
   142
  maintaining a live link to an evolving theory context: updates on
wenzelm@39821
   143
  drafts are propagated automatically.  Dynamic updating stops when
wenzelm@39821
   144
  the next @{text "checkpoint"} is reached.
wenzelm@20451
   145
wenzelm@20451
   146
  Derived entities may store a theory reference in order to indicate
wenzelm@39821
   147
  the formal context from which they are derived.  This implicitly
wenzelm@39821
   148
  assumes monotonic reasoning, because the referenced context may
wenzelm@39821
   149
  become larger without further notice.
wenzelm@18537
   150
*}
wenzelm@18537
   151
wenzelm@20430
   152
text %mlref {*
wenzelm@20447
   153
  \begin{mldecls}
wenzelm@20447
   154
  @{index_ML_type theory} \\
wenzelm@39837
   155
  @{index_ML Theory.eq_thy: "theory * theory -> bool"} \\
wenzelm@20447
   156
  @{index_ML Theory.subthy: "theory * theory -> bool"} \\
wenzelm@20447
   157
  @{index_ML Theory.checkpoint: "theory -> theory"} \\
wenzelm@20547
   158
  @{index_ML Theory.copy: "theory -> theory"} \\
wenzelm@34921
   159
  @{index_ML Theory.merge: "theory * theory -> theory"} \\
wenzelm@34921
   160
  @{index_ML Theory.begin_theory: "string -> theory list -> theory"} \\
wenzelm@39837
   161
  @{index_ML Theory.parents_of: "theory -> theory list"} \\
wenzelm@39837
   162
  @{index_ML Theory.ancestors_of: "theory -> theory list"} \\
wenzelm@20547
   163
  \end{mldecls}
wenzelm@20547
   164
  \begin{mldecls}
wenzelm@20447
   165
  @{index_ML_type theory_ref} \\
wenzelm@20447
   166
  @{index_ML Theory.deref: "theory_ref -> theory"} \\
wenzelm@24137
   167
  @{index_ML Theory.check_thy: "theory -> theory_ref"} \\
wenzelm@20447
   168
  \end{mldecls}
wenzelm@20447
   169
wenzelm@20447
   170
  \begin{description}
wenzelm@20447
   171
wenzelm@39864
   172
  \item Type @{ML_type theory} represents theory contexts.  This is
wenzelm@39821
   173
  essentially a linear type, with explicit runtime checking.
wenzelm@39821
   174
  Primitive theory operations destroy the original version, which then
wenzelm@39821
   175
  becomes ``stale''.  This can be prevented by explicit checkpointing,
wenzelm@39821
   176
  which the system does at least at the boundary of toplevel command
wenzelm@39821
   177
  transactions \secref{sec:isar-toplevel}.
wenzelm@20447
   178
wenzelm@39837
   179
  \item @{ML "Theory.eq_thy"}~@{text "(thy\<^sub>1, thy\<^sub>2)"} check strict
wenzelm@39837
   180
  identity of two theories.
wenzelm@39837
   181
wenzelm@34921
   182
  \item @{ML "Theory.subthy"}~@{text "(thy\<^sub>1, thy\<^sub>2)"} compares theories
wenzelm@34921
   183
  according to the intrinsic graph structure of the construction.
wenzelm@34921
   184
  This sub-theory relation is a nominal approximation of inclusion
wenzelm@34921
   185
  (@{text "\<subseteq>"}) of the corresponding content (according to the
wenzelm@34921
   186
  semantics of the ML modules that implement the data).
wenzelm@20447
   187
wenzelm@20447
   188
  \item @{ML "Theory.checkpoint"}~@{text "thy"} produces a safe
wenzelm@34921
   189
  stepping stone in the linear development of @{text "thy"}.  This
wenzelm@34921
   190
  changes the old theory, but the next update will result in two
wenzelm@34921
   191
  related, valid theories.
wenzelm@20447
   192
wenzelm@20447
   193
  \item @{ML "Theory.copy"}~@{text "thy"} produces a variant of @{text
wenzelm@34921
   194
  "thy"} with the same data.  The copy is not related to the original,
wenzelm@34921
   195
  but the original is unchanged.
wenzelm@34921
   196
wenzelm@34921
   197
  \item @{ML "Theory.merge"}~@{text "(thy\<^sub>1, thy\<^sub>2)"} absorbs one theory
wenzelm@34921
   198
  into the other, without changing @{text "thy\<^sub>1"} or @{text "thy\<^sub>2"}.
wenzelm@34921
   199
  This version of ad-hoc theory merge fails for unrelated theories!
wenzelm@34921
   200
wenzelm@34921
   201
  \item @{ML "Theory.begin_theory"}~@{text "name parents"} constructs
wenzelm@39825
   202
  a new theory based on the given parents.  This ML function is
wenzelm@34921
   203
  normally not invoked directly.
wenzelm@20447
   204
wenzelm@39837
   205
  \item @{ML "Theory.parents_of"}~@{text "thy"} returns the direct
wenzelm@39837
   206
  ancestors of @{text thy}.
wenzelm@39837
   207
wenzelm@39837
   208
  \item @{ML "Theory.ancestors_of"}~@{text "thy"} returns all
wenzelm@39837
   209
  ancestors of @{text thy} (not including @{text thy} itself).
wenzelm@39837
   210
wenzelm@39864
   211
  \item Type @{ML_type theory_ref} represents a sliding reference to
wenzelm@39864
   212
  an always valid theory; updates on the original are propagated
wenzelm@20447
   213
  automatically.
wenzelm@20447
   214
wenzelm@24137
   215
  \item @{ML "Theory.deref"}~@{text "thy_ref"} turns a @{ML_type
wenzelm@24137
   216
  "theory_ref"} into an @{ML_type "theory"} value.  As the referenced
wenzelm@24137
   217
  theory evolves monotonically over time, later invocations of @{ML
wenzelm@20451
   218
  "Theory.deref"} may refer to a larger context.
wenzelm@20447
   219
wenzelm@24137
   220
  \item @{ML "Theory.check_thy"}~@{text "thy"} produces a @{ML_type
wenzelm@24137
   221
  "theory_ref"} from a valid @{ML_type "theory"} value.
wenzelm@24137
   222
wenzelm@20447
   223
  \end{description}
wenzelm@20430
   224
*}
wenzelm@20430
   225
wenzelm@39832
   226
text %mlantiq {*
wenzelm@39832
   227
  \begin{matharray}{rcl}
wenzelm@39832
   228
  @{ML_antiquotation_def "theory"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   229
  @{ML_antiquotation_def "theory_ref"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   230
  \end{matharray}
wenzelm@39832
   231
wenzelm@39832
   232
  \begin{rail}
wenzelm@39832
   233
  ('theory' | 'theory\_ref') nameref?
wenzelm@39832
   234
  ;
wenzelm@39832
   235
  \end{rail}
wenzelm@39832
   236
wenzelm@39832
   237
  \begin{description}
wenzelm@39832
   238
wenzelm@39832
   239
  \item @{text "@{theory}"} refers to the background theory of the
wenzelm@39832
   240
  current context --- as abstract value.
wenzelm@39832
   241
wenzelm@39832
   242
  \item @{text "@{theory A}"} refers to an explicitly named ancestor
wenzelm@39832
   243
  theory @{text "A"} of the background theory of the current context
wenzelm@39832
   244
  --- as abstract value.
wenzelm@39832
   245
wenzelm@39832
   246
  \item @{text "@{theory_ref}"} is similar to @{text "@{theory}"}, but
wenzelm@39832
   247
  produces a @{ML_type theory_ref} via @{ML "Theory.check_thy"} as
wenzelm@39832
   248
  explained above.
wenzelm@39832
   249
wenzelm@39832
   250
  \end{description}
wenzelm@39832
   251
*}
wenzelm@39832
   252
wenzelm@18537
   253
wenzelm@18537
   254
subsection {* Proof context \label{sec:context-proof} *}
wenzelm@18537
   255
wenzelm@34921
   256
text {* A proof context is a container for pure data with a
wenzelm@39821
   257
  back-reference to the theory from which it is derived.  The @{text
wenzelm@39821
   258
  "init"} operation creates a proof context from a given theory.
wenzelm@34921
   259
  Modifications to draft theories are propagated to the proof context
wenzelm@34921
   260
  as usual, but there is also an explicit @{text "transfer"} operation
wenzelm@34921
   261
  to force resynchronization with more substantial updates to the
wenzelm@34921
   262
  underlying theory.
wenzelm@20429
   263
wenzelm@34921
   264
  Entities derived in a proof context need to record logical
wenzelm@20447
   265
  requirements explicitly, since there is no separate context
wenzelm@34921
   266
  identification or symbolic inclusion as for theories.  For example,
wenzelm@34921
   267
  hypotheses used in primitive derivations (cf.\ \secref{sec:thms})
wenzelm@34921
   268
  are recorded separately within the sequent @{text "\<Gamma> \<turnstile> \<phi>"}, just to
wenzelm@34921
   269
  make double sure.  Results could still leak into an alien proof
wenzelm@34921
   270
  context due to programming errors, but Isabelle/Isar includes some
wenzelm@34921
   271
  extra validity checks in critical positions, notably at the end of a
wenzelm@34921
   272
  sub-proof.
wenzelm@20429
   273
wenzelm@20451
   274
  Proof contexts may be manipulated arbitrarily, although the common
wenzelm@20451
   275
  discipline is to follow block structure as a mental model: a given
wenzelm@20451
   276
  context is extended consecutively, and results are exported back
wenzelm@34921
   277
  into the original context.  Note that an Isar proof state models
wenzelm@20451
   278
  block-structured reasoning explicitly, using a stack of proof
wenzelm@34921
   279
  contexts internally.  For various technical reasons, the background
wenzelm@34921
   280
  theory of an Isar proof state must not be changed while the proof is
wenzelm@34921
   281
  still under construction!
wenzelm@18537
   282
*}
wenzelm@18537
   283
wenzelm@20449
   284
text %mlref {*
wenzelm@20449
   285
  \begin{mldecls}
wenzelm@20449
   286
  @{index_ML_type Proof.context} \\
wenzelm@36611
   287
  @{index_ML ProofContext.init_global: "theory -> Proof.context"} \\
wenzelm@20449
   288
  @{index_ML ProofContext.theory_of: "Proof.context -> theory"} \\
wenzelm@20449
   289
  @{index_ML ProofContext.transfer: "theory -> Proof.context -> Proof.context"} \\
wenzelm@20449
   290
  \end{mldecls}
wenzelm@20449
   291
wenzelm@20449
   292
  \begin{description}
wenzelm@20449
   293
wenzelm@39864
   294
  \item Type @{ML_type Proof.context} represents proof contexts.
wenzelm@39864
   295
  Elements of this type are essentially pure values, with a sliding
wenzelm@39864
   296
  reference to the background theory.
wenzelm@20449
   297
wenzelm@36611
   298
  \item @{ML ProofContext.init_global}~@{text "thy"} produces a proof context
wenzelm@20449
   299
  derived from @{text "thy"}, initializing all data.
wenzelm@20449
   300
wenzelm@20449
   301
  \item @{ML ProofContext.theory_of}~@{text "ctxt"} selects the
wenzelm@20451
   302
  background theory from @{text "ctxt"}, dereferencing its internal
wenzelm@20451
   303
  @{ML_type theory_ref}.
wenzelm@20449
   304
wenzelm@20449
   305
  \item @{ML ProofContext.transfer}~@{text "thy ctxt"} promotes the
wenzelm@20449
   306
  background theory of @{text "ctxt"} to the super theory @{text
wenzelm@20449
   307
  "thy"}.
wenzelm@20449
   308
wenzelm@20449
   309
  \end{description}
wenzelm@20449
   310
*}
wenzelm@20449
   311
wenzelm@39832
   312
text %mlantiq {*
wenzelm@39832
   313
  \begin{matharray}{rcl}
wenzelm@39832
   314
  @{ML_antiquotation_def "context"} & : & @{text ML_antiquotation} \\
wenzelm@39832
   315
  \end{matharray}
wenzelm@39832
   316
wenzelm@39832
   317
  \begin{description}
wenzelm@39832
   318
wenzelm@39832
   319
  \item @{text "@{context}"} refers to \emph{the} context at
wenzelm@39832
   320
  compile-time --- as abstract value.  Independently of (local) theory
wenzelm@39832
   321
  or proof mode, this always produces a meaningful result.
wenzelm@39832
   322
wenzelm@39832
   323
  This is probably the most common antiquotation in interactive
wenzelm@39832
   324
  experimentation with ML inside Isar.
wenzelm@39832
   325
wenzelm@39832
   326
  \end{description}
wenzelm@39832
   327
*}
wenzelm@39832
   328
wenzelm@20430
   329
wenzelm@20451
   330
subsection {* Generic contexts \label{sec:generic-context} *}
wenzelm@20429
   331
wenzelm@20449
   332
text {*
wenzelm@20449
   333
  A generic context is the disjoint sum of either a theory or proof
wenzelm@20451
   334
  context.  Occasionally, this enables uniform treatment of generic
wenzelm@20450
   335
  context data, typically extra-logical information.  Operations on
wenzelm@20449
   336
  generic contexts include the usual injections, partial selections,
wenzelm@20449
   337
  and combinators for lifting operations on either component of the
wenzelm@20449
   338
  disjoint sum.
wenzelm@20449
   339
wenzelm@20449
   340
  Moreover, there are total operations @{text "theory_of"} and @{text
wenzelm@20449
   341
  "proof_of"} to convert a generic context into either kind: a theory
wenzelm@20451
   342
  can always be selected from the sum, while a proof context might
wenzelm@34921
   343
  have to be constructed by an ad-hoc @{text "init"} operation, which
wenzelm@34921
   344
  incurs a small runtime overhead.
wenzelm@20449
   345
*}
wenzelm@20430
   346
wenzelm@20449
   347
text %mlref {*
wenzelm@20449
   348
  \begin{mldecls}
wenzelm@20449
   349
  @{index_ML_type Context.generic} \\
wenzelm@20449
   350
  @{index_ML Context.theory_of: "Context.generic -> theory"} \\
wenzelm@20449
   351
  @{index_ML Context.proof_of: "Context.generic -> Proof.context"} \\
wenzelm@20449
   352
  \end{mldecls}
wenzelm@20449
   353
wenzelm@20449
   354
  \begin{description}
wenzelm@20430
   355
wenzelm@39864
   356
  \item Type @{ML_type Context.generic} is the direct sum of @{ML_type
wenzelm@20451
   357
  "theory"} and @{ML_type "Proof.context"}, with the datatype
wenzelm@20451
   358
  constructors @{ML "Context.Theory"} and @{ML "Context.Proof"}.
wenzelm@20449
   359
wenzelm@20449
   360
  \item @{ML Context.theory_of}~@{text "context"} always produces a
wenzelm@20449
   361
  theory from the generic @{text "context"}, using @{ML
wenzelm@20449
   362
  "ProofContext.theory_of"} as required.
wenzelm@20449
   363
wenzelm@20449
   364
  \item @{ML Context.proof_of}~@{text "context"} always produces a
wenzelm@20449
   365
  proof context from the generic @{text "context"}, using @{ML
wenzelm@36611
   366
  "ProofContext.init_global"} as required (note that this re-initializes the
wenzelm@20451
   367
  context data with each invocation).
wenzelm@20449
   368
wenzelm@20449
   369
  \end{description}
wenzelm@20449
   370
*}
wenzelm@20437
   371
wenzelm@20476
   372
wenzelm@20476
   373
subsection {* Context data \label{sec:context-data} *}
wenzelm@20447
   374
wenzelm@33524
   375
text {* The main purpose of theory and proof contexts is to manage
wenzelm@33524
   376
  arbitrary (pure) data.  New data types can be declared incrementally
wenzelm@33524
   377
  at compile time.  There are separate declaration mechanisms for any
wenzelm@33524
   378
  of the three kinds of contexts: theory, proof, generic.
wenzelm@20449
   379
wenzelm@33524
   380
  \paragraph{Theory data} declarations need to implement the following
wenzelm@33524
   381
  SML signature:
wenzelm@20449
   382
wenzelm@20449
   383
  \medskip
wenzelm@20449
   384
  \begin{tabular}{ll}
wenzelm@22869
   385
  @{text "\<type> T"} & representing type \\
wenzelm@22869
   386
  @{text "\<val> empty: T"} & empty default value \\
wenzelm@22869
   387
  @{text "\<val> extend: T \<rightarrow> T"} & re-initialize on import \\
wenzelm@22869
   388
  @{text "\<val> merge: T \<times> T \<rightarrow> T"} & join on import \\
wenzelm@20449
   389
  \end{tabular}
wenzelm@20449
   390
  \medskip
wenzelm@20449
   391
wenzelm@39861
   392
  The @{text "empty"} value acts as initial default for \emph{any}
wenzelm@39861
   393
  theory that does not declare actual data content; @{text "extend"}
wenzelm@39861
   394
  is acts like a unitary version of @{text "merge"}.
wenzelm@20449
   395
wenzelm@34921
   396
  Implementing @{text "merge"} can be tricky.  The general idea is
wenzelm@34921
   397
  that @{text "merge (data\<^sub>1, data\<^sub>2)"} inserts those parts of @{text
wenzelm@34921
   398
  "data\<^sub>2"} into @{text "data\<^sub>1"} that are not yet present, while
wenzelm@34921
   399
  keeping the general order of things.  The @{ML Library.merge}
wenzelm@34921
   400
  function on plain lists may serve as canonical template.
wenzelm@34921
   401
wenzelm@34921
   402
  Particularly note that shared parts of the data must not be
wenzelm@34921
   403
  duplicated by naive concatenation, or a theory graph that is like a
wenzelm@34921
   404
  chain of diamonds would cause an exponential blowup!
wenzelm@34921
   405
wenzelm@33524
   406
  \paragraph{Proof context data} declarations need to implement the
wenzelm@33524
   407
  following SML signature:
wenzelm@20449
   408
wenzelm@20449
   409
  \medskip
wenzelm@20449
   410
  \begin{tabular}{ll}
wenzelm@22869
   411
  @{text "\<type> T"} & representing type \\
wenzelm@22869
   412
  @{text "\<val> init: theory \<rightarrow> T"} & produce initial value \\
wenzelm@20449
   413
  \end{tabular}
wenzelm@20449
   414
  \medskip
wenzelm@20449
   415
wenzelm@39861
   416
  The @{text "init"} operation is supposed to produce a pure value
wenzelm@39861
   417
  from the given background theory and should be somehow
wenzelm@34921
   418
  ``immediate''.  Whenever a proof context is initialized, which
wenzelm@34921
   419
  happens frequently, the the system invokes the @{text "init"}
wenzelm@39821
   420
  operation of \emph{all} theory data slots ever declared.  This also
wenzelm@39821
   421
  means that one needs to be economic about the total number of proof
wenzelm@39821
   422
  data declarations in the system, i.e.\ each ML module should declare
wenzelm@39821
   423
  at most one, sometimes two data slots for its internal use.
wenzelm@39821
   424
  Repeated data declarations to simulate a record type should be
wenzelm@39821
   425
  avoided!
wenzelm@20449
   426
wenzelm@20451
   427
  \paragraph{Generic data} provides a hybrid interface for both theory
wenzelm@33524
   428
  and proof data.  The @{text "init"} operation for proof contexts is
wenzelm@33524
   429
  predefined to select the current data value from the background
wenzelm@33524
   430
  theory.
wenzelm@20449
   431
wenzelm@39821
   432
  \bigskip Any of the above data declarations over type @{text "T"}
wenzelm@39821
   433
  result in an ML structure with the following signature:
wenzelm@20449
   434
wenzelm@20449
   435
  \medskip
wenzelm@20449
   436
  \begin{tabular}{ll}
wenzelm@20449
   437
  @{text "get: context \<rightarrow> T"} \\
wenzelm@20449
   438
  @{text "put: T \<rightarrow> context \<rightarrow> context"} \\
wenzelm@20449
   439
  @{text "map: (T \<rightarrow> T) \<rightarrow> context \<rightarrow> context"} \\
wenzelm@20449
   440
  \end{tabular}
wenzelm@20449
   441
  \medskip
wenzelm@20449
   442
wenzelm@39861
   443
  These other operations provide exclusive access for the particular
wenzelm@39861
   444
  kind of context (theory, proof, or generic context).  This interface
wenzelm@39861
   445
  observes the ML discipline for types and scopes: there is no other
wenzelm@39861
   446
  way to access the corresponding data slot of a context.  By keeping
wenzelm@39861
   447
  these operations private, an Isabelle/ML module may maintain
wenzelm@39861
   448
  abstract values authentically.  *}
wenzelm@20447
   449
wenzelm@20450
   450
text %mlref {*
wenzelm@20450
   451
  \begin{mldecls}
wenzelm@33524
   452
  @{index_ML_functor Theory_Data} \\
wenzelm@33524
   453
  @{index_ML_functor Proof_Data} \\
wenzelm@33524
   454
  @{index_ML_functor Generic_Data} \\
wenzelm@20450
   455
  \end{mldecls}
wenzelm@20450
   456
wenzelm@20450
   457
  \begin{description}
wenzelm@20450
   458
wenzelm@33524
   459
  \item @{ML_functor Theory_Data}@{text "(spec)"} declares data for
wenzelm@20450
   460
  type @{ML_type theory} according to the specification provided as
wenzelm@20451
   461
  argument structure.  The resulting structure provides data init and
wenzelm@20451
   462
  access operations as described above.
wenzelm@20450
   463
wenzelm@33524
   464
  \item @{ML_functor Proof_Data}@{text "(spec)"} is analogous to
wenzelm@33524
   465
  @{ML_functor Theory_Data} for type @{ML_type Proof.context}.
wenzelm@20450
   466
wenzelm@33524
   467
  \item @{ML_functor Generic_Data}@{text "(spec)"} is analogous to
wenzelm@33524
   468
  @{ML_functor Theory_Data} for type @{ML_type Context.generic}.
wenzelm@20450
   469
wenzelm@20450
   470
  \end{description}
wenzelm@20450
   471
*}
wenzelm@20450
   472
wenzelm@34928
   473
text %mlex {*
wenzelm@34928
   474
  The following artificial example demonstrates theory
wenzelm@34928
   475
  data: we maintain a set of terms that are supposed to be wellformed
wenzelm@34928
   476
  wrt.\ the enclosing theory.  The public interface is as follows:
wenzelm@34928
   477
*}
wenzelm@34928
   478
wenzelm@34928
   479
ML {*
wenzelm@34928
   480
  signature WELLFORMED_TERMS =
wenzelm@34928
   481
  sig
wenzelm@34928
   482
    val get: theory -> term list
wenzelm@34928
   483
    val add: term -> theory -> theory
wenzelm@34928
   484
  end;
wenzelm@34928
   485
*}
wenzelm@34928
   486
wenzelm@39861
   487
text {* The implementation uses private theory data internally, and
wenzelm@39861
   488
  only exposes an operation that involves explicit argument checking
wenzelm@39861
   489
  wrt.\ the given theory. *}
wenzelm@34928
   490
wenzelm@34928
   491
ML {*
wenzelm@34928
   492
  structure Wellformed_Terms: WELLFORMED_TERMS =
wenzelm@34928
   493
  struct
wenzelm@34928
   494
wenzelm@34928
   495
  structure Terms = Theory_Data
wenzelm@34928
   496
  (
wenzelm@39687
   497
    type T = term Ord_List.T;
wenzelm@34928
   498
    val empty = [];
wenzelm@34928
   499
    val extend = I;
wenzelm@34928
   500
    fun merge (ts1, ts2) =
wenzelm@39687
   501
      Ord_List.union Term_Ord.fast_term_ord ts1 ts2;
wenzelm@39861
   502
  );
wenzelm@34928
   503
wenzelm@34928
   504
  val get = Terms.get;
wenzelm@34928
   505
wenzelm@34928
   506
  fun add raw_t thy =
wenzelm@39821
   507
    let
wenzelm@39821
   508
      val t = Sign.cert_term thy raw_t;
wenzelm@39821
   509
    in
wenzelm@39821
   510
      Terms.map (Ord_List.insert Term_Ord.fast_term_ord t) thy
wenzelm@39821
   511
    end;
wenzelm@34928
   512
wenzelm@34928
   513
  end;
wenzelm@34928
   514
*}
wenzelm@34928
   515
wenzelm@39864
   516
text {* Type @{ML_type "term Ord_List.T"} is used for reasonably
wenzelm@39864
   517
  efficient representation of a set of terms: all operations are
wenzelm@39864
   518
  linear in the number of stored elements.  Here we assume that users
wenzelm@39864
   519
  of this module do not care about the declaration order, since that
wenzelm@39864
   520
  data structure forces its own arrangement of elements.
wenzelm@34928
   521
wenzelm@34928
   522
  Observe how the @{verbatim merge} operation joins the data slots of
wenzelm@39687
   523
  the two constituents: @{ML Ord_List.union} prevents duplication of
wenzelm@34928
   524
  common data from different branches, thus avoiding the danger of
wenzelm@39821
   525
  exponential blowup.  Plain list append etc.\ must never be used for
wenzelm@39821
   526
  theory data merges!
wenzelm@34928
   527
wenzelm@34928
   528
  \medskip Our intended invariant is achieved as follows:
wenzelm@34928
   529
  \begin{enumerate}
wenzelm@34928
   530
wenzelm@34928
   531
  \item @{ML Wellformed_Terms.add} only admits terms that have passed
wenzelm@34928
   532
  the @{ML Sign.cert_term} check of the given theory at that point.
wenzelm@34928
   533
wenzelm@34928
   534
  \item Wellformedness in the sense of @{ML Sign.cert_term} is
wenzelm@34928
   535
  monotonic wrt.\ the sub-theory relation.  So our data can move
wenzelm@34928
   536
  upwards in the hierarchy (via extension or merges), and maintain
wenzelm@34928
   537
  wellformedness without further checks.
wenzelm@34928
   538
wenzelm@34928
   539
  \end{enumerate}
wenzelm@34928
   540
wenzelm@34928
   541
  Note that all basic operations of the inference kernel (which
wenzelm@34928
   542
  includes @{ML Sign.cert_term}) observe this monotonicity principle,
wenzelm@34928
   543
  but other user-space tools don't.  For example, fully-featured
wenzelm@34928
   544
  type-inference via @{ML Syntax.check_term} (cf.\
wenzelm@34928
   545
  \secref{sec:term-check}) is not necessarily monotonic wrt.\ the
wenzelm@34928
   546
  background theory, since constraints of term constants can be
wenzelm@39821
   547
  modified by later declarations, for example.
wenzelm@34928
   548
wenzelm@34928
   549
  In most cases, user-space context data does not have to take such
wenzelm@34928
   550
  invariants too seriously.  The situation is different in the
wenzelm@34928
   551
  implementation of the inference kernel itself, which uses the very
wenzelm@34928
   552
  same data mechanisms for types, constants, axioms etc.
wenzelm@34928
   553
*}
wenzelm@34928
   554
wenzelm@20447
   555
wenzelm@39865
   556
subsection {* Configuration options \label{sec:config-options} *}
wenzelm@39865
   557
wenzelm@39865
   558
text {* A \emph{configuration option} is a named optional value of
wenzelm@39865
   559
  some basic type (Boolean, integer, string) that is stored in the
wenzelm@39865
   560
  context.  It is a simple application of general context data
wenzelm@39865
   561
  (\secref{sec:context-data}) that is sufficiently common to justify
wenzelm@39865
   562
  customized setup, which includes some concrete declarations for
wenzelm@39865
   563
  end-users using existing notation for attributes (cf.\
wenzelm@39865
   564
  \secref{sec:attributes}).
wenzelm@39865
   565
wenzelm@39865
   566
  For example, the predefined configuration option @{attribute
wenzelm@39865
   567
  show_types} controls output of explicit type constraints for
wenzelm@39865
   568
  variables in printed terms (cf.\ \secref{sec:parse-print}).  Its
wenzelm@39865
   569
  value can be modified within Isar text like this:
wenzelm@39865
   570
*}
wenzelm@39865
   571
wenzelm@39865
   572
declare [[show_types = false]]
wenzelm@39865
   573
  -- {* declaration within (local) theory context *}
wenzelm@39865
   574
wenzelm@39865
   575
example_proof
wenzelm@39865
   576
  note [[show_types = true]]
wenzelm@39865
   577
    -- {* declaration within proof (forward mode) *}
wenzelm@39865
   578
  term x
wenzelm@39865
   579
wenzelm@39865
   580
  have "x = x"
wenzelm@39865
   581
    using [[show_types = false]]
wenzelm@39865
   582
      -- {* declaration within proof (backward mode) *}
wenzelm@39865
   583
    ..
wenzelm@39865
   584
qed
wenzelm@39865
   585
wenzelm@39865
   586
text {* Configuration options that are not set explicitly hold a
wenzelm@39865
   587
  default value that can depend on the application context.  This
wenzelm@39865
   588
  allows to retrieve the value from another slot within the context,
wenzelm@39865
   589
  or fall back on a global preference mechanism, for example.
wenzelm@39865
   590
wenzelm@39865
   591
  The operations to declare configuration options and get/map their
wenzelm@39865
   592
  values are modeled as direct replacements for historic global
wenzelm@39865
   593
  references, only that the context is made explicit.  This allows
wenzelm@39865
   594
  easy configuration of tools, without relying on the execution order
wenzelm@39865
   595
  as required for old-style mutable references.  *}
wenzelm@39865
   596
wenzelm@39865
   597
text %mlref {*
wenzelm@39865
   598
  \begin{mldecls}
wenzelm@39865
   599
  @{index_ML Config.get: "Proof.context -> 'a Config.T -> 'a"} \\
wenzelm@39865
   600
  @{index_ML Config.map: "'a Config.T -> ('a -> 'a) -> Proof.context -> Proof.context"} \\
wenzelm@39865
   601
  @{index_ML Attrib.config_bool: "string -> (Context.generic -> bool) ->
wenzelm@39865
   602
  bool Config.T * (theory -> theory)"} \\
wenzelm@39865
   603
  @{index_ML Attrib.config_int: "string -> (Context.generic -> int) ->
wenzelm@39865
   604
  int Config.T * (theory -> theory)"} \\
wenzelm@39865
   605
  @{index_ML Attrib.config_string: "string -> (Context.generic -> string) ->
wenzelm@39865
   606
  string Config.T * (theory -> theory)"} \\
wenzelm@39865
   607
  \end{mldecls}
wenzelm@39865
   608
wenzelm@39865
   609
  \begin{description}
wenzelm@39865
   610
wenzelm@39865
   611
  \item @{ML Config.get}~@{text "ctxt config"} gets the value of
wenzelm@39865
   612
  @{text "config"} in the given context.
wenzelm@39865
   613
wenzelm@39865
   614
  \item @{ML Config.map}~@{text "config f ctxt"} updates the context
wenzelm@39865
   615
  by updating the value of @{text "config"}.
wenzelm@39865
   616
wenzelm@39865
   617
  \item @{text "(config, setup) ="}~@{ML Attrib.config_bool}~@{text
wenzelm@39865
   618
  "name default"} creates a named configuration option of type
wenzelm@39865
   619
  @{ML_type bool}, with the given @{text "default"} depending on the
wenzelm@39865
   620
  application context.  The resulting @{text "config"} can be used to
wenzelm@39865
   621
  get/map its value in a given context.  The @{text "setup"} function
wenzelm@39865
   622
  needs to be applied to the theory initially, in order to make
wenzelm@39865
   623
  concrete declaration syntax available to the user.
wenzelm@39865
   624
wenzelm@39865
   625
  \item @{ML Attrib.config_int} and @{ML Attrib.config_string} work
wenzelm@39865
   626
  like @{ML Attrib.config_bool}, but for types @{ML_type int} and
wenzelm@39865
   627
  @{ML_type string}, respectively.
wenzelm@39865
   628
wenzelm@39865
   629
  \end{description}
wenzelm@39865
   630
*}
wenzelm@39865
   631
wenzelm@39865
   632
text %mlex {* The following example shows how to declare and use a
wenzelm@39865
   633
  Boolean configuration option called @{text "my_flag"} with constant
wenzelm@39865
   634
  default value @{ML false}.  *}
wenzelm@39865
   635
wenzelm@39865
   636
ML {*
wenzelm@39865
   637
  val (my_flag, my_flag_setup) =
wenzelm@39865
   638
    Attrib.config_bool "my_flag" (K false)
wenzelm@39865
   639
*}
wenzelm@39865
   640
setup my_flag_setup
wenzelm@39865
   641
wenzelm@39865
   642
text {* Now the user can refer to @{attribute my_flag} in
wenzelm@39865
   643
  declarations, while we can retrieve the current value from the
wenzelm@39865
   644
  context via @{ML Config.get}.  *}
wenzelm@39865
   645
wenzelm@39865
   646
ML_val {* Config.get @{context} my_flag *}
wenzelm@39865
   647
wenzelm@39865
   648
declare [[my_flag = true]]
wenzelm@39865
   649
wenzelm@39865
   650
ML_val {* Config.get @{context} my_flag *}
wenzelm@39865
   651
wenzelm@39865
   652
example_proof
wenzelm@39865
   653
  note [[my_flag = false]]
wenzelm@39865
   654
  ML_val {* Config.get @{context} my_flag *}
wenzelm@39865
   655
qed
wenzelm@39865
   656
wenzelm@39865
   657
ML_val {* Config.get @{context} my_flag *}
wenzelm@39865
   658
wenzelm@39865
   659
wenzelm@26872
   660
section {* Names \label{sec:names} *}
wenzelm@20451
   661
wenzelm@34925
   662
text {* In principle, a name is just a string, but there are various
wenzelm@34925
   663
  conventions for representing additional structure.  For example,
wenzelm@34927
   664
  ``@{text "Foo.bar.baz"}'' is considered as a long name consisting of
wenzelm@34927
   665
  qualifier @{text "Foo.bar"} and base name @{text "baz"}.  The
wenzelm@34927
   666
  individual constituents of a name may have further substructure,
wenzelm@34927
   667
  e.g.\ the string ``\verb,\,\verb,<alpha>,'' encodes as a single
wenzelm@34927
   668
  symbol.
wenzelm@34927
   669
wenzelm@34927
   670
  \medskip Subsequently, we shall introduce specific categories of
wenzelm@34927
   671
  names.  Roughly speaking these correspond to logical entities as
wenzelm@34927
   672
  follows:
wenzelm@34927
   673
  \begin{itemize}
wenzelm@34927
   674
wenzelm@34927
   675
  \item Basic names (\secref{sec:basic-name}): free and bound
wenzelm@34927
   676
  variables.
wenzelm@34927
   677
wenzelm@34927
   678
  \item Indexed names (\secref{sec:indexname}): schematic variables.
wenzelm@34927
   679
wenzelm@34927
   680
  \item Long names (\secref{sec:long-name}): constants of any kind
wenzelm@34927
   681
  (type constructors, term constants, other concepts defined in user
wenzelm@34927
   682
  space).  Such entities are typically managed via name spaces
wenzelm@34927
   683
  (\secref{sec:name-space}).
wenzelm@34927
   684
wenzelm@34927
   685
  \end{itemize}
wenzelm@20451
   686
*}
wenzelm@20437
   687
wenzelm@20437
   688
wenzelm@39863
   689
subsection {* Strings of symbols \label{sec:symbols} *}
wenzelm@20437
   690
wenzelm@34925
   691
text {* A \emph{symbol} constitutes the smallest textual unit in
wenzelm@34925
   692
  Isabelle --- raw ML characters are normally not encountered at all!
wenzelm@34925
   693
  Isabelle strings consist of a sequence of symbols, represented as a
wenzelm@34925
   694
  packed string or an exploded list of strings.  Each symbol is in
wenzelm@34925
   695
  itself a small string, which has either one of the following forms:
wenzelm@20437
   696
wenzelm@20451
   697
  \begin{enumerate}
wenzelm@20437
   698
wenzelm@37533
   699
  \item a single ASCII character ``@{text "c"}'', for example
wenzelm@37533
   700
  ``\verb,a,'',
wenzelm@37533
   701
wenzelm@37533
   702
  \item a codepoint according to UTF8 (non-ASCII byte sequence),
wenzelm@20437
   703
wenzelm@20488
   704
  \item a regular symbol ``\verb,\,\verb,<,@{text "ident"}\verb,>,'',
wenzelm@20476
   705
  for example ``\verb,\,\verb,<alpha>,'',
wenzelm@20437
   706
wenzelm@20488
   707
  \item a control symbol ``\verb,\,\verb,<^,@{text "ident"}\verb,>,'',
wenzelm@20476
   708
  for example ``\verb,\,\verb,<^bold>,'',
wenzelm@20437
   709
wenzelm@20488
   710
  \item a raw symbol ``\verb,\,\verb,<^raw:,@{text text}\verb,>,''
wenzelm@34925
   711
  where @{text text} consists of printable characters excluding
wenzelm@20476
   712
  ``\verb,.,'' and ``\verb,>,'', for example
wenzelm@20476
   713
  ``\verb,\,\verb,<^raw:$\sum_{i = 1}^n$>,'',
wenzelm@20437
   714
wenzelm@20488
   715
  \item a numbered raw control symbol ``\verb,\,\verb,<^raw,@{text
wenzelm@20476
   716
  n}\verb,>, where @{text n} consists of digits, for example
wenzelm@20451
   717
  ``\verb,\,\verb,<^raw42>,''.
wenzelm@20437
   718
wenzelm@20451
   719
  \end{enumerate}
wenzelm@20437
   720
wenzelm@39861
   721
  The @{text "ident"} syntax for symbol names is @{text "letter
wenzelm@39861
   722
  (letter | digit)\<^sup>*"}, where @{text "letter = A..Za..z"} and @{text
wenzelm@39861
   723
  "digit = 0..9"}.  There are infinitely many regular symbols and
wenzelm@39861
   724
  control symbols, but a fixed collection of standard symbols is
wenzelm@39861
   725
  treated specifically.  For example, ``\verb,\,\verb,<alpha>,'' is
wenzelm@39861
   726
  classified as a letter, which means it may occur within regular
wenzelm@39861
   727
  Isabelle identifiers.
wenzelm@20437
   728
wenzelm@37533
   729
  The character set underlying Isabelle symbols is 7-bit ASCII, but
wenzelm@37533
   730
  8-bit character sequences are passed-through unchanged.  Unicode/UCS
wenzelm@37533
   731
  data in UTF-8 encoding is processed in a non-strict fashion, such
wenzelm@37533
   732
  that well-formed code sequences are recognized
wenzelm@37533
   733
  accordingly.\footnote{Note that ISO-Latin-1 differs from UTF-8 only
wenzelm@37533
   734
  in some special punctuation characters that even have replacements
wenzelm@37533
   735
  within the standard collection of Isabelle symbols.  Text consisting
wenzelm@37533
   736
  of ASCII plus accented letters can be processed in either encoding.}
wenzelm@37533
   737
  Unicode provides its own collection of mathematical symbols, but
wenzelm@37533
   738
  within the core Isabelle/ML world there is no link to the standard
wenzelm@37533
   739
  collection of Isabelle regular symbols.
wenzelm@20476
   740
wenzelm@20476
   741
  \medskip Output of Isabelle symbols depends on the print mode
wenzelm@29758
   742
  (\secref{print-mode}).  For example, the standard {\LaTeX} setup of
wenzelm@29758
   743
  the Isabelle document preparation system would present
wenzelm@20451
   744
  ``\verb,\,\verb,<alpha>,'' as @{text "\<alpha>"}, and
wenzelm@20451
   745
  ``\verb,\,\verb,<^bold>,\verb,\,\verb,<alpha>,'' as @{text
wenzelm@34925
   746
  "\<^bold>\<alpha>"}.  On-screen rendering usually works by mapping a finite
wenzelm@34925
   747
  subset of Isabelle symbols to suitable Unicode characters.
wenzelm@20451
   748
*}
wenzelm@20437
   749
wenzelm@20437
   750
text %mlref {*
wenzelm@20437
   751
  \begin{mldecls}
wenzelm@34921
   752
  @{index_ML_type "Symbol.symbol": string} \\
wenzelm@20437
   753
  @{index_ML Symbol.explode: "string -> Symbol.symbol list"} \\
wenzelm@20437
   754
  @{index_ML Symbol.is_letter: "Symbol.symbol -> bool"} \\
wenzelm@20437
   755
  @{index_ML Symbol.is_digit: "Symbol.symbol -> bool"} \\
wenzelm@20437
   756
  @{index_ML Symbol.is_quasi: "Symbol.symbol -> bool"} \\
wenzelm@20547
   757
  @{index_ML Symbol.is_blank: "Symbol.symbol -> bool"} \\
wenzelm@20547
   758
  \end{mldecls}
wenzelm@20547
   759
  \begin{mldecls}
wenzelm@20437
   760
  @{index_ML_type "Symbol.sym"} \\
wenzelm@20437
   761
  @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
wenzelm@20437
   762
  \end{mldecls}
wenzelm@20437
   763
wenzelm@20437
   764
  \begin{description}
wenzelm@20437
   765
wenzelm@39864
   766
  \item Type @{ML_type "Symbol.symbol"} represents individual Isabelle
wenzelm@34921
   767
  symbols.
wenzelm@20437
   768
wenzelm@20476
   769
  \item @{ML "Symbol.explode"}~@{text "str"} produces a symbol list
wenzelm@39821
   770
  from the packed form.  This function supersedes @{ML
wenzelm@20476
   771
  "String.explode"} for virtually all purposes of manipulating text in
wenzelm@34925
   772
  Isabelle!\footnote{The runtime overhead for exploded strings is
wenzelm@34925
   773
  mainly that of the list structure: individual symbols that happen to
wenzelm@39821
   774
  be a singleton string do not require extra memory in Poly/ML.}
wenzelm@20437
   775
wenzelm@20437
   776
  \item @{ML "Symbol.is_letter"}, @{ML "Symbol.is_digit"}, @{ML
wenzelm@20476
   777
  "Symbol.is_quasi"}, @{ML "Symbol.is_blank"} classify standard
wenzelm@20476
   778
  symbols according to fixed syntactic conventions of Isabelle, cf.\
wenzelm@20476
   779
  \cite{isabelle-isar-ref}.
wenzelm@20437
   780
wenzelm@39864
   781
  \item Type @{ML_type "Symbol.sym"} is a concrete datatype that
wenzelm@39864
   782
  represents the different kinds of symbols explicitly, with
wenzelm@39864
   783
  constructors @{ML "Symbol.Char"}, @{ML "Symbol.Sym"}, @{ML
wenzelm@39864
   784
  "Symbol.UTF8"}, @{ML "Symbol.Ctrl"}, @{ML "Symbol.Raw"}.
wenzelm@20437
   785
wenzelm@20437
   786
  \item @{ML "Symbol.decode"} converts the string representation of a
wenzelm@20451
   787
  symbol into the datatype version.
wenzelm@20437
   788
wenzelm@20437
   789
  \end{description}
wenzelm@34925
   790
wenzelm@34925
   791
  \paragraph{Historical note.} In the original SML90 standard the
wenzelm@34925
   792
  primitive ML type @{ML_type char} did not exists, and the basic @{ML
wenzelm@34925
   793
  "explode: string -> string list"} operation would produce a list of
wenzelm@34925
   794
  singleton strings as in Isabelle/ML today.  When SML97 came out,
wenzelm@34927
   795
  Isabelle did not adopt its slightly anachronistic 8-bit characters,
wenzelm@34927
   796
  but the idea of exploding a string into a list of small strings was
wenzelm@34925
   797
  extended to ``symbols'' as explained above.  Thus Isabelle sources
wenzelm@34925
   798
  can refer to an infinite store of user-defined symbols, without
wenzelm@34925
   799
  having to worry about the multitude of Unicode encodings.
wenzelm@20437
   800
*}
wenzelm@20437
   801
wenzelm@20437
   802
wenzelm@34927
   803
subsection {* Basic names \label{sec:basic-name} *}
wenzelm@20476
   804
wenzelm@20476
   805
text {*
wenzelm@20476
   806
  A \emph{basic name} essentially consists of a single Isabelle
wenzelm@20476
   807
  identifier.  There are conventions to mark separate classes of basic
wenzelm@29761
   808
  names, by attaching a suffix of underscores: one underscore means
wenzelm@29761
   809
  \emph{internal name}, two underscores means \emph{Skolem name},
wenzelm@29761
   810
  three underscores means \emph{internal Skolem name}.
wenzelm@20476
   811
wenzelm@20476
   812
  For example, the basic name @{text "foo"} has the internal version
wenzelm@20476
   813
  @{text "foo_"}, with Skolem versions @{text "foo__"} and @{text
wenzelm@20476
   814
  "foo___"}, respectively.
wenzelm@20476
   815
wenzelm@20488
   816
  These special versions provide copies of the basic name space, apart
wenzelm@20488
   817
  from anything that normally appears in the user text.  For example,
wenzelm@20488
   818
  system generated variables in Isar proof contexts are usually marked
wenzelm@34926
   819
  as internal, which prevents mysterious names like @{text "xaa"} to
wenzelm@34926
   820
  appear in human-readable text.
wenzelm@20476
   821
wenzelm@20488
   822
  \medskip Manipulating binding scopes often requires on-the-fly
wenzelm@20488
   823
  renamings.  A \emph{name context} contains a collection of already
wenzelm@20488
   824
  used names.  The @{text "declare"} operation adds names to the
wenzelm@20488
   825
  context.
wenzelm@20476
   826
wenzelm@20488
   827
  The @{text "invents"} operation derives a number of fresh names from
wenzelm@20488
   828
  a given starting point.  For example, the first three names derived
wenzelm@20488
   829
  from @{text "a"} are @{text "a"}, @{text "b"}, @{text "c"}.
wenzelm@20476
   830
wenzelm@20476
   831
  The @{text "variants"} operation produces fresh names by
wenzelm@20488
   832
  incrementing tentative names as base-26 numbers (with digits @{text
wenzelm@20488
   833
  "a..z"}) until all clashes are resolved.  For example, name @{text
wenzelm@20488
   834
  "foo"} results in variants @{text "fooa"}, @{text "foob"}, @{text
wenzelm@20488
   835
  "fooc"}, \dots, @{text "fooaa"}, @{text "fooab"} etc.; each renaming
wenzelm@20488
   836
  step picks the next unused variant from this sequence.
wenzelm@20476
   837
*}
wenzelm@20476
   838
wenzelm@20476
   839
text %mlref {*
wenzelm@20476
   840
  \begin{mldecls}
wenzelm@20476
   841
  @{index_ML Name.internal: "string -> string"} \\
wenzelm@20547
   842
  @{index_ML Name.skolem: "string -> string"} \\
wenzelm@20547
   843
  \end{mldecls}
wenzelm@20547
   844
  \begin{mldecls}
wenzelm@20476
   845
  @{index_ML_type Name.context} \\
wenzelm@20476
   846
  @{index_ML Name.context: Name.context} \\
wenzelm@20476
   847
  @{index_ML Name.declare: "string -> Name.context -> Name.context"} \\
wenzelm@20476
   848
  @{index_ML Name.invents: "Name.context -> string -> int -> string list"} \\
wenzelm@20476
   849
  @{index_ML Name.variants: "string list -> Name.context -> string list * Name.context"} \\
wenzelm@20476
   850
  \end{mldecls}
wenzelm@34926
   851
  \begin{mldecls}
wenzelm@34926
   852
  @{index_ML Variable.names_of: "Proof.context -> Name.context"} \\
wenzelm@34926
   853
  \end{mldecls}
wenzelm@20476
   854
wenzelm@20476
   855
  \begin{description}
wenzelm@20476
   856
wenzelm@20476
   857
  \item @{ML Name.internal}~@{text "name"} produces an internal name
wenzelm@20476
   858
  by adding one underscore.
wenzelm@20476
   859
wenzelm@20476
   860
  \item @{ML Name.skolem}~@{text "name"} produces a Skolem name by
wenzelm@20476
   861
  adding two underscores.
wenzelm@20476
   862
wenzelm@39864
   863
  \item Type @{ML_type Name.context} represents the context of already
wenzelm@39864
   864
  used names; the initial value is @{ML "Name.context"}.
wenzelm@20476
   865
wenzelm@20488
   866
  \item @{ML Name.declare}~@{text "name"} enters a used name into the
wenzelm@20488
   867
  context.
wenzelm@20437
   868
wenzelm@20488
   869
  \item @{ML Name.invents}~@{text "context name n"} produces @{text
wenzelm@20488
   870
  "n"} fresh names derived from @{text "name"}.
wenzelm@20488
   871
wenzelm@20488
   872
  \item @{ML Name.variants}~@{text "names context"} produces fresh
wenzelm@29761
   873
  variants of @{text "names"}; the result is entered into the context.
wenzelm@20476
   874
wenzelm@34926
   875
  \item @{ML Variable.names_of}~@{text "ctxt"} retrieves the context
wenzelm@34926
   876
  of declared type and term variable names.  Projecting a proof
wenzelm@34926
   877
  context down to a primitive name context is occasionally useful when
wenzelm@34926
   878
  invoking lower-level operations.  Regular management of ``fresh
wenzelm@34926
   879
  variables'' is done by suitable operations of structure @{ML_struct
wenzelm@34926
   880
  Variable}, which is also able to provide an official status of
wenzelm@34926
   881
  ``locally fixed variable'' within the logical environment (cf.\
wenzelm@34926
   882
  \secref{sec:variables}).
wenzelm@34926
   883
wenzelm@20476
   884
  \end{description}
wenzelm@20476
   885
*}
wenzelm@20476
   886
wenzelm@39857
   887
text %mlex {* The following simple examples demonstrate how to produce
wenzelm@39857
   888
  fresh names from the initial @{ML Name.context}. *}
wenzelm@39857
   889
wenzelm@39857
   890
ML {*
wenzelm@39857
   891
  Name.invents Name.context "a" 5;
wenzelm@39857
   892
  #1 (Name.variants ["x", "x", "a", "a", "'a", "'a"] Name.context);
wenzelm@39857
   893
*}
wenzelm@39857
   894
wenzelm@39857
   895
text {* \medskip The same works reletively to the formal context as
wenzelm@39861
   896
  follows. *}
wenzelm@39857
   897
wenzelm@39857
   898
locale ex = fixes a b c :: 'a
wenzelm@39857
   899
begin
wenzelm@39857
   900
wenzelm@39857
   901
ML {*
wenzelm@39857
   902
  val names = Variable.names_of @{context};
wenzelm@39857
   903
  Name.invents names "a" 5;
wenzelm@39857
   904
  #1 (Name.variants ["x", "x", "a", "a", "'a", "'a"] names);
wenzelm@39857
   905
*}
wenzelm@39857
   906
wenzelm@39857
   907
end
wenzelm@39857
   908
wenzelm@20476
   909
wenzelm@34927
   910
subsection {* Indexed names \label{sec:indexname} *}
wenzelm@20476
   911
wenzelm@20476
   912
text {*
wenzelm@20476
   913
  An \emph{indexed name} (or @{text "indexname"}) is a pair of a basic
wenzelm@20488
   914
  name and a natural number.  This representation allows efficient
wenzelm@20488
   915
  renaming by incrementing the second component only.  The canonical
wenzelm@20488
   916
  way to rename two collections of indexnames apart from each other is
wenzelm@20488
   917
  this: determine the maximum index @{text "maxidx"} of the first
wenzelm@20488
   918
  collection, then increment all indexes of the second collection by
wenzelm@20488
   919
  @{text "maxidx + 1"}; the maximum index of an empty collection is
wenzelm@20488
   920
  @{text "-1"}.
wenzelm@20476
   921
wenzelm@34927
   922
  Occasionally, basic names are injected into the same pair type of
wenzelm@34927
   923
  indexed names: then @{text "(x, -1)"} is used to encode the basic
wenzelm@34927
   924
  name @{text "x"}.
wenzelm@20488
   925
wenzelm@20488
   926
  \medskip Isabelle syntax observes the following rules for
wenzelm@20488
   927
  representing an indexname @{text "(x, i)"} as a packed string:
wenzelm@20476
   928
wenzelm@20476
   929
  \begin{itemize}
wenzelm@20476
   930
wenzelm@20479
   931
  \item @{text "?x"} if @{text "x"} does not end with a digit and @{text "i = 0"},
wenzelm@20476
   932
wenzelm@20476
   933
  \item @{text "?xi"} if @{text "x"} does not end with a digit,
wenzelm@20476
   934
wenzelm@20488
   935
  \item @{text "?x.i"} otherwise.
wenzelm@20476
   936
wenzelm@20476
   937
  \end{itemize}
wenzelm@20470
   938
wenzelm@34927
   939
  Indexnames may acquire large index numbers after several maxidx
wenzelm@34927
   940
  shifts have been applied.  Results are usually normalized towards
wenzelm@34927
   941
  @{text "0"} at certain checkpoints, notably at the end of a proof.
wenzelm@34927
   942
  This works by producing variants of the corresponding basic name
wenzelm@34927
   943
  components.  For example, the collection @{text "?x1, ?x7, ?x42"}
wenzelm@34927
   944
  becomes @{text "?x, ?xa, ?xb"}.
wenzelm@20476
   945
*}
wenzelm@20476
   946
wenzelm@20476
   947
text %mlref {*
wenzelm@20476
   948
  \begin{mldecls}
wenzelm@39861
   949
  @{index_ML_type indexname: "string * int"} \\
wenzelm@20476
   950
  \end{mldecls}
wenzelm@20476
   951
wenzelm@20476
   952
  \begin{description}
wenzelm@20476
   953
wenzelm@39864
   954
  \item Type @{ML_type indexname} represents indexed names.  This is
wenzelm@39864
   955
  an abbreviation for @{ML_type "string * int"}.  The second component
wenzelm@39864
   956
  is usually non-negative, except for situations where @{text "(x,
wenzelm@39864
   957
  -1)"} is used to inject basic names into this type.  Other negative
wenzelm@34926
   958
  indexes should not be used.
wenzelm@20476
   959
wenzelm@20476
   960
  \end{description}
wenzelm@20476
   961
*}
wenzelm@20476
   962
wenzelm@20476
   963
wenzelm@34927
   964
subsection {* Long names \label{sec:long-name} *}
wenzelm@20476
   965
wenzelm@34927
   966
text {* A \emph{long name} consists of a sequence of non-empty name
wenzelm@34927
   967
  components.  The packed representation uses a dot as separator, as
wenzelm@34927
   968
  in ``@{text "A.b.c"}''.  The last component is called \emph{base
wenzelm@34927
   969
  name}, the remaining prefix is called \emph{qualifier} (which may be
wenzelm@34927
   970
  empty).  The qualifier can be understood as the access path to the
wenzelm@34927
   971
  named entity while passing through some nested block-structure,
wenzelm@34927
   972
  although our free-form long names do not really enforce any strict
wenzelm@34927
   973
  discipline.
wenzelm@34927
   974
wenzelm@34927
   975
  For example, an item named ``@{text "A.b.c"}'' may be understood as
wenzelm@34927
   976
  a local entity @{text "c"}, within a local structure @{text "b"},
wenzelm@34927
   977
  within a global structure @{text "A"}.  In practice, long names
wenzelm@34927
   978
  usually represent 1--3 levels of qualification.  User ML code should
wenzelm@34927
   979
  not make any assumptions about the particular structure of long
wenzelm@34927
   980
  names!
wenzelm@20437
   981
wenzelm@20476
   982
  The empty name is commonly used as an indication of unnamed
wenzelm@34927
   983
  entities, or entities that are not entered into the corresponding
wenzelm@34927
   984
  name space, whenever this makes any sense.  The basic operations on
wenzelm@34927
   985
  long names map empty names again to empty names.
wenzelm@20437
   986
*}
wenzelm@20437
   987
wenzelm@20476
   988
text %mlref {*
wenzelm@20476
   989
  \begin{mldecls}
wenzelm@30365
   990
  @{index_ML Long_Name.base_name: "string -> string"} \\
wenzelm@30365
   991
  @{index_ML Long_Name.qualifier: "string -> string"} \\
wenzelm@30365
   992
  @{index_ML Long_Name.append: "string -> string -> string"} \\
wenzelm@30365
   993
  @{index_ML Long_Name.implode: "string list -> string"} \\
wenzelm@30365
   994
  @{index_ML Long_Name.explode: "string -> string list"} \\
wenzelm@20547
   995
  \end{mldecls}
wenzelm@34927
   996
wenzelm@34927
   997
  \begin{description}
wenzelm@34927
   998
wenzelm@34927
   999
  \item @{ML Long_Name.base_name}~@{text "name"} returns the base name
wenzelm@34927
  1000
  of a long name.
wenzelm@34927
  1001
wenzelm@34927
  1002
  \item @{ML Long_Name.qualifier}~@{text "name"} returns the qualifier
wenzelm@34927
  1003
  of a long name.
wenzelm@34927
  1004
wenzelm@34927
  1005
  \item @{ML Long_Name.append}~@{text "name\<^isub>1 name\<^isub>2"} appends two long
wenzelm@34927
  1006
  names.
wenzelm@34927
  1007
wenzelm@34927
  1008
  \item @{ML Long_Name.implode}~@{text "names"} and @{ML
wenzelm@34927
  1009
  Long_Name.explode}~@{text "name"} convert between the packed string
wenzelm@34927
  1010
  representation and the explicit list form of long names.
wenzelm@34927
  1011
wenzelm@34927
  1012
  \end{description}
wenzelm@34927
  1013
*}
wenzelm@34927
  1014
wenzelm@34927
  1015
wenzelm@34927
  1016
subsection {* Name spaces \label{sec:name-space} *}
wenzelm@34927
  1017
wenzelm@34927
  1018
text {* A @{text "name space"} manages a collection of long names,
wenzelm@34927
  1019
  together with a mapping between partially qualified external names
wenzelm@34927
  1020
  and fully qualified internal names (in both directions).  Note that
wenzelm@34927
  1021
  the corresponding @{text "intern"} and @{text "extern"} operations
wenzelm@34927
  1022
  are mostly used for parsing and printing only!  The @{text
wenzelm@34927
  1023
  "declare"} operation augments a name space according to the accesses
wenzelm@34927
  1024
  determined by a given binding, and a naming policy from the context.
wenzelm@34927
  1025
wenzelm@34927
  1026
  \medskip A @{text "binding"} specifies details about the prospective
wenzelm@34927
  1027
  long name of a newly introduced formal entity.  It consists of a
wenzelm@34927
  1028
  base name, prefixes for qualification (separate ones for system
wenzelm@34927
  1029
  infrastructure and user-space mechanisms), a slot for the original
wenzelm@34927
  1030
  source position, and some additional flags.
wenzelm@34927
  1031
wenzelm@34927
  1032
  \medskip A @{text "naming"} provides some additional details for
wenzelm@34927
  1033
  producing a long name from a binding.  Normally, the naming is
wenzelm@34927
  1034
  implicit in the theory or proof context.  The @{text "full"}
wenzelm@34927
  1035
  operation (and its variants for different context types) produces a
wenzelm@34927
  1036
  fully qualified internal name to be entered into a name space.  The
wenzelm@34927
  1037
  main equation of this ``chemical reaction'' when binding new
wenzelm@34927
  1038
  entities in a context is as follows:
wenzelm@34927
  1039
wenzelm@39861
  1040
  \medskip
wenzelm@34927
  1041
  \begin{tabular}{l}
wenzelm@34927
  1042
  @{text "binding + naming \<longrightarrow> long name + name space accesses"}
wenzelm@34927
  1043
  \end{tabular}
wenzelm@34927
  1044
wenzelm@39861
  1045
  \bigskip As a general principle, there is a separate name space for
wenzelm@34927
  1046
  each kind of formal entity, e.g.\ fact, logical constant, type
wenzelm@34927
  1047
  constructor, type class.  It is usually clear from the occurrence in
wenzelm@34927
  1048
  concrete syntax (or from the scope) which kind of entity a name
wenzelm@34927
  1049
  refers to.  For example, the very same name @{text "c"} may be used
wenzelm@34927
  1050
  uniformly for a constant, type constructor, and type class.
wenzelm@34927
  1051
wenzelm@34927
  1052
  There are common schemes to name derived entities systematically
wenzelm@34927
  1053
  according to the name of the main logical entity involved, e.g.\
wenzelm@34927
  1054
  fact @{text "c.intro"} for a canonical introduction rule related to
wenzelm@34927
  1055
  constant @{text "c"}.  This technique of mapping names from one
wenzelm@34927
  1056
  space into another requires some care in order to avoid conflicts.
wenzelm@34927
  1057
  In particular, theorem names derived from a type constructor or type
wenzelm@39839
  1058
  class should get an additional suffix in addition to the usual
wenzelm@39839
  1059
  qualification.  This leads to the following conventions for derived
wenzelm@39839
  1060
  names:
wenzelm@39839
  1061
wenzelm@39839
  1062
  \medskip
wenzelm@39839
  1063
  \begin{tabular}{ll}
wenzelm@39839
  1064
  logical entity & fact name \\\hline
wenzelm@39839
  1065
  constant @{text "c"} & @{text "c.intro"} \\
wenzelm@39839
  1066
  type @{text "c"} & @{text "c_type.intro"} \\
wenzelm@39839
  1067
  class @{text "c"} & @{text "c_class.intro"} \\
wenzelm@39839
  1068
  \end{tabular}
wenzelm@34927
  1069
*}
wenzelm@34927
  1070
wenzelm@34927
  1071
text %mlref {*
wenzelm@34927
  1072
  \begin{mldecls}
wenzelm@34927
  1073
  @{index_ML_type binding} \\
wenzelm@34927
  1074
  @{index_ML Binding.empty: binding} \\
wenzelm@34927
  1075
  @{index_ML Binding.name: "string -> binding"} \\
wenzelm@34927
  1076
  @{index_ML Binding.qualify: "bool -> string -> binding -> binding"} \\
wenzelm@34927
  1077
  @{index_ML Binding.prefix: "bool -> string -> binding -> binding"} \\
wenzelm@34927
  1078
  @{index_ML Binding.conceal: "binding -> binding"} \\
wenzelm@34927
  1079
  @{index_ML Binding.str_of: "binding -> string"} \\
wenzelm@34927
  1080
  \end{mldecls}
wenzelm@20547
  1081
  \begin{mldecls}
haftmann@33174
  1082
  @{index_ML_type Name_Space.naming} \\
haftmann@33174
  1083
  @{index_ML Name_Space.default_naming: Name_Space.naming} \\
haftmann@33174
  1084
  @{index_ML Name_Space.add_path: "string -> Name_Space.naming -> Name_Space.naming"} \\
haftmann@33174
  1085
  @{index_ML Name_Space.full_name: "Name_Space.naming -> binding -> string"} \\
wenzelm@20547
  1086
  \end{mldecls}
wenzelm@20547
  1087
  \begin{mldecls}
haftmann@33174
  1088
  @{index_ML_type Name_Space.T} \\
haftmann@33174
  1089
  @{index_ML Name_Space.empty: "string -> Name_Space.T"} \\
haftmann@33174
  1090
  @{index_ML Name_Space.merge: "Name_Space.T * Name_Space.T -> Name_Space.T"} \\
haftmann@33174
  1091
  @{index_ML Name_Space.declare: "bool -> Name_Space.naming -> binding -> Name_Space.T ->
haftmann@33174
  1092
  string * Name_Space.T"} \\
haftmann@33174
  1093
  @{index_ML Name_Space.intern: "Name_Space.T -> string -> string"} \\
haftmann@33174
  1094
  @{index_ML Name_Space.extern: "Name_Space.T -> string -> string"} \\
wenzelm@34927
  1095
  @{index_ML Name_Space.is_concealed: "Name_Space.T -> string -> bool"}
wenzelm@20476
  1096
  \end{mldecls}
wenzelm@20437
  1097
wenzelm@20476
  1098
  \begin{description}
wenzelm@20476
  1099
wenzelm@39864
  1100
  \item Type @{ML_type binding} represents the abstract concept of
wenzelm@39864
  1101
  name bindings.
wenzelm@34927
  1102
wenzelm@34927
  1103
  \item @{ML Binding.empty} is the empty binding.
wenzelm@20476
  1104
wenzelm@34927
  1105
  \item @{ML Binding.name}~@{text "name"} produces a binding with base
wenzelm@39832
  1106
  name @{text "name"}.  Note that this lacks proper source position
wenzelm@39832
  1107
  information; see also the ML antiquotation @{ML_antiquotation
wenzelm@39832
  1108
  binding}.
wenzelm@34927
  1109
wenzelm@34927
  1110
  \item @{ML Binding.qualify}~@{text "mandatory name binding"}
wenzelm@34927
  1111
  prefixes qualifier @{text "name"} to @{text "binding"}.  The @{text
wenzelm@34927
  1112
  "mandatory"} flag tells if this name component always needs to be
wenzelm@34927
  1113
  given in name space accesses --- this is mostly @{text "false"} in
wenzelm@34927
  1114
  practice.  Note that this part of qualification is typically used in
wenzelm@34927
  1115
  derived specification mechanisms.
wenzelm@20437
  1116
wenzelm@34927
  1117
  \item @{ML Binding.prefix} is similar to @{ML Binding.qualify}, but
wenzelm@34927
  1118
  affects the system prefix.  This part of extra qualification is
wenzelm@34927
  1119
  typically used in the infrastructure for modular specifications,
wenzelm@34927
  1120
  notably ``local theory targets'' (see also \chref{ch:local-theory}).
wenzelm@20437
  1121
wenzelm@34927
  1122
  \item @{ML Binding.conceal}~@{text "binding"} indicates that the
wenzelm@34927
  1123
  binding shall refer to an entity that serves foundational purposes
wenzelm@34927
  1124
  only.  This flag helps to mark implementation details of
wenzelm@34927
  1125
  specification mechanism etc.  Other tools should not depend on the
wenzelm@34927
  1126
  particulars of concealed entities (cf.\ @{ML
wenzelm@34927
  1127
  Name_Space.is_concealed}).
wenzelm@34927
  1128
wenzelm@34927
  1129
  \item @{ML Binding.str_of}~@{text "binding"} produces a string
wenzelm@34927
  1130
  representation for human-readable output, together with some formal
wenzelm@34927
  1131
  markup that might get used in GUI front-ends, for example.
wenzelm@20476
  1132
wenzelm@39864
  1133
  \item Type @{ML_type Name_Space.naming} represents the abstract
wenzelm@39864
  1134
  concept of a naming policy.
wenzelm@20437
  1135
haftmann@33174
  1136
  \item @{ML Name_Space.default_naming} is the default naming policy.
wenzelm@20476
  1137
  In a theory context, this is usually augmented by a path prefix
wenzelm@20476
  1138
  consisting of the theory name.
wenzelm@20476
  1139
haftmann@33174
  1140
  \item @{ML Name_Space.add_path}~@{text "path naming"} augments the
wenzelm@20488
  1141
  naming policy by extending its path component.
wenzelm@20437
  1142
haftmann@33174
  1143
  \item @{ML Name_Space.full_name}~@{text "naming binding"} turns a
wenzelm@30281
  1144
  name binding (usually a basic name) into the fully qualified
haftmann@29008
  1145
  internal name, according to the given naming policy.
wenzelm@20476
  1146
wenzelm@39864
  1147
  \item Type @{ML_type Name_Space.T} represents name spaces.
wenzelm@20476
  1148
haftmann@33174
  1149
  \item @{ML Name_Space.empty}~@{text "kind"} and @{ML Name_Space.merge}~@{text
wenzelm@20488
  1150
  "(space\<^isub>1, space\<^isub>2)"} are the canonical operations for
wenzelm@20488
  1151
  maintaining name spaces according to theory data management
haftmann@33174
  1152
  (\secref{sec:context-data}); @{text "kind"} is a formal comment
haftmann@33174
  1153
  to characterize the purpose of a name space.
wenzelm@20437
  1154
haftmann@33174
  1155
  \item @{ML Name_Space.declare}~@{text "strict naming bindings
haftmann@33174
  1156
  space"} enters a name binding as fully qualified internal name into
haftmann@33174
  1157
  the name space, with external accesses determined by the naming
haftmann@33174
  1158
  policy.
wenzelm@20476
  1159
haftmann@33174
  1160
  \item @{ML Name_Space.intern}~@{text "space name"} internalizes a
wenzelm@20476
  1161
  (partially qualified) external name.
wenzelm@20437
  1162
wenzelm@20488
  1163
  This operation is mostly for parsing!  Note that fully qualified
wenzelm@20476
  1164
  names stemming from declarations are produced via @{ML
haftmann@33174
  1165
  "Name_Space.full_name"} and @{ML "Name_Space.declare"}
haftmann@29008
  1166
  (or their derivatives for @{ML_type theory} and
wenzelm@20488
  1167
  @{ML_type Proof.context}).
wenzelm@20437
  1168
haftmann@33174
  1169
  \item @{ML Name_Space.extern}~@{text "space name"} externalizes a
wenzelm@20476
  1170
  (fully qualified) internal name.
wenzelm@20476
  1171
wenzelm@30281
  1172
  This operation is mostly for printing!  User code should not rely on
wenzelm@30281
  1173
  the precise result too much.
wenzelm@20476
  1174
wenzelm@34927
  1175
  \item @{ML Name_Space.is_concealed}~@{text "space name"} indicates
wenzelm@34927
  1176
  whether @{text "name"} refers to a strictly private entity that
wenzelm@34927
  1177
  other tools are supposed to ignore!
wenzelm@34927
  1178
wenzelm@20476
  1179
  \end{description}
wenzelm@20476
  1180
*}
wenzelm@30272
  1181
wenzelm@39832
  1182
text %mlantiq {*
wenzelm@39832
  1183
  \begin{matharray}{rcl}
wenzelm@39832
  1184
  @{ML_antiquotation_def "binding"} & : & @{text ML_antiquotation} \\
wenzelm@39832
  1185
  \end{matharray}
wenzelm@39832
  1186
wenzelm@39832
  1187
  \begin{rail}
wenzelm@39832
  1188
  'binding' name
wenzelm@39832
  1189
  ;
wenzelm@39832
  1190
  \end{rail}
wenzelm@39832
  1191
wenzelm@39832
  1192
  \begin{description}
wenzelm@39832
  1193
wenzelm@39832
  1194
  \item @{text "@{binding name}"} produces a binding with base name
wenzelm@39832
  1195
  @{text "name"} and the source position taken from the concrete
wenzelm@39832
  1196
  syntax of this antiquotation.  In many situations this is more
wenzelm@39832
  1197
  appropriate than the more basic @{ML Binding.name} function.
wenzelm@39832
  1198
wenzelm@39832
  1199
  \end{description}
wenzelm@39832
  1200
*}
wenzelm@39832
  1201
wenzelm@39833
  1202
text %mlex {* The following example yields the source position of some
wenzelm@39833
  1203
  concrete binding inlined into the text.
wenzelm@39833
  1204
*}
wenzelm@39833
  1205
wenzelm@39833
  1206
ML {* Binding.pos_of @{binding here} *}
wenzelm@39833
  1207
wenzelm@39861
  1208
text {* \medskip That position can be also printed in a message as
wenzelm@39861
  1209
  follows. *}
wenzelm@39833
  1210
wenzelm@39833
  1211
ML_command {*
wenzelm@39833
  1212
  writeln
wenzelm@39833
  1213
    ("Look here" ^ Position.str_of (Binding.pos_of @{binding here}))
wenzelm@39833
  1214
*}
wenzelm@39833
  1215
wenzelm@39861
  1216
text {* This illustrates a key virtue of formalized bindings as
wenzelm@39861
  1217
  opposed to raw specifications of base names: the system can use this
wenzelm@39861
  1218
  additional information for advanced feedback given to the user. *}
wenzelm@39833
  1219
wenzelm@18537
  1220
end