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