doc-src/IsarImplementation/Thy/prelim.thy
changeset 20451 27ea2ba48fa3
parent 20450 725a91601ed1
child 20452 6d8b29c7a960
equal deleted inserted replaced
20450:725a91601ed1 20451:27ea2ba48fa3
     6 chapter {* Preliminaries *}
     6 chapter {* Preliminaries *}
     7 
     7 
     8 section {* Contexts \label{sec:context} *}
     8 section {* Contexts \label{sec:context} *}
     9 
     9 
    10 text {*
    10 text {*
    11   A logical context represents the background that is taken for
    11   A logical context represents the background that is required for
    12   granted when formulating statements and composing proofs.  It acts
    12   formulating statements and composing proofs.  It acts as a medium to
    13   as a medium to produce formal content, depending on earlier material
    13   produce formal content, depending on earlier material (declarations,
    14   (declarations, results etc.).
    14   results etc.).
    15 
    15 
    16   In particular, derivations within the primitive Pure logic can be
    16   For example, derivations within the Isabelle/Pure logic can be
    17   described as a judgment @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<phi>"}, meaning that a
    17   described as a judgment @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<phi>"}, which means that a
    18   proposition @{text "\<phi>"} is derivable from hypotheses @{text "\<Gamma>"}
    18   proposition @{text "\<phi>"} is derivable from hypotheses @{text "\<Gamma>"}
    19   within the theory @{text "\<Theta>"}.  There are logical reasons for
    19   within the theory @{text "\<Theta>"}.  There are logical reasons for
    20   keeping @{text "\<Theta>"} and @{text "\<Gamma>"} separate: theories support type
    20   keeping @{text "\<Theta>"} and @{text "\<Gamma>"} separate: theories can be
    21   constructors and schematic polymorphism of constants and axioms,
    21   liberal about supporting type constructors and schematic
    22   while the inner calculus of @{text "\<Gamma> \<turnstile> \<phi>"} is limited to Simple
    22   polymorphism of constants and axioms, while the inner calculus of
    23   Type Theory (with fixed type variables in the assumptions).
    23   @{text "\<Gamma> \<turnstile> \<phi>"} is strictly limited to Simple Type Theory (with
       
    24   fixed type variables in the assumptions).
    24 
    25 
    25   \medskip Contexts and derivations are linked by the following key
    26   \medskip Contexts and derivations are linked by the following key
    26   principles:
    27   principles:
    27 
    28 
    28   \begin{itemize}
    29   \begin{itemize}
    29 
    30 
    30   \item Transfer: monotonicity of derivations admits results to be
    31   \item Transfer: monotonicity of derivations admits results to be
    31   transferred into a larger context, i.e.\ @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<phi>"}
    32   transferred into a \emph{larger} context, i.e.\ @{text "\<Gamma> \<turnstile>\<^sub>\<Theta>
    32   implies @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta>\<^sub>' \<phi>"} for contexts @{text "\<Theta>' \<supseteq>
    33   \<phi>"} implies @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta>\<^sub>' \<phi>"} for contexts @{text "\<Theta>'
    33   \<Theta>"} and @{text "\<Gamma>' \<supseteq> \<Gamma>"}.
    34   \<supseteq> \<Theta>"} and @{text "\<Gamma>' \<supseteq> \<Gamma>"}.
    34 
    35 
    35   \item Export: discharge of hypotheses admits results to be exported
    36   \item Export: discharge of hypotheses admits results to be exported
    36   into a smaller context, i.e.\ @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta> \<phi>"} implies
    37   into a \emph{smaller} context, i.e.\ @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta> \<phi>"}
    37   @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<Delta> \<Longrightarrow> \<phi>"} where @{text "\<Gamma>' \<supseteq> \<Gamma>"} and @{text "\<Delta> =
    38   implies @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<Delta> \<Longrightarrow> \<phi>"} where @{text "\<Gamma>' \<supseteq> \<Gamma>"} and
    38   \<Gamma>' - \<Gamma>"}.  Note that @{text "\<Theta>"} remains unchanged here, only the
    39   @{text "\<Delta> = \<Gamma>' - \<Gamma>"}.  Note that @{text "\<Theta>"} remains unchanged here,
    39   @{text "\<Gamma>"} part is affected.
    40   only the @{text "\<Gamma>"} part is affected.
    40 
    41 
    41   \end{itemize}
    42   \end{itemize}
    42 
    43 
    43   \medskip Isabelle/Isar provides two different notions of abstract
    44   \medskip By modeling the main characteristics of the primitive
    44   containers called \emph{theory context} and \emph{proof context},
    45   @{text "\<Theta>"} and @{text "\<Gamma>"} above, and abstracting over any
    45   respectively.  These model the main characteristics of the primitive
    46   particular logical content, we arrive at the fundamental notions of
    46   @{text "\<Theta>"} and @{text "\<Gamma>"} above, without subscribing to any
    47   \emph{theory context} and \emph{proof context} in Isabelle/Isar.
    47   particular kind of content yet.  Instead, contexts merely impose a
    48   These implement a certain policy to manage arbitrary \emph{context
    48   certain policy of managing arbitrary \emph{context data}.  The
    49   data}.  There is a strongly-typed mechanism to declare new kinds of
    49   system provides strongly typed mechanisms to declare new kinds of
       
    50   data at compile time.
    50   data at compile time.
    51 
    51 
    52   Thus the internal bootstrap process of Isabelle/Pure eventually
    52   The internal bootstrap process of Isabelle/Pure eventually reaches a
    53   reaches a stage where certain data slots provide the logical content
    53   stage where certain data slots provide the logical content of @{text
    54   of @{text "\<Theta>"} and @{text "\<Gamma>"} sketched above, but this does not
    54   "\<Theta>"} and @{text "\<Gamma>"} sketched above, but this does not stop there!
    55   stop there!  Various additional data slots support all kinds of
    55   Various additional data slots support all kinds of mechanisms that
    56   mechanisms that are not necessarily part of the core logic.
    56   are not necessarily part of the core logic.
    57 
    57 
    58   For example, there would be data for canonical introduction and
    58   For example, there would be data for canonical introduction and
    59   elimination rules for arbitrary operators (depending on the
    59   elimination rules for arbitrary operators (depending on the
    60   object-logic and application), which enables users to perform
    60   object-logic and application), which enables users to perform
    61   standard proof steps implicitly (cf.\ the @{text "rule"} method).
    61   standard proof steps implicitly (cf.\ the @{text "rule"} method
    62 
    62   \cite{isabelle-isar-ref}).
    63   Isabelle is able to bring forth more and more concepts successively.
    63 
    64   In particular, an object-logic like Isabelle/HOL continues the
    64   \medskip Thus Isabelle/Isar is able to bring forth more and more
    65   Isabelle/Pure setup by adding specific components for automated
    65   concepts successively.  In particular, an object-logic like
    66   reasoning (classical reasoner, tableau prover, structured induction
    66   Isabelle/HOL continues the Isabelle/Pure setup by adding specific
    67   etc.) and derived specification mechanisms (inductive predicates,
    67   components for automated reasoning (classical reasoner, tableau
    68   recursive functions etc.).  All of this is based on the generic data
    68   prover, structured induction etc.) and derived specification
    69   management by theory and proof contexts.
    69   mechanisms (inductive predicates, recursive functions etc.).  All of
       
    70   this is ultimately based on the generic data management by theory
       
    71   and proof contexts introduced here.
    70 *}
    72 *}
    71 
    73 
    72 
    74 
    73 subsection {* Theory context \label{sec:context-theory} *}
    75 subsection {* Theory context \label{sec:context-theory} *}
    74 
    76 
    75 text {*
    77 text {*
    76   \glossary{Theory}{FIXME}
    78   \glossary{Theory}{FIXME}
    77 
    79 
    78   Each theory is explicitly named and holds a unique identifier.
    80   A \emph{theory} is a data container with explicit named and unique
    79   There is a separate \emph{theory reference} for pointing backwards
    81   identifier.  Theories are related by a (nominal) sub-theory
    80   to the enclosing theory context of derived entities.  Theories are
    82   relation, which corresponds to the dependency graph of the original
    81   related by a (nominal) sub-theory relation, which corresponds to the
    83   construction; each theory is derived from a certain sub-graph of
    82   canonical dependency graph: each theory is derived from a certain
    84   ancestor theories.
    83   sub-graph of ancestor theories.  The @{text "merge"} of two theories
    85 
    84   refers to the least upper bound, which actually degenerates into
    86   The @{text "merge"} operation produces the least upper bound of two
    85   absorption of one theory into the other, due to the nominal
    87   theories, which actually degenerates into absorption of one theory
    86   sub-theory relation this.
    88   into the other (due to the nominal sub-theory relation).
    87 
    89 
    88   The @{text "begin"} operation starts a new theory by importing
    90   The @{text "begin"} operation starts a new theory by importing
    89   several parent theories and entering a special @{text "draft"} mode,
    91   several parent theories and entering a special @{text "draft"} mode,
    90   which is sustained until the final @{text "end"} operation.  A draft
    92   which is sustained until the final @{text "end"} operation.  A draft
    91   mode theory acts like a linear type, where updates invalidate
    93   theory acts like a linear type, where updates invalidate earlier
    92   earlier drafts, but theory reference values will be propagated
    94   versions.  An invalidated draft is called ``stale''.
    93   automatically.  Thus derived entities that ``belong'' to a draft
       
    94   might be transferred spontaneously to a larger context.  An
       
    95   invalidated draft is called ``stale''.
       
    96 
    95 
    97   The @{text "checkpoint"} operation produces an intermediate stepping
    96   The @{text "checkpoint"} operation produces an intermediate stepping
    98   stone that will survive the next update unscathed: both the original
    97   stone that will survive the next update: both the original and the
    99   and the changed theory remain valid and are related by the
    98   changed theory remain valid and are related by the sub-theory
   100   sub-theory relation.  Checkpointing essentially recovers purely
    99   relation.  Checkpointing essentially recovers purely functional
   101   functional theory values, at the expense of some extra internal
   100   theory values, at the expense of some extra internal bookkeeping.
   102   bookkeeping.
       
   103 
   101 
   104   The @{text "copy"} operation produces an auxiliary version that has
   102   The @{text "copy"} operation produces an auxiliary version that has
   105   the same data content, but is unrelated to the original: updates of
   103   the same data content, but is unrelated to the original: updates of
   106   the copy do not affect the original, neither does the sub-theory
   104   the copy do not affect the original, neither does the sub-theory
   107   relation hold.
   105   relation hold.
   108 
   106 
   109   \medskip The example in \figref{fig:ex-theory} below shows a theory
   107   \medskip The example in \figref{fig:ex-theory} below shows a theory
   110   graph derived from @{text "Pure"}. Theory @{text "Length"} imports
   108   graph derived from @{text "Pure"}, with theory @{text "Length"}
   111   @{text "Nat"} and @{text "List"}.  The theory body consists of a
   109   importing @{text "Nat"} and @{text "List"}.  The body of @{text
   112   sequence of updates, working mostly on drafts.  Intermediate
   110   "Length"} consists of a sequence of updates, working mostly on
   113   checkpoints may occur as well, due to the history mechanism provided
   111   drafts.  Intermediate checkpoints may occur as well, due to the
   114   by the Isar top-level, cf.\ \secref{sec:isar-toplevel}.
   112   history mechanism provided by the Isar top-level, cf.\
       
   113   \secref{sec:isar-toplevel}.
   115 
   114 
   116   \begin{figure}[htb]
   115   \begin{figure}[htb]
   117   \begin{center}
   116   \begin{center}
   118   \begin{tabular}{rcccl}
   117   \begin{tabular}{rcccl}
   119         &            & @{text "Pure"} \\
   118         &            & @{text "Pure"} \\
   130         &            & $\vdots$~~ \\
   129         &            & $\vdots$~~ \\
   131         &            & @{text "\<bullet>"}~~ \\
   130         &            & @{text "\<bullet>"}~~ \\
   132         &            & $\vdots$~~ \\
   131         &            & $\vdots$~~ \\
   133         &            & \multicolumn{3}{l}{~~$\isarkeyword{end}$} \\
   132         &            & \multicolumn{3}{l}{~~$\isarkeyword{end}$} \\
   134   \end{tabular}
   133   \end{tabular}
   135   \caption{Theory definition depending on ancestors}\label{fig:ex-theory}
   134   \caption{A theory definition depending on ancestors}\label{fig:ex-theory}
   136   \end{center}
   135   \end{center}
   137   \end{figure}
   136   \end{figure}
       
   137 
       
   138   \medskip There is a separate notion of \emph{theory reference} for
       
   139   maintaining a live link to an evolving theory context: updates on
       
   140   drafts are propagated automatically.  The dynamic stops after an
       
   141   explicit @{text "end"} only.
       
   142 
       
   143   Derived entities may store a theory reference in order to indicate
       
   144   the context they belong to.  This implicitly assumes monotonic
       
   145   reasoning, because the referenced context may become larger without
       
   146   further notice.
   138 *}
   147 *}
   139 
   148 
   140 text %mlref {*
   149 text %mlref {*
   141   \begin{mldecls}
   150   \begin{mldecls}
   142   @{index_ML_type theory} \\
   151   @{index_ML_type theory} \\
   149   @{index_ML Theory.deref: "theory_ref -> theory"} \\
   158   @{index_ML Theory.deref: "theory_ref -> theory"} \\
   150   \end{mldecls}
   159   \end{mldecls}
   151 
   160 
   152   \begin{description}
   161   \begin{description}
   153 
   162 
   154   \item @{ML_type theory} represents theory contexts.  This is a
   163   \item @{ML_type theory} represents theory contexts.  This is
   155   linear type!  Most operations destroy the old version, which then
   164   essentially a linear type!  Most operations destroy the original
   156   becomes ``stale''.
   165   version, which then becomes ``stale''.
   157 
   166 
   158   \item @{ML "Theory.subthy"}~@{text "(thy\<^sub>1, thy\<^sub>2)"}
   167   \item @{ML "Theory.subthy"}~@{text "(thy\<^sub>1, thy\<^sub>2)"}
   159   compares theories according to the inherent graph structure of the
   168   compares theories according to the inherent graph structure of the
   160   construction.  This sub-theory relation is a nominal approximation
   169   construction.  This sub-theory relation is a nominal approximation
   161   of inclusion (@{text "\<subseteq>"}) of the corresponding content.
   170   of inclusion (@{text "\<subseteq>"}) of the corresponding content.
   167   \item @{ML "Theory.checkpoint"}~@{text "thy"} produces a safe
   176   \item @{ML "Theory.checkpoint"}~@{text "thy"} produces a safe
   168   stepping stone in the linear development of @{text "thy"}.  The next
   177   stepping stone in the linear development of @{text "thy"}.  The next
   169   update will result in two related, valid theories.
   178   update will result in two related, valid theories.
   170 
   179 
   171   \item @{ML "Theory.copy"}~@{text "thy"} produces a variant of @{text
   180   \item @{ML "Theory.copy"}~@{text "thy"} produces a variant of @{text
   172   "thy"} that holds a copy of the same data.  The copy is not related
   181   "thy"} that holds a copy of the same data.  The result is not
   173   to the original, which is not touched at all.
   182   related to the original; the original is unchanched.
   174 
   183 
   175   \item @{ML_type theory_ref} represents a sliding reference to a
   184   \item @{ML_type theory_ref} represents a sliding reference to an
   176   valid theory --- updates on the original are propagated
   185   always valid theory; updates on the original are propagated
   177   automatically.
   186   automatically.
   178 
   187 
   179   \item @{ML "Theory.self_ref"}~@{text "thy"} and @{ML
   188   \item @{ML "Theory.self_ref"}~@{text "thy"} and @{ML
   180   "Theory.deref"}~@{text "thy_ref"} convert between @{ML_type
   189   "Theory.deref"}~@{text "thy_ref"} convert between @{ML_type
   181   "theory"} and @{ML_type "theory_ref"}.  As the referenced theory
   190   "theory"} and @{ML_type "theory_ref"}.  As the referenced theory
   182   evolves monotonically over time, later invocations of @{ML
   191   evolves monotonically over time, later invocations of @{ML
   183   "Theory.deref"} may refer to larger contexts.
   192   "Theory.deref"} may refer to a larger context.
   184 
   193 
   185   \end{description}
   194   \end{description}
   186 *}
   195 *}
   187 
   196 
   188 
   197 
   196   generic notion of introducing and discharging hypotheses.
   205   generic notion of introducing and discharging hypotheses.
   197   Arbritrary auxiliary context data may be adjoined.}
   206   Arbritrary auxiliary context data may be adjoined.}
   198 
   207 
   199   A proof context is a container for pure data with a back-reference
   208   A proof context is a container for pure data with a back-reference
   200   to the theory it belongs to.  The @{text "init"} operation creates a
   209   to the theory it belongs to.  The @{text "init"} operation creates a
   201   proof context derived from a given theory.  Modifications to draft
   210   proof context from a given theory.  Modifications to draft theories
   202   theories are propagated to the proof context as usual, but there is
   211   are propagated to the proof context as usual, but there is also an
   203   also an explicit @{text "transfer"} operation to force
   212   explicit @{text "transfer"} operation to force resynchronization
   204   resynchronization with more substantial updates to the underlying
   213   with more substantial updates to the underlying theory.  The actual
   205   theory.  The actual context data does not require any special
   214   context data does not require any special bookkeeping, thanks to the
   206   bookkeeping, thanks to the lack of destructive features.
   215   lack of destructive features.
   207 
   216 
   208   Entities derived in a proof context need to record inherent logical
   217   Entities derived in a proof context need to record inherent logical
   209   requirements explicitly, since there is no separate context
   218   requirements explicitly, since there is no separate context
   210   identification as for theories.  For example, hypotheses used in
   219   identification as for theories.  For example, hypotheses used in
   211   primitive derivations (cf.\ \secref{sec:thm}) are recorded
   220   primitive derivations (cf.\ \secref{sec:thms}) are recorded
   212   separately within the sequent @{text "\<Gamma> \<turnstile> \<phi>"}, just to make double
   221   separately within the sequent @{text "\<Gamma> \<turnstile> \<phi>"}, just to make double
   213   sure.  Results could still leak into an alien proof context do to
   222   sure.  Results could still leak into an alien proof context do to
   214   programming errors, but Isabelle/Isar includes some extra validity
   223   programming errors, but Isabelle/Isar includes some extra validity
   215   checks in critical positions, notably at the end of sub-proof.
   224   checks in critical positions, notably at the end of sub-proof.
   216 
   225 
   217   Proof contexts may be produced in arbitrary ways, although the
   226   Proof contexts may be manipulated arbitrarily, although the common
   218   common discipline is to follow block structure as a mental model: a
   227   discipline is to follow block structure as a mental model: a given
   219   given context is extended consecutively, and results are exported
   228   context is extended consecutively, and results are exported back
   220   back into the original context.  Note that the Isar proof states
   229   into the original context.  Note that the Isar proof states model
   221   model block-structured reasoning explicitly, using a stack of proof
   230   block-structured reasoning explicitly, using a stack of proof
   222   contexts, cf.\ \secref{isar-proof-state}.
   231   contexts internally, cf.\ \secref{sec:isar-proof-state}.
   223 *}
   232 *}
   224 
   233 
   225 text %mlref {*
   234 text %mlref {*
   226   \begin{mldecls}
   235   \begin{mldecls}
   227   @{index_ML_type Proof.context} \\
   236   @{index_ML_type Proof.context} \\
   238 
   247 
   239   \item @{ML ProofContext.init}~@{text "thy"} produces a proof context
   248   \item @{ML ProofContext.init}~@{text "thy"} produces a proof context
   240   derived from @{text "thy"}, initializing all data.
   249   derived from @{text "thy"}, initializing all data.
   241 
   250 
   242   \item @{ML ProofContext.theory_of}~@{text "ctxt"} selects the
   251   \item @{ML ProofContext.theory_of}~@{text "ctxt"} selects the
   243   background theory from @{text "ctxt"}.
   252   background theory from @{text "ctxt"}, dereferencing its internal
       
   253   @{ML_type theory_ref}.
   244 
   254 
   245   \item @{ML ProofContext.transfer}~@{text "thy ctxt"} promotes the
   255   \item @{ML ProofContext.transfer}~@{text "thy ctxt"} promotes the
   246   background theory of @{text "ctxt"} to the super theory @{text
   256   background theory of @{text "ctxt"} to the super theory @{text
   247   "thy"}.
   257   "thy"}.
   248 
   258 
   249   \end{description}
   259   \end{description}
   250 *}
   260 *}
   251 
   261 
   252 
   262 
   253 
   263 subsection {* Generic contexts \label{sec:generic-context} *}
   254 subsection {* Generic contexts *}
       
   255 
   264 
   256 text {*
   265 text {*
   257   A generic context is the disjoint sum of either a theory or proof
   266   A generic context is the disjoint sum of either a theory or proof
   258   context.  Occasionally, this simplifies uniform treatment of generic
   267   context.  Occasionally, this enables uniform treatment of generic
   259   context data, typically extra-logical information.  Operations on
   268   context data, typically extra-logical information.  Operations on
   260   generic contexts include the usual injections, partial selections,
   269   generic contexts include the usual injections, partial selections,
   261   and combinators for lifting operations on either component of the
   270   and combinators for lifting operations on either component of the
   262   disjoint sum.
   271   disjoint sum.
   263 
   272 
   264   Moreover, there are total operations @{text "theory_of"} and @{text
   273   Moreover, there are total operations @{text "theory_of"} and @{text
   265   "proof_of"} to convert a generic context into either kind: a theory
   274   "proof_of"} to convert a generic context into either kind: a theory
   266   can always be selected, while a proof context may have to be
   275   can always be selected from the sum, while a proof context might
   267   constructed by an ad-hoc @{text "init"} operation.
   276   have to be constructed by an ad-hoc @{text "init"} operation.
   268 *}
   277 *}
   269 
   278 
   270 text %mlref {*
   279 text %mlref {*
   271   \begin{mldecls}
   280   \begin{mldecls}
   272   @{index_ML_type Context.generic} \\
   281   @{index_ML_type Context.generic} \\
   275   \end{mldecls}
   284   \end{mldecls}
   276 
   285 
   277   \begin{description}
   286   \begin{description}
   278 
   287 
   279   \item @{ML_type Context.generic} is the direct sum of @{ML_type
   288   \item @{ML_type Context.generic} is the direct sum of @{ML_type
   280   "theory"} and @{ML_type "Proof.context"}, with datatype constructors
   289   "theory"} and @{ML_type "Proof.context"}, with the datatype
   281   @{ML "Context.Theory"} and @{ML "Context.Proof"}.
   290   constructors @{ML "Context.Theory"} and @{ML "Context.Proof"}.
   282 
   291 
   283   \item @{ML Context.theory_of}~@{text "context"} always produces a
   292   \item @{ML Context.theory_of}~@{text "context"} always produces a
   284   theory from the generic @{text "context"}, using @{ML
   293   theory from the generic @{text "context"}, using @{ML
   285   "ProofContext.theory_of"} as required.
   294   "ProofContext.theory_of"} as required.
   286 
   295 
   287   \item @{ML Context.proof_of}~@{text "context"} always produces a
   296   \item @{ML Context.proof_of}~@{text "context"} always produces a
   288   proof context from the generic @{text "context"}, using @{ML
   297   proof context from the generic @{text "context"}, using @{ML
   289   "ProofContext.init"} as required.  Note that this re-initializes the
   298   "ProofContext.init"} as required (note that this re-initializes the
   290   context data with each invocation.
   299   context data with each invocation).
   291 
   300 
   292   \end{description}
   301   \end{description}
   293 *}
   302 *}
   294 
   303 
   295 subsection {* Context data *}
   304 subsection {* Context data *}
   296 
   305 
   297 text {*
   306 text {*
   298   Both theory and proof contexts manage arbitrary data, which is the
   307   The main purpose of theory and proof contexts is to manage arbitrary
   299   main purpose of contexts in the first place.  Data can be declared
   308   data.  New data types can be declared incrementally at compile time.
   300   incrementally at compile --- Isabelle/Pure and major object-logics
   309   There are separate declaration mechanisms for any of the three kinds
   301   are bootstrapped that way.
   310   of contexts: theory, proof, generic.
   302 
   311 
   303   \paragraph{Theory data} may refer to destructive entities, which are
   312   \paragraph{Theory data} may refer to destructive entities, which are
   304   maintained in correspondence to the linear evolution of theory
   313   maintained in direct correspondence to the linear evolution of
   305   values, or explicit copies.\footnote{Most existing instances of
   314   theory values, including explicit copies.\footnote{Most existing
   306   destructive theory data are merely historical relics (e.g.\ the
   315   instances of destructive theory data are merely historical relics
   307   destructive theorem storage, and destructive hints for the
   316   (e.g.\ the destructive theorem storage, and destructive hints for
   308   Simplifier and Classical rules).}  A theory data declaration needs
   317   the Simplifier and Classical rules).}  A theory data declaration
   309   to implement the following specification:
   318   needs to implement the following specification (depending on type
       
   319   @{text "T"}):
   310 
   320 
   311   \medskip
   321   \medskip
   312   \begin{tabular}{ll}
   322   \begin{tabular}{ll}
   313   @{text "name: string"} \\
   323   @{text "name: string"} \\
   314   @{text "T"} & the ML type \\
       
   315   @{text "empty: T"} & initial value \\
   324   @{text "empty: T"} & initial value \\
   316   @{text "copy: T \<rightarrow> T"} & refresh impure data \\
   325   @{text "copy: T \<rightarrow> T"} & refresh impure data \\
   317   @{text "extend: T \<rightarrow> T"} & re-initialize on import \\
   326   @{text "extend: T \<rightarrow> T"} & re-initialize on import \\
   318   @{text "merge: T \<times> T \<rightarrow> T"} & join on import \\
   327   @{text "merge: T \<times> T \<rightarrow> T"} & join on import \\
   319   @{text "print: T \<rightarrow> unit"} & diagnostic output \\
   328   @{text "print: T \<rightarrow> unit"} & diagnostic output \\
   324   messages; @{text "copy"} is just the identity for pure data; @{text
   333   messages; @{text "copy"} is just the identity for pure data; @{text
   325   "extend"} is acts like a unitary version of @{text "merge"}, both
   334   "extend"} is acts like a unitary version of @{text "merge"}, both
   326   should also include the functionality of @{text "copy"} for impure
   335   should also include the functionality of @{text "copy"} for impure
   327   data.
   336   data.
   328 
   337 
   329   \paragraph{Proof context data} is purely functional.  It is declared
   338   \paragraph{Proof context data} is purely functional.  A declaration
   330   by implementing the following specification:
   339   needs to implement the following specification:
   331 
   340 
   332   \medskip
   341   \medskip
   333   \begin{tabular}{ll}
   342   \begin{tabular}{ll}
   334   @{text "name: string"} \\
   343   @{text "name: string"} \\
   335   @{text "T"} & the ML type \\
       
   336   @{text "init: theory \<rightarrow> T"} & produce initial value \\
   344   @{text "init: theory \<rightarrow> T"} & produce initial value \\
   337   @{text "print: T \<rightarrow> unit"} & diagnostic output \\
   345   @{text "print: T \<rightarrow> unit"} & diagnostic output \\
   338   \end{tabular}
   346   \end{tabular}
   339   \medskip
   347   \medskip
   340 
   348 
   341   \noindent The @{text "init"} operation is supposed to produce a pure
   349   \noindent The @{text "init"} operation is supposed to produce a pure
   342   value from the given background theory.  The rest is analogous to
   350   value from the given background theory.  The remainder is analogous
   343   (pure) theory data.
   351   to theory data.
   344 
   352 
   345   \paragraph{Generic data} provides a hybrid interface for both kinds.
   353   \paragraph{Generic data} provides a hybrid interface for both theory
   346   The declaration is essentially the same as for pure theory data,
   354   and proof data.  The declaration is essentially the same as for
   347   without @{text "copy"} (it is always the identity).  The @{text
   355   (pure) theory data, without @{text "copy"}, though.  The @{text
   348   "init"} operation for proof contexts selects the current data value
   356   "init"} operation for proof contexts merely selects the current data
   349   from the background theory.
   357   value from the background theory.
   350 
   358 
   351   \bigskip In any case, a data declaration of type @{text "T"} results
   359   \bigskip In any case, a data declaration of type @{text "T"} results
   352   in the following interface:
   360   in the following interface:
   353 
   361 
   354   \medskip
   362   \medskip
   364   \noindent Here @{text "init"} needs to be applied to the current
   372   \noindent Here @{text "init"} needs to be applied to the current
   365   theory context once, in order to register the initial setup.  The
   373   theory context once, in order to register the initial setup.  The
   366   other operations provide access for the particular kind of context
   374   other operations provide access for the particular kind of context
   367   (theory, proof, or generic context).  Note that this is a safe
   375   (theory, proof, or generic context).  Note that this is a safe
   368   interface: there is no other way to access the corresponding data
   376   interface: there is no other way to access the corresponding data
   369   slot within a context.  By keeping these operations private, a
   377   slot of a context.  By keeping these operations private, a component
   370   component may maintain abstract values authentically, without other
   378   may maintain abstract values authentically, without other components
   371   components interfering.
   379   interfering.
   372 *}
   380 *}
   373 
   381 
   374 text %mlref {*
   382 text %mlref {*
   375   \begin{mldecls}
   383   \begin{mldecls}
   376   @{index_ML_functor TheoryDataFun} \\
   384   @{index_ML_functor TheoryDataFun} \\
   380 
   388 
   381   \begin{description}
   389   \begin{description}
   382 
   390 
   383   \item @{ML_functor TheoryDataFun}@{text "(spec)"} declares data for
   391   \item @{ML_functor TheoryDataFun}@{text "(spec)"} declares data for
   384   type @{ML_type theory} according to the specification provided as
   392   type @{ML_type theory} according to the specification provided as
   385   argument structure.  The result structure provides init and access
   393   argument structure.  The resulting structure provides data init and
   386   operations as described above.
   394   access operations as described above.
   387 
   395 
   388   \item @{ML_functor ProofDataFun}@{text "(spec)"} is analogous for
   396   \item @{ML_functor ProofDataFun}@{text "(spec)"} is analogous for
   389   type @{ML_type Proof.context}.
   397   type @{ML_type Proof.context}.
   390 
   398 
   391   \item @{ML_functor GenericDataFun}@{text "(spec)"} is analogous for
   399   \item @{ML_functor GenericDataFun}@{text "(spec)"} is analogous for
   395 *}
   403 *}
   396 
   404 
   397 
   405 
   398 section {* Named entities *}
   406 section {* Named entities *}
   399 
   407 
   400 text {* Named entities of different kinds (logical constant, type,
   408 text {*
   401 type class, theorem, method etc.) live in separate name spaces.  It is
   409   By general convention, each kind of formal entities (logical
   402 usually clear from the occurrence of a name which kind of entity it
   410   constant, type, type class, theorem, method etc.) lives in a
   403 refers to.  For example, proof method @{text "foo"} vs.\ theorem
   411   separate name space.  It is usually clear from the syntactic context
   404 @{text "foo"} vs.\ logical constant @{text "foo"} are easily
   412   of a name, which kind of entity it refers to.  For example, proof
   405 distinguished by means of the syntactic context.  A notable exception
   413   method @{text "foo"} vs.\ theorem @{text "foo"} vs.\ logical
   406 are logical identifiers within a term (\secref{sec:terms}): constants,
   414   constant @{text "foo"} are easily distinguished thanks to the design
   407 fixed variables, and bound variables all share the same identifier
   415   of the concrete outer syntax.  A notable exception are logical
   408 syntax, but are distinguished by their scope.
   416   identifiers within a term (\secref{sec:terms}): constants, fixed
   409 
   417   variables, and bound variables all share the same identifier syntax,
   410 Each name space is organized as a collection of \emph{qualified
   418   but are distinguished by their scope.
   411 names}, which consist of a sequence of basic name components separated
   419 
   412 by dots: @{text "Bar.bar.foo"}, @{text "Bar.foo"}, and @{text "foo"}
   420   Name spaces are organized uniformly, as a collection of qualified
   413 are examples for valid qualified names.  Name components are
   421   names consisting of a sequence of basic name components separated by
   414 subdivided into \emph{symbols}, which constitute the smallest textual
   422   dots: @{text "Bar.bar.foo"}, @{text "Bar.foo"}, and @{text "foo"}
   415 unit in Isabelle --- raw characters are normally not encountered
   423   are examples for qualified names.
   416 directly. *}
   424 
       
   425   Despite the independence of names of different kinds, certain naming
       
   426   conventions may relate them to each other.  For example, a constant
       
   427   @{text "foo"} could be accompanied with theorems @{text
       
   428   "foo.intro"}, @{text "foo.elim"}, @{text "foo.simps"} etc.  The same
       
   429   could happen for a type @{text "foo"}, but this is apt to cause
       
   430   clashes in the theorem name space!  To avoid this, there is an
       
   431   additional convention to add a suffix that determines the original
       
   432   kind.  For example, constant @{text "foo"} could associated with
       
   433   theorem @{text "foo.intro"}, type @{text "foo"} with theorem @{text
       
   434   "foo_type.intro"}, and type class @{text "foo"} with @{text
       
   435   "foo_class.intro"}.
       
   436 
       
   437   \medskip Name components are subdivided into \emph{symbols}, which
       
   438   constitute the smallest textual unit in Isabelle --- raw characters
       
   439   are normally not encountered.
       
   440 *}
   417 
   441 
   418 
   442 
   419 subsection {* Strings of symbols *}
   443 subsection {* Strings of symbols *}
   420 
   444 
   421 text {* Isabelle strings consist of a sequence of
   445 text {*
   422 symbols\glossary{Symbol}{The smallest unit of text in Isabelle,
   446   Isabelle strings consist of a sequence of
   423 subsumes plain ASCII characters as well as an infinite collection of
   447   symbols\glossary{Symbol}{The smallest unit of text in Isabelle,
   424 named symbols (for greek, math etc.).}, which are either packed as an
   448   subsumes plain ASCII characters as well as an infinite collection of
   425 actual @{text "string"}, or represented as a list.  Each symbol is in
   449   named symbols (for greek, math etc.).}, which are either packed as
   426 itself a small string of the following form:
   450   an actual @{text "string"}, or represented as a list.  Each symbol
   427 
   451   is in itself a small string of the following form:
   428 \begin{enumerate}
   452 
   429 
   453   \begin{enumerate}
   430 \item either a singleton ASCII character ``@{text "c"}'' (with
   454 
   431 character code 0--127), for example ``\verb,a,'',
   455   \item either a singleton ASCII character ``@{text "c"}'' (with
   432 
   456   character code 0--127), for example ``\verb,a,'',
   433 \item or a regular symbol ``\verb,\,\verb,<,@{text "ident"}\verb,>,'',
   457 
   434 for example ``\verb,\,\verb,<alpha>,'',
   458   \item or a regular symbol ``\verb,\,\verb,<,@{text
   435 
   459   "ident"}\verb,>,'', for example ``\verb,\,\verb,<alpha>,'',
   436 \item or a control symbol ``\verb,\,\verb,<^,@{text
   460 
   437 "ident"}\verb,>,'', for example ``\verb,\,\verb,<^bold>,'',
   461   \item or a control symbol ``\verb,\,\verb,<^,@{text
   438 
   462   "ident"}\verb,>,'', for example ``\verb,\,\verb,<^bold>,'',
   439 \item or a raw control symbol ``\verb,\,\verb,<^raw:,@{text
   463 
   440 "\<dots>"}\verb,>,'' where ``@{text "\<dots>"}'' refers to any
   464   \item or a raw control symbol ``\verb,\,\verb,<^raw:,@{text
   441 printable ASCII character (excluding ``\verb,.,'' and ``\verb,>,'') or
   465   "\<dots>"}\verb,>,'' where ``@{text "\<dots>"}'' refers to any printable ASCII
   442 non-ASCII character, for example ``\verb,\,\verb,<^raw:$\sum_{i = 1}^n$>,'',
   466   character (excluding ``\verb,.,'' and ``\verb,>,'') or non-ASCII
   443 
   467   character, for example ``\verb,\,\verb,<^raw:$\sum_{i = 1}^n$>,'',
   444 \item or a numbered raw control symbol ``\verb,\,\verb,<^raw,@{text
   468 
   445 "nnn"}\verb,>, where @{text "nnn"} are digits, for example
   469   \item or a numbered raw control symbol ``\verb,\,\verb,<^raw,@{text
   446 ``\verb,\,\verb,<^raw42>,''.
   470   "nnn"}\verb,>, where @{text "nnn"} are digits, for example
   447 
   471   ``\verb,\,\verb,<^raw42>,''.
   448 \end{enumerate}
   472 
   449 
   473   \end{enumerate}
   450 The @{text "ident"} syntax for symbol names is @{text "letter (letter
   474 
   451 | digit)\<^sup>*"}, where @{text "letter = A..Za..Z"} and @{text
   475   The @{text "ident"} syntax for symbol names is @{text "letter
   452 "digit = 0..9"}.  There are infinitely many regular symbols and
   476   (letter | digit)\<^sup>*"}, where @{text "letter = A..Za..z"} and
   453 control symbols available, but a certain collection of standard
   477   @{text "digit = 0..9"}.  There are infinitely many regular symbols
   454 symbols is treated specifically.  For example,
   478   and control symbols available, but a certain collection of standard
   455 ``\verb,\,\verb,<alpha>,'' is classified as a (non-ASCII) letter,
   479   symbols is treated specifically.  For example,
   456 which means it may occur within regular Isabelle identifier syntax.
   480   ``\verb,\,\verb,<alpha>,'' is classified as a (non-ASCII) letter,
   457 
   481   which means it may occur within regular Isabelle identifier syntax.
   458 Output of symbols depends on the print mode (\secref{sec:print-mode}).
   482 
   459 For example, the standard {\LaTeX} setup of the Isabelle document
   483   Output of symbols depends on the print mode
   460 preparation system would present ``\verb,\,\verb,<alpha>,'' as @{text
   484   (\secref{sec:print-mode}).  For example, the standard {\LaTeX} setup
   461 "\<alpha>"}, and ``\verb,\,\verb,<^bold>,\verb,\,\verb,<alpha>,'' as @{text
   485   of the Isabelle document preparation system would present
   462 "\<^bold>\<alpha>"}.
   486   ``\verb,\,\verb,<alpha>,'' as @{text "\<alpha>"}, and
   463 
   487   ``\verb,\,\verb,<^bold>,\verb,\,\verb,<alpha>,'' as @{text
   464 \medskip It is important to note that the character set underlying
   488   "\<^bold>\<alpha>"}.
   465 Isabelle symbols is plain 7-bit ASCII.  Since 8-bit characters are
   489 
   466 passed through transparently, Isabelle may easily process actual
   490   \medskip It is important to note that the character set underlying
   467 Unicode/UCS data (using the well-known UTF-8 encoding, for example).
   491   Isabelle symbols is plain 7-bit ASCII.  Since 8-bit characters are
   468 Unicode provides its own collection of mathematical symbols, but there
   492   passed through transparently, Isabelle may easily process
   469 is presently no link to Isabelle's named ones; both kinds of symbols
   493   Unicode/UCS data as well (using UTF-8 encoding).  Unicode provides
   470 coexist independently. *}
   494   its own collection of mathematical symbols, but there is no built-in
       
   495   link to the ones of Isabelle.
       
   496 *}
   471 
   497 
   472 text %mlref {*
   498 text %mlref {*
   473   \begin{mldecls}
   499   \begin{mldecls}
   474   @{index_ML_type "Symbol.symbol"} \\
   500   @{index_ML_type "Symbol.symbol"} \\
   475   @{index_ML Symbol.explode: "string -> Symbol.symbol list"} \\
   501   @{index_ML Symbol.explode: "string -> Symbol.symbol list"} \\
   476   @{index_ML Symbol.is_letter: "Symbol.symbol -> bool"} \\
   502   @{index_ML Symbol.is_letter: "Symbol.symbol -> bool"} \\
   477   @{index_ML Symbol.is_digit: "Symbol.symbol -> bool"} \\
   503   @{index_ML Symbol.is_digit: "Symbol.symbol -> bool"} \\
   478   @{index_ML Symbol.is_quasi: "Symbol.symbol -> bool"} \\
   504   @{index_ML Symbol.is_quasi: "Symbol.symbol -> bool"} \\
   479   @{index_ML Symbol.is_blank: "Symbol.symbol -> bool"} \\
   505   @{index_ML Symbol.is_blank: "Symbol.symbol -> bool"} \\[1ex]
   480   @{index_ML_type "Symbol.sym"} \\
   506   @{index_ML_type "Symbol.sym"} \\
   481   @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
   507   @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
   482   \end{mldecls}
   508   \end{mldecls}
   483 
   509 
   484   \begin{description}
   510   \begin{description}
   485 
   511 
   486   \item @{ML_type "Symbol.symbol"} represents Isabelle symbols; this type
   512   \item @{ML_type "Symbol.symbol"} represents Isabelle symbols.  This
   487   is merely an alias for @{ML_type "string"}, but emphasizes the
   513   type is an alias for @{ML_type "string"}, but emphasizes the
   488   specific format encountered here.
   514   specific format encountered here.
   489 
   515 
   490   \item @{ML "Symbol.explode"}~@{text "s"} produces a symbol list from
   516   \item @{ML "Symbol.explode"}~@{text "s"} produces a symbol list from
   491   the packed form usually encountered as user input.  This function
   517   the packed form that is encountered in most practical situations.
   492   replaces @{ML "String.explode"} for virtually all purposes of
   518   This function supercedes @{ML "String.explode"} for virtually all
   493   manipulating text in Isabelle!  Plain @{ML "implode"} may be used
   519   purposes of manipulating text in Isabelle!  Plain @{ML "implode"}
   494   for the reverse operation.
   520   may still be used for the reverse operation.
   495 
   521 
   496   \item @{ML "Symbol.is_letter"}, @{ML "Symbol.is_digit"}, @{ML
   522   \item @{ML "Symbol.is_letter"}, @{ML "Symbol.is_digit"}, @{ML
   497   "Symbol.is_quasi"}, @{ML "Symbol.is_blank"} classify certain symbols
   523   "Symbol.is_quasi"}, @{ML "Symbol.is_blank"} classify certain symbols
   498   (both ASCII and several named ones) according to fixed syntactic
   524   (both ASCII and several named ones) according to fixed syntactic
   499   convections of Isabelle, e.g.\ see \cite{isabelle-isar-ref}.
   525   conventions of Isabelle, cf.\ \cite{isabelle-isar-ref}.
   500 
   526 
   501   \item @{ML_type "Symbol.sym"} is a concrete datatype that represents
   527   \item @{ML_type "Symbol.sym"} is a concrete datatype that represents
   502   the different kinds of symbols explicitly as @{ML "Symbol.Char"},
   528   the different kinds of symbols explicitly with constructors @{ML
   503   @{ML "Symbol.Sym"}, @{ML "Symbol.Ctrl"}, or @{ML "Symbol.Raw"}.
   529   "Symbol.Char"}, @{ML "Symbol.Sym"}, @{ML "Symbol.Ctrl"}, or @{ML
       
   530   "Symbol.Raw"}.
   504 
   531 
   505   \item @{ML "Symbol.decode"} converts the string representation of a
   532   \item @{ML "Symbol.decode"} converts the string representation of a
   506   symbol into the explicit datatype version.
   533   symbol into the datatype version.
   507 
   534 
   508   \end{description}
   535   \end{description}
   509 *}
   536 *}
   510 
   537 
   511 
   538 
   512 subsection {* Qualified names and name spaces *}
   539 subsection {* Qualified names and name spaces *}
   513 
   540 
   514 text {*
   541 text {*
       
   542   A \emph{qualified name} essentially consists of a non-empty list of
       
   543   basic name components.  The packad notation uses a dot as separator,
       
   544   as in @{text "A.b"}, for example.  The very last component is called
       
   545   \emph{base} name, the remaining prefix \emph{qualifier} (which may
       
   546   be empty).
       
   547 
       
   548   A @{text "naming"} policy tells how to produce fully qualified names
       
   549   from a given specification.  The @{text "full"} operation applies
       
   550   performs naming of a name; the policy is usually taken from the
       
   551   context.  For example, a common policy is to attach an implicit
       
   552   prefix.
       
   553 
       
   554   A @{text "name space"} manages declarations of fully qualified
       
   555   names.  There are separate operations to @{text "declare"}, @{text
       
   556   "intern"}, and @{text "extern"} names.
       
   557 
   515   FIXME
   558   FIXME
   516 
   559 *}
   517   Qualified names are constructed according to implicit naming
   560 
   518   principles of the present context.
   561 text %mlref FIXME
   519 
       
   520 
       
   521   The last component is called \emph{base name}; the remaining prefix
       
   522   of qualification may be empty.
       
   523 
       
   524   Some practical conventions help to organize named entities more
       
   525   systematically:
       
   526 
       
   527   \begin{itemize}
       
   528 
       
   529   \item Names are qualified first by the theory name, second by an
       
   530   optional ``structure''.  For example, a constant @{text "c"}
       
   531   declared as part of a certain structure @{text "b"} (say a type
       
   532   definition) in theory @{text "A"} will be named @{text "A.b.c"}
       
   533   internally.
       
   534 
       
   535   \item
       
   536 
       
   537   \item
       
   538 
       
   539   \item
       
   540 
       
   541   \item
       
   542 
       
   543   \end{itemize}
       
   544 
       
   545   Names of different kinds of entities are basically independent, but
       
   546   some practical naming conventions relate them to each other.  For
       
   547   example, a constant @{text "foo"} may be accompanied with theorems
       
   548   @{text "foo.intro"}, @{text "foo.elim"}, @{text "foo.simps"} etc.
       
   549   The same may happen for a type @{text "foo"}, which is then apt to
       
   550   cause clashes in the theorem name space!  To avoid this, we
       
   551   occasionally follow an additional convention of suffixes that
       
   552   determine the original kind of entity that a name has been derived.
       
   553   For example, constant @{text "foo"} is associated with theorem
       
   554   @{text "foo.intro"}, type @{text "foo"} with theorem @{text
       
   555   "foo_type.intro"}, and type class @{text "foo"} with @{text
       
   556   "foo_class.intro"}.
       
   557 *}
       
   558 
   562 
   559 
   563 
   560 section {* Structured output *}
   564 section {* Structured output *}
   561 
   565 
   562 subsection {* Pretty printing *}
   566 subsection {* Pretty printing *}
   565 
   569 
   566 subsection {* Output channels *}
   570 subsection {* Output channels *}
   567 
   571 
   568 text FIXME
   572 text FIXME
   569 
   573 
   570 subsection {* Print modes *}
   574 subsection {* Print modes \label{sec:print-mode} *}
   571 
   575 
   572 text FIXME
   576 text FIXME
   573 
   577 
   574 
   578 
   575 end
   579 end