doc-src/IsarImplementation/Thy/Prelim.thy
author wenzelm
Sun, 31 Jan 2010 21:40:44 +0100
changeset 34925 38a44d813a3c
parent 34924 520727474bbe
child 34926 19294b07e445
permissions -rw-r--r--
more details on Isabelle symbols;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29755
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 29581
diff changeset
     1
theory Prelim
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 29581
diff changeset
     2
imports Base
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 29581
diff changeset
     3
begin
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     4
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     5
chapter {* Preliminaries *}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     6
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
     7
section {* Contexts \label{sec:context} *}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     8
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
     9
text {*
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    10
  A logical context represents the background that is required for
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    11
  formulating statements and composing proofs.  It acts as a medium to
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    12
  produce formal content, depending on earlier material (declarations,
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    13
  results etc.).
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    14
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    15
  For example, derivations within the Isabelle/Pure logic can be
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    16
  described as a judgment @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<phi>"}, which means that a
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    17
  proposition @{text "\<phi>"} is derivable from hypotheses @{text "\<Gamma>"}
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    18
  within the theory @{text "\<Theta>"}.  There are logical reasons for
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    19
  keeping @{text "\<Theta>"} and @{text "\<Gamma>"} separate: theories can be
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    20
  liberal about supporting type constructors and schematic
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    21
  polymorphism of constants and axioms, while the inner calculus of
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    22
  @{text "\<Gamma> \<turnstile> \<phi>"} is strictly limited to Simple Type Theory (with
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    23
  fixed type variables in the assumptions).
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    24
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    25
  \medskip Contexts and derivations are linked by the following key
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    26
  principles:
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    27
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    28
  \begin{itemize}
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    29
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    30
  \item Transfer: monotonicity of derivations admits results to be
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    31
  transferred into a \emph{larger} context, i.e.\ @{text "\<Gamma> \<turnstile>\<^sub>\<Theta>
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    32
  \<phi>"} implies @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta>\<^sub>' \<phi>"} for contexts @{text "\<Theta>'
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    33
  \<supseteq> \<Theta>"} and @{text "\<Gamma>' \<supseteq> \<Gamma>"}.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    34
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    35
  \item Export: discharge of hypotheses admits results to be exported
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    36
  into a \emph{smaller} context, i.e.\ @{text "\<Gamma>' \<turnstile>\<^sub>\<Theta> \<phi>"}
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    37
  implies @{text "\<Gamma> \<turnstile>\<^sub>\<Theta> \<Delta> \<Longrightarrow> \<phi>"} where @{text "\<Gamma>' \<supseteq> \<Gamma>"} and
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    38
  @{text "\<Delta> = \<Gamma>' - \<Gamma>"}.  Note that @{text "\<Theta>"} remains unchanged here,
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    39
  only the @{text "\<Gamma>"} part is affected.
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    40
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    41
  \end{itemize}
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    42
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    43
  \medskip By modeling the main characteristics of the primitive
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    44
  @{text "\<Theta>"} and @{text "\<Gamma>"} above, and abstracting over any
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    45
  particular logical content, we arrive at the fundamental notions of
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    46
  \emph{theory context} and \emph{proof context} in Isabelle/Isar.
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    47
  These implement a certain policy to manage arbitrary \emph{context
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    48
  data}.  There is a strongly-typed mechanism to declare new kinds of
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    49
  data at compile time.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    50
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    51
  The internal bootstrap process of Isabelle/Pure eventually reaches a
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    52
  stage where certain data slots provide the logical content of @{text
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    53
  "\<Theta>"} and @{text "\<Gamma>"} sketched above, but this does not stop there!
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    54
  Various additional data slots support all kinds of mechanisms that
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    55
  are not necessarily part of the core logic.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    56
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    57
  For example, there would be data for canonical introduction and
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    58
  elimination rules for arbitrary operators (depending on the
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    59
  object-logic and application), which enables users to perform
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    60
  standard proof steps implicitly (cf.\ the @{text "rule"} method
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    61
  \cite{isabelle-isar-ref}).
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    62
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    63
  \medskip Thus Isabelle/Isar is able to bring forth more and more
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    64
  concepts successively.  In particular, an object-logic like
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    65
  Isabelle/HOL continues the Isabelle/Pure setup by adding specific
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    66
  components for automated reasoning (classical reasoner, tableau
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    67
  prover, structured induction etc.) and derived specification
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    68
  mechanisms (inductive predicates, recursive functions etc.).  All of
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    69
  this is ultimately based on the generic data management by theory
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    70
  and proof contexts introduced here.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    71
*}
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
subsection {* Theory context \label{sec:context-theory} *}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    75
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    76
text {* A \emph{theory} is a data container with explicit name and
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    77
  unique identifier.  Theories are related by a (nominal) sub-theory
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    78
  relation, which corresponds to the dependency graph of the original
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
    79
  construction; each theory is derived from a certain sub-graph of
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    80
  ancestor theories.  To this end, the system maintains a set of
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    81
  symbolic ``identification stamps'' within each theory.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    82
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    83
  In order to avoid the full-scale overhead of explicit sub-theory
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    84
  identification of arbitrary intermediate stages, a theory is
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    85
  switched into @{text "draft"} mode under certain circumstances.  A
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    86
  draft theory acts like a linear type, where updates invalidate
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    87
  earlier versions.  An invalidated draft is called \emph{stale}.
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
    88
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    89
  The @{text "checkpoint"} operation produces a safe stepping stone
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    90
  that will survive the next update without becoming stale: both the
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    91
  old and the new theory remain valid and are related by the
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    92
  sub-theory relation.  Checkpointing essentially recovers purely
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    93
  functional theory values, at the expense of some extra internal
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
    94
  bookkeeping.
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
    95
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
    96
  The @{text "copy"} operation produces an auxiliary version that has
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
    97
  the same data content, but is unrelated to the original: updates of
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
    98
  the copy do not affect the original, neither does the sub-theory
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
    99
  relation hold.
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
   100
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   101
  The @{text "merge"} operation produces the least upper bound of two
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   102
  theories, which actually degenerates into absorption of one theory
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   103
  into the other (according to the nominal sub-theory relation).
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   104
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   105
  The @{text "begin"} operation starts a new theory by importing
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   106
  several parent theories and entering a special mode of nameless
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   107
  incremental updates, until the final @{text "end"} operation is
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   108
  performed.
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   109
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   110
  \medskip The example in \figref{fig:ex-theory} below shows a theory
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   111
  graph derived from @{text "Pure"}, with theory @{text "Length"}
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   112
  importing @{text "Nat"} and @{text "List"}.  The body of @{text
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   113
  "Length"} consists of a sequence of updates, working mostly on
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   114
  drafts internally, while transaction boundaries of Isar top-level
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   115
  commands (\secref{sec:isar-toplevel}) are guaranteed to be safe
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   116
  checkpoints.
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   117
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   118
  \begin{figure}[htb]
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   119
  \begin{center}
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
   120
  \begin{tabular}{rcccl}
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   121
        &            & @{text "Pure"} \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   122
        &            & @{text "\<down>"} \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   123
        &            & @{text "FOL"} \\
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   124
        & $\swarrow$ &              & $\searrow$ & \\
21852
wenzelm
parents: 20547
diff changeset
   125
  @{text "Nat"} &    &              &            & @{text "List"} \\
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   126
        & $\searrow$ &              & $\swarrow$ \\
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   127
        &            & @{text "Length"} \\
26864
1417e704d724 replaced macros by antiquotations;
wenzelm
parents: 24137
diff changeset
   128
        &            & \multicolumn{3}{l}{~~@{keyword "imports"}} \\
1417e704d724 replaced macros by antiquotations;
wenzelm
parents: 24137
diff changeset
   129
        &            & \multicolumn{3}{l}{~~@{keyword "begin"}} \\
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   130
        &            & $\vdots$~~ \\
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   131
        &            & @{text "\<bullet>"}~~ \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   132
        &            & $\vdots$~~ \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   133
        &            & @{text "\<bullet>"}~~ \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   134
        &            & $\vdots$~~ \\
26864
1417e704d724 replaced macros by antiquotations;
wenzelm
parents: 24137
diff changeset
   135
        &            & \multicolumn{3}{l}{~~@{command "end"}} \\
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
   136
  \end{tabular}
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   137
  \caption{A theory definition depending on ancestors}\label{fig:ex-theory}
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   138
  \end{center}
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   139
  \end{figure}
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   140
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   141
  \medskip There is a separate notion of \emph{theory reference} for
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   142
  maintaining a live link to an evolving theory context: updates on
20488
wenzelm
parents: 20479
diff changeset
   143
  drafts are propagated automatically.  Dynamic updating stops after
wenzelm
parents: 20479
diff changeset
   144
  an explicit @{text "end"} only.
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   145
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   146
  Derived entities may store a theory reference in order to indicate
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   147
  the context they belong to.  This implicitly assumes monotonic
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   148
  reasoning, because the referenced context may become larger without
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   149
  further notice.
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   150
*}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   151
20430
wenzelm
parents: 20429
diff changeset
   152
text %mlref {*
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   153
  \begin{mldecls}
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   154
  @{index_ML_type theory} \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   155
  @{index_ML Theory.subthy: "theory * theory -> bool"} \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   156
  @{index_ML Theory.checkpoint: "theory -> theory"} \\
20547
wenzelm
parents: 20530
diff changeset
   157
  @{index_ML Theory.copy: "theory -> theory"} \\
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   158
  @{index_ML Theory.merge: "theory * theory -> theory"} \\
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   159
  @{index_ML Theory.begin_theory: "string -> theory list -> theory"} \\
20547
wenzelm
parents: 20530
diff changeset
   160
  \end{mldecls}
wenzelm
parents: 20530
diff changeset
   161
  \begin{mldecls}
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   162
  @{index_ML_type theory_ref} \\
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   163
  @{index_ML Theory.deref: "theory_ref -> theory"} \\
24137
8d7896398147 replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents: 22869
diff changeset
   164
  @{index_ML Theory.check_thy: "theory -> theory_ref"} \\
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   165
  \end{mldecls}
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   166
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   167
  \begin{description}
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   168
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   169
  \item @{ML_type theory} represents theory contexts.  This is
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   170
  essentially a linear type, with explicit runtime checking!  Most
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   171
  internal theory operations destroy the original version, which then
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   172
  becomes ``stale''.
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   173
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   174
  \item @{ML "Theory.subthy"}~@{text "(thy\<^sub>1, thy\<^sub>2)"} compares theories
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   175
  according to the intrinsic graph structure of the construction.
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   176
  This sub-theory relation is a nominal approximation of inclusion
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   177
  (@{text "\<subseteq>"}) of the corresponding content (according to the
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   178
  semantics of the ML modules that implement the data).
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   179
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   180
  \item @{ML "Theory.checkpoint"}~@{text "thy"} produces a safe
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   181
  stepping stone in the linear development of @{text "thy"}.  This
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   182
  changes the old theory, but the next update will result in two
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   183
  related, valid theories.
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   184
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   185
  \item @{ML "Theory.copy"}~@{text "thy"} produces a variant of @{text
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   186
  "thy"} with the same data.  The copy is not related to the original,
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   187
  but the original is unchanged.
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   188
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   189
  \item @{ML "Theory.merge"}~@{text "(thy\<^sub>1, thy\<^sub>2)"} absorbs one theory
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   190
  into the other, without changing @{text "thy\<^sub>1"} or @{text "thy\<^sub>2"}.
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   191
  This version of ad-hoc theory merge fails for unrelated theories!
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   192
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   193
  \item @{ML "Theory.begin_theory"}~@{text "name parents"} constructs
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   194
  a new theory based on the given parents.  This {\ML} function is
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   195
  normally not invoked directly.
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   196
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   197
  \item @{ML_type theory_ref} represents a sliding reference to an
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   198
  always valid theory; updates on the original are propagated
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   199
  automatically.
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   200
24137
8d7896398147 replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents: 22869
diff changeset
   201
  \item @{ML "Theory.deref"}~@{text "thy_ref"} turns a @{ML_type
8d7896398147 replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents: 22869
diff changeset
   202
  "theory_ref"} into an @{ML_type "theory"} value.  As the referenced
8d7896398147 replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents: 22869
diff changeset
   203
  theory evolves monotonically over time, later invocations of @{ML
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   204
  "Theory.deref"} may refer to a larger context.
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   205
24137
8d7896398147 replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents: 22869
diff changeset
   206
  \item @{ML "Theory.check_thy"}~@{text "thy"} produces a @{ML_type
8d7896398147 replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents: 22869
diff changeset
   207
  "theory_ref"} from a valid @{ML_type "theory"} value.
8d7896398147 replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
wenzelm
parents: 22869
diff changeset
   208
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   209
  \end{description}
20430
wenzelm
parents: 20429
diff changeset
   210
*}
wenzelm
parents: 20429
diff changeset
   211
34924
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   212
text %mlex {*
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   213
  The following artificial example demonstrates theory
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   214
  data: we maintain a set of terms that are supposed to be wellformed
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   215
  wrt.\ the enclosing theory.  The public interface is as follows:
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   216
*}
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   217
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   218
ML {*
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   219
signature WELLFORMED_TERMS =
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   220
sig
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   221
  val get: theory -> term list
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   222
  val add: term -> theory -> theory
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   223
end;
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   224
*}
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   225
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   226
text {* \noindent The implementation uses private theory data
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   227
  internally, and only exposes an operation that involves explicit
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   228
  argument checking wrt.\ the given theory. *}
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   229
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   230
ML {*
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   231
structure Wellformed_Terms: WELLFORMED_TERMS =
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   232
struct
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   233
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   234
structure Terms = Theory_Data
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   235
(
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   236
  type T = term OrdList.T;
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   237
  val empty = [];
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   238
  val extend = I;
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   239
  fun merge (ts1, ts2) =
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   240
    OrdList.union TermOrd.fast_term_ord ts1 ts2;
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   241
)
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   242
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   243
val get = Terms.get;
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   244
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   245
fun add raw_t thy =
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   246
  let val t = Sign.cert_term thy raw_t
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   247
  in Terms.map (OrdList.insert TermOrd.fast_term_ord t) thy end;
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   248
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   249
end;
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   250
*}
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   251
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   252
text {* We use @{ML_type "term OrdList.T"} for reasonably efficient
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   253
  representation of a set of terms: all operations are linear in the
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   254
  number of stored elements.  Here we assume that our users do not
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   255
  care about the declaration order, since that data structure forces
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   256
  its own arrangement of elements.
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   257
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   258
  Observe how the @{verbatim merge} operation joins the data slots of
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   259
  the two constituents: @{ML OrdList.union} prevents duplication of
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   260
  common data from different branches, thus avoiding the danger of
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   261
  exponential blowup.  (Plain list append etc.\ must never be used for
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   262
  theory data merges.)
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   263
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   264
  \medskip Our intended invariant is achieved as follows:
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   265
  \begin{enumerate}
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   266
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   267
  \item @{ML Wellformed_Terms.add} only admits terms that have passed
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   268
  the @{ML Sign.cert_term} check of the given theory at that point.
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   269
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   270
  \item Wellformedness in the sense of @{ML Sign.cert_term} is
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   271
  monotonic wrt.\ the sub-theory relation.  So our data can move
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   272
  upwards in the hierarchy (via extension or merges), and maintain
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   273
  wellformedness without further checks.
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   274
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   275
  \end{enumerate}
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   276
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   277
  Note that all basic operations of the inference kernel (which
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   278
  includes @{ML Sign.cert_term}) observe this monotonicity principle,
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   279
  but other user-space tools don't.  For example, fully-featured
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   280
  type-inference via @{ML Syntax.check_term} (cf.\
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   281
  \secref{sec:term-check}) is not necessarily monotonic wrt.\ the
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   282
  background theory, since constraints of term constants can be
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   283
  strengthened by later declarations, for example.
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   284
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   285
  In most cases, user-space context data does not have to take such
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   286
  invariants too seriously.  The situation is different in the
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   287
  implementation of the inference kernel itself, which uses the very
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   288
  same data mechanisms for types, constants, axioms etc.
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   289
*}
520727474bbe theory data example;
wenzelm
parents: 34921
diff changeset
   290
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   291
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   292
subsection {* Proof context \label{sec:context-proof} *}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   293
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   294
text {* A proof context is a container for pure data with a
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   295
  back-reference to the theory it belongs to.  The @{text "init"}
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   296
  operation creates a proof context from a given theory.
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   297
  Modifications to draft theories are propagated to the proof context
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   298
  as usual, but there is also an explicit @{text "transfer"} operation
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   299
  to force resynchronization with more substantial updates to the
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   300
  underlying theory.
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
   301
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   302
  Entities derived in a proof context need to record logical
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   303
  requirements explicitly, since there is no separate context
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   304
  identification or symbolic inclusion as for theories.  For example,
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   305
  hypotheses used in primitive derivations (cf.\ \secref{sec:thms})
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   306
  are recorded separately within the sequent @{text "\<Gamma> \<turnstile> \<phi>"}, just to
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   307
  make double sure.  Results could still leak into an alien proof
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   308
  context due to programming errors, but Isabelle/Isar includes some
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   309
  extra validity checks in critical positions, notably at the end of a
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   310
  sub-proof.
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
   311
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   312
  Proof contexts may be manipulated arbitrarily, although the common
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   313
  discipline is to follow block structure as a mental model: a given
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   314
  context is extended consecutively, and results are exported back
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   315
  into the original context.  Note that an Isar proof state models
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   316
  block-structured reasoning explicitly, using a stack of proof
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   317
  contexts internally.  For various technical reasons, the background
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   318
  theory of an Isar proof state must not be changed while the proof is
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   319
  still under construction!
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   320
*}
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   321
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   322
text %mlref {*
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   323
  \begin{mldecls}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   324
  @{index_ML_type Proof.context} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   325
  @{index_ML ProofContext.init: "theory -> Proof.context"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   326
  @{index_ML ProofContext.theory_of: "Proof.context -> theory"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   327
  @{index_ML ProofContext.transfer: "theory -> Proof.context -> Proof.context"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   328
  \end{mldecls}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   329
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   330
  \begin{description}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   331
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   332
  \item @{ML_type Proof.context} represents proof contexts.  Elements
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   333
  of this type are essentially pure values, with a sliding reference
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   334
  to the background theory.
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   335
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   336
  \item @{ML ProofContext.init}~@{text "thy"} produces a proof context
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   337
  derived from @{text "thy"}, initializing all data.
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   338
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   339
  \item @{ML ProofContext.theory_of}~@{text "ctxt"} selects the
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   340
  background theory from @{text "ctxt"}, dereferencing its internal
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   341
  @{ML_type theory_ref}.
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   342
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   343
  \item @{ML ProofContext.transfer}~@{text "thy ctxt"} promotes the
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   344
  background theory of @{text "ctxt"} to the super theory @{text
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   345
  "thy"}.
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   346
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   347
  \end{description}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   348
*}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   349
20430
wenzelm
parents: 20429
diff changeset
   350
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   351
subsection {* Generic contexts \label{sec:generic-context} *}
20429
116255c9209b more on contexts;
wenzelm
parents: 20214
diff changeset
   352
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   353
text {*
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   354
  A generic context is the disjoint sum of either a theory or proof
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   355
  context.  Occasionally, this enables uniform treatment of generic
20450
wenzelm
parents: 20449
diff changeset
   356
  context data, typically extra-logical information.  Operations on
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   357
  generic contexts include the usual injections, partial selections,
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   358
  and combinators for lifting operations on either component of the
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   359
  disjoint sum.
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   360
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   361
  Moreover, there are total operations @{text "theory_of"} and @{text
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   362
  "proof_of"} to convert a generic context into either kind: a theory
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   363
  can always be selected from the sum, while a proof context might
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   364
  have to be constructed by an ad-hoc @{text "init"} operation, which
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   365
  incurs a small runtime overhead.
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   366
*}
20430
wenzelm
parents: 20429
diff changeset
   367
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   368
text %mlref {*
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   369
  \begin{mldecls}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   370
  @{index_ML_type Context.generic} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   371
  @{index_ML Context.theory_of: "Context.generic -> theory"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   372
  @{index_ML Context.proof_of: "Context.generic -> Proof.context"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   373
  \end{mldecls}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   374
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   375
  \begin{description}
20430
wenzelm
parents: 20429
diff changeset
   376
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   377
  \item @{ML_type Context.generic} is the direct sum of @{ML_type
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   378
  "theory"} and @{ML_type "Proof.context"}, with the datatype
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   379
  constructors @{ML "Context.Theory"} and @{ML "Context.Proof"}.
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   380
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   381
  \item @{ML Context.theory_of}~@{text "context"} always produces a
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   382
  theory from the generic @{text "context"}, using @{ML
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   383
  "ProofContext.theory_of"} as required.
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   384
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   385
  \item @{ML Context.proof_of}~@{text "context"} always produces a
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   386
  proof context from the generic @{text "context"}, using @{ML
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   387
  "ProofContext.init"} as required (note that this re-initializes the
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   388
  context data with each invocation).
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   389
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   390
  \end{description}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   391
*}
20437
wenzelm
parents: 20430
diff changeset
   392
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   393
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   394
subsection {* Context data \label{sec:context-data} *}
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   395
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   396
text {* The main purpose of theory and proof contexts is to manage
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   397
  arbitrary (pure) data.  New data types can be declared incrementally
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   398
  at compile time.  There are separate declaration mechanisms for any
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   399
  of the three kinds of contexts: theory, proof, generic.
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   400
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   401
  \paragraph{Theory data} declarations need to implement the following
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   402
  SML signature:
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   403
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   404
  \medskip
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   405
  \begin{tabular}{ll}
22869
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   406
  @{text "\<type> T"} & representing type \\
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   407
  @{text "\<val> empty: T"} & empty default value \\
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   408
  @{text "\<val> extend: T \<rightarrow> T"} & re-initialize on import \\
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   409
  @{text "\<val> merge: T \<times> T \<rightarrow> T"} & join on import \\
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   410
  \end{tabular}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   411
  \medskip
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   412
22869
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   413
  \noindent The @{text "empty"} value acts as initial default for
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   414
  \emph{any} theory that does not declare actual data content; @{text
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   415
  "extend"} is acts like a unitary version of @{text "merge"}.
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   416
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   417
  Implementing @{text "merge"} can be tricky.  The general idea is
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   418
  that @{text "merge (data\<^sub>1, data\<^sub>2)"} inserts those parts of @{text
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   419
  "data\<^sub>2"} into @{text "data\<^sub>1"} that are not yet present, while
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   420
  keeping the general order of things.  The @{ML Library.merge}
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   421
  function on plain lists may serve as canonical template.
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   422
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   423
  Particularly note that shared parts of the data must not be
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   424
  duplicated by naive concatenation, or a theory graph that is like a
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   425
  chain of diamonds would cause an exponential blowup!
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   426
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   427
  \paragraph{Proof context data} declarations need to implement the
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   428
  following SML signature:
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   429
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   430
  \medskip
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   431
  \begin{tabular}{ll}
22869
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   432
  @{text "\<type> T"} & representing type \\
9f915d44a666 simplified context data;
wenzelm
parents: 22438
diff changeset
   433
  @{text "\<val> init: theory \<rightarrow> T"} & produce initial value \\
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   434
  \end{tabular}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   435
  \medskip
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   436
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   437
  \noindent The @{text "init"} operation is supposed to produce a pure
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   438
  value from the given background theory and should be somehow
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   439
  ``immediate''.  Whenever a proof context is initialized, which
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   440
  happens frequently, the the system invokes the @{text "init"}
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   441
  operation of \emph{all} theory data slots ever declared.
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   442
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   443
  \paragraph{Generic data} provides a hybrid interface for both theory
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   444
  and proof data.  The @{text "init"} operation for proof contexts is
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   445
  predefined to select the current data value from the background
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   446
  theory.
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   447
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   448
  \bigskip Any of these data declaration over type @{text "T"} result
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   449
  in an ML structure with the following signature:
20449
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   450
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   451
  \medskip
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   452
  \begin{tabular}{ll}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   453
  @{text "get: context \<rightarrow> T"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   454
  @{text "put: T \<rightarrow> context \<rightarrow> context"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   455
  @{text "map: (T \<rightarrow> T) \<rightarrow> context \<rightarrow> context"} \\
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   456
  \end{tabular}
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   457
  \medskip
f8a7a8236c68 more stuff;
wenzelm
parents: 20447
diff changeset
   458
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   459
  \noindent These other operations provide exclusive access for the
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   460
  particular kind of context (theory, proof, or generic context).
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   461
  This interface fully observes the ML discipline for types and
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   462
  scopes: there is no other way to access the corresponding data slot
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   463
  of a context.  By keeping these operations private, an Isabelle/ML
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   464
  module may maintain abstract values authentically.
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   465
*}
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   466
20450
wenzelm
parents: 20449
diff changeset
   467
text %mlref {*
wenzelm
parents: 20449
diff changeset
   468
  \begin{mldecls}
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   469
  @{index_ML_functor Theory_Data} \\
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   470
  @{index_ML_functor Proof_Data} \\
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   471
  @{index_ML_functor Generic_Data} \\
20450
wenzelm
parents: 20449
diff changeset
   472
  \end{mldecls}
wenzelm
parents: 20449
diff changeset
   473
wenzelm
parents: 20449
diff changeset
   474
  \begin{description}
wenzelm
parents: 20449
diff changeset
   475
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   476
  \item @{ML_functor Theory_Data}@{text "(spec)"} declares data for
20450
wenzelm
parents: 20449
diff changeset
   477
  type @{ML_type theory} according to the specification provided as
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   478
  argument structure.  The resulting structure provides data init and
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   479
  access operations as described above.
20450
wenzelm
parents: 20449
diff changeset
   480
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   481
  \item @{ML_functor Proof_Data}@{text "(spec)"} is analogous to
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   482
  @{ML_functor Theory_Data} for type @{ML_type Proof.context}.
20450
wenzelm
parents: 20449
diff changeset
   483
33524
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   484
  \item @{ML_functor Generic_Data}@{text "(spec)"} is analogous to
a08e6c1cbc04 updated functor Theory_Data, Proof_Data, Generic_Data;
wenzelm
parents: 33174
diff changeset
   485
  @{ML_functor Theory_Data} for type @{ML_type Context.generic}.
20450
wenzelm
parents: 20449
diff changeset
   486
wenzelm
parents: 20449
diff changeset
   487
  \end{description}
wenzelm
parents: 20449
diff changeset
   488
*}
wenzelm
parents: 20449
diff changeset
   489
20447
5b75f1c4d7d6 more on contexts;
wenzelm
parents: 20437
diff changeset
   490
26872
336dfd860744 fixed some labels;
wenzelm
parents: 26864
diff changeset
   491
section {* Names \label{sec:names} *}
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   492
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   493
text {* In principle, a name is just a string, but there are various
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   494
  conventions for representing additional structure.  For example,
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   495
  ``@{text "Foo.bar.baz"}'' is considered as a qualified name
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   496
  consisting of three basic name components.  The individual
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   497
  constituents of a name may have further substructure, e.g.\ the
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   498
  string ``\verb,\,\verb,<alpha>,'' encodes as a single symbol.
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   499
*}
20437
wenzelm
parents: 20430
diff changeset
   500
wenzelm
parents: 20430
diff changeset
   501
wenzelm
parents: 20430
diff changeset
   502
subsection {* Strings of symbols *}
wenzelm
parents: 20430
diff changeset
   503
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   504
text {* A \emph{symbol} constitutes the smallest textual unit in
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   505
  Isabelle --- raw ML characters are normally not encountered at all!
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   506
  Isabelle strings consist of a sequence of symbols, represented as a
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   507
  packed string or an exploded list of strings.  Each symbol is in
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   508
  itself a small string, which has either one of the following forms:
20437
wenzelm
parents: 20430
diff changeset
   509
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   510
  \begin{enumerate}
20437
wenzelm
parents: 20430
diff changeset
   511
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   512
  \item a single ASCII character ``@{text "c"}'' or raw byte in the
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   513
  range of 128\dots 255, for example ``\verb,a,'',
20437
wenzelm
parents: 20430
diff changeset
   514
20488
wenzelm
parents: 20479
diff changeset
   515
  \item a regular symbol ``\verb,\,\verb,<,@{text "ident"}\verb,>,'',
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   516
  for example ``\verb,\,\verb,<alpha>,'',
20437
wenzelm
parents: 20430
diff changeset
   517
20488
wenzelm
parents: 20479
diff changeset
   518
  \item a control symbol ``\verb,\,\verb,<^,@{text "ident"}\verb,>,'',
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   519
  for example ``\verb,\,\verb,<^bold>,'',
20437
wenzelm
parents: 20430
diff changeset
   520
20488
wenzelm
parents: 20479
diff changeset
   521
  \item a raw symbol ``\verb,\,\verb,<^raw:,@{text text}\verb,>,''
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   522
  where @{text text} consists of printable characters excluding
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   523
  ``\verb,.,'' and ``\verb,>,'', for example
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   524
  ``\verb,\,\verb,<^raw:$\sum_{i = 1}^n$>,'',
20437
wenzelm
parents: 20430
diff changeset
   525
20488
wenzelm
parents: 20479
diff changeset
   526
  \item a numbered raw control symbol ``\verb,\,\verb,<^raw,@{text
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   527
  n}\verb,>, where @{text n} consists of digits, for example
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   528
  ``\verb,\,\verb,<^raw42>,''.
20437
wenzelm
parents: 20430
diff changeset
   529
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   530
  \end{enumerate}
20437
wenzelm
parents: 20430
diff changeset
   531
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   532
  \noindent The @{text "ident"} syntax for symbol names is @{text
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   533
  "letter (letter | digit)\<^sup>*"}, where @{text "letter =
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   534
  A..Za..z"} and @{text "digit = 0..9"}.  There are infinitely many
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   535
  regular symbols and control symbols, but a fixed collection of
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   536
  standard symbols is treated specifically.  For example,
20488
wenzelm
parents: 20479
diff changeset
   537
  ``\verb,\,\verb,<alpha>,'' is classified as a letter, which means it
wenzelm
parents: 20479
diff changeset
   538
  may occur within regular Isabelle identifiers.
20437
wenzelm
parents: 20430
diff changeset
   539
20488
wenzelm
parents: 20479
diff changeset
   540
  Since the character set underlying Isabelle symbols is 7-bit ASCII
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   541
  and 8-bit characters are passed through transparently, Isabelle can
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   542
  also process Unicode/UCS data in UTF-8 encoding.\footnote{When
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   543
  counting precise source positions internally, bytes in the range of
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   544
  128\dots 191 are ignored.  In UTF-8 encoding, this interval covers
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   545
  the additional trailer bytes, so Isabelle happens to count Unicode
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   546
  characters here, not bytes in memory.  In ISO-Latin encoding, the
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   547
  ignored range merely includes some extra punctuation characters that
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   548
  even have replacements within the standard collection of Isabelle
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   549
  symbols; the accented letters range is counted properly.} Unicode
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   550
  provides its own collection of mathematical symbols, but within the
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   551
  core Isabelle/ML world there is no link to the standard collection
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   552
  of Isabelle regular symbols.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   553
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   554
  \medskip Output of Isabelle symbols depends on the print mode
29758
7a3b5bbed313 removed rudiments of glossary;
wenzelm
parents: 29755
diff changeset
   555
  (\secref{print-mode}).  For example, the standard {\LaTeX} setup of
7a3b5bbed313 removed rudiments of glossary;
wenzelm
parents: 29755
diff changeset
   556
  the Isabelle document preparation system would present
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   557
  ``\verb,\,\verb,<alpha>,'' as @{text "\<alpha>"}, and
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   558
  ``\verb,\,\verb,<^bold>,\verb,\,\verb,<alpha>,'' as @{text
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   559
  "\<^bold>\<alpha>"}.  On-screen rendering usually works by mapping a finite
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   560
  subset of Isabelle symbols to suitable Unicode characters.
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   561
*}
20437
wenzelm
parents: 20430
diff changeset
   562
wenzelm
parents: 20430
diff changeset
   563
text %mlref {*
wenzelm
parents: 20430
diff changeset
   564
  \begin{mldecls}
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   565
  @{index_ML_type "Symbol.symbol": string} \\
20437
wenzelm
parents: 20430
diff changeset
   566
  @{index_ML Symbol.explode: "string -> Symbol.symbol list"} \\
wenzelm
parents: 20430
diff changeset
   567
  @{index_ML Symbol.is_letter: "Symbol.symbol -> bool"} \\
wenzelm
parents: 20430
diff changeset
   568
  @{index_ML Symbol.is_digit: "Symbol.symbol -> bool"} \\
wenzelm
parents: 20430
diff changeset
   569
  @{index_ML Symbol.is_quasi: "Symbol.symbol -> bool"} \\
20547
wenzelm
parents: 20530
diff changeset
   570
  @{index_ML Symbol.is_blank: "Symbol.symbol -> bool"} \\
wenzelm
parents: 20530
diff changeset
   571
  \end{mldecls}
wenzelm
parents: 20530
diff changeset
   572
  \begin{mldecls}
20437
wenzelm
parents: 20430
diff changeset
   573
  @{index_ML_type "Symbol.sym"} \\
wenzelm
parents: 20430
diff changeset
   574
  @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
wenzelm
parents: 20430
diff changeset
   575
  \end{mldecls}
wenzelm
parents: 20430
diff changeset
   576
wenzelm
parents: 20430
diff changeset
   577
  \begin{description}
wenzelm
parents: 20430
diff changeset
   578
20488
wenzelm
parents: 20479
diff changeset
   579
  \item @{ML_type "Symbol.symbol"} represents individual Isabelle
34921
008126f730a0 formal markup of type aliases;
wenzelm
parents: 33524
diff changeset
   580
  symbols.
20437
wenzelm
parents: 20430
diff changeset
   581
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   582
  \item @{ML "Symbol.explode"}~@{text "str"} produces a symbol list
20488
wenzelm
parents: 20479
diff changeset
   583
  from the packed form.  This function supercedes @{ML
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   584
  "String.explode"} for virtually all purposes of manipulating text in
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   585
  Isabelle!\footnote{The runtime overhead for exploded strings is
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   586
  mainly that of the list structure: individual symbols that happen to
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   587
  be a singleton string --- which is the most common case --- do not
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   588
  require extra memory in Poly/ML.}
20437
wenzelm
parents: 20430
diff changeset
   589
wenzelm
parents: 20430
diff changeset
   590
  \item @{ML "Symbol.is_letter"}, @{ML "Symbol.is_digit"}, @{ML
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   591
  "Symbol.is_quasi"}, @{ML "Symbol.is_blank"} classify standard
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   592
  symbols according to fixed syntactic conventions of Isabelle, cf.\
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   593
  \cite{isabelle-isar-ref}.
20437
wenzelm
parents: 20430
diff changeset
   594
wenzelm
parents: 20430
diff changeset
   595
  \item @{ML_type "Symbol.sym"} is a concrete datatype that represents
20488
wenzelm
parents: 20479
diff changeset
   596
  the different kinds of symbols explicitly, with constructors @{ML
wenzelm
parents: 20479
diff changeset
   597
  "Symbol.Char"}, @{ML "Symbol.Sym"}, @{ML "Symbol.Ctrl"}, @{ML
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   598
  "Symbol.Raw"}.
20437
wenzelm
parents: 20430
diff changeset
   599
wenzelm
parents: 20430
diff changeset
   600
  \item @{ML "Symbol.decode"} converts the string representation of a
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 20450
diff changeset
   601
  symbol into the datatype version.
20437
wenzelm
parents: 20430
diff changeset
   602
wenzelm
parents: 20430
diff changeset
   603
  \end{description}
34925
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   604
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   605
  \paragraph{Historical note.} In the original SML90 standard the
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   606
  primitive ML type @{ML_type char} did not exists, and the basic @{ML
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   607
  "explode: string -> string list"} operation would produce a list of
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   608
  singleton strings as in Isabelle/ML today.  When SML97 came out,
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   609
  Isabelle ignored its slightly anachronistic 8-bit characters, but
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   610
  the idea of exploding a string into a list of small strings was
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   611
  extended to ``symbols'' as explained above.  Thus Isabelle sources
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   612
  can refer to an infinite store of user-defined symbols, without
38a44d813a3c more details on Isabelle symbols;
wenzelm
parents: 34924
diff changeset
   613
  having to worry about the multitude of Unicode encodings.
20437
wenzelm
parents: 20430
diff changeset
   614
*}
wenzelm
parents: 20430
diff changeset
   615
wenzelm
parents: 20430
diff changeset
   616
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   617
subsection {* Basic names \label{sec:basic-names} *}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   618
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   619
text {*
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   620
  A \emph{basic name} essentially consists of a single Isabelle
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   621
  identifier.  There are conventions to mark separate classes of basic
29761
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29758
diff changeset
   622
  names, by attaching a suffix of underscores: one underscore means
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29758
diff changeset
   623
  \emph{internal name}, two underscores means \emph{Skolem name},
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29758
diff changeset
   624
  three underscores means \emph{internal Skolem name}.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   625
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   626
  For example, the basic name @{text "foo"} has the internal version
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   627
  @{text "foo_"}, with Skolem versions @{text "foo__"} and @{text
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   628
  "foo___"}, respectively.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   629
20488
wenzelm
parents: 20479
diff changeset
   630
  These special versions provide copies of the basic name space, apart
wenzelm
parents: 20479
diff changeset
   631
  from anything that normally appears in the user text.  For example,
wenzelm
parents: 20479
diff changeset
   632
  system generated variables in Isar proof contexts are usually marked
wenzelm
parents: 20479
diff changeset
   633
  as internal, which prevents mysterious name references like @{text
wenzelm
parents: 20479
diff changeset
   634
  "xaa"} to appear in the text.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   635
20488
wenzelm
parents: 20479
diff changeset
   636
  \medskip Manipulating binding scopes often requires on-the-fly
wenzelm
parents: 20479
diff changeset
   637
  renamings.  A \emph{name context} contains a collection of already
wenzelm
parents: 20479
diff changeset
   638
  used names.  The @{text "declare"} operation adds names to the
wenzelm
parents: 20479
diff changeset
   639
  context.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   640
20488
wenzelm
parents: 20479
diff changeset
   641
  The @{text "invents"} operation derives a number of fresh names from
wenzelm
parents: 20479
diff changeset
   642
  a given starting point.  For example, the first three names derived
wenzelm
parents: 20479
diff changeset
   643
  from @{text "a"} are @{text "a"}, @{text "b"}, @{text "c"}.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   644
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   645
  The @{text "variants"} operation produces fresh names by
20488
wenzelm
parents: 20479
diff changeset
   646
  incrementing tentative names as base-26 numbers (with digits @{text
wenzelm
parents: 20479
diff changeset
   647
  "a..z"}) until all clashes are resolved.  For example, name @{text
wenzelm
parents: 20479
diff changeset
   648
  "foo"} results in variants @{text "fooa"}, @{text "foob"}, @{text
wenzelm
parents: 20479
diff changeset
   649
  "fooc"}, \dots, @{text "fooaa"}, @{text "fooab"} etc.; each renaming
wenzelm
parents: 20479
diff changeset
   650
  step picks the next unused variant from this sequence.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   651
*}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   652
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   653
text %mlref {*
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   654
  \begin{mldecls}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   655
  @{index_ML Name.internal: "string -> string"} \\
20547
wenzelm
parents: 20530
diff changeset
   656
  @{index_ML Name.skolem: "string -> string"} \\
wenzelm
parents: 20530
diff changeset
   657
  \end{mldecls}
wenzelm
parents: 20530
diff changeset
   658
  \begin{mldecls}
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   659
  @{index_ML_type Name.context} \\
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   660
  @{index_ML Name.context: Name.context} \\
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   661
  @{index_ML Name.declare: "string -> Name.context -> Name.context"} \\
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   662
  @{index_ML Name.invents: "Name.context -> string -> int -> string list"} \\
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   663
  @{index_ML Name.variants: "string list -> Name.context -> string list * Name.context"} \\
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   664
  \end{mldecls}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   665
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   666
  \begin{description}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   667
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   668
  \item @{ML Name.internal}~@{text "name"} produces an internal name
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   669
  by adding one underscore.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   670
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   671
  \item @{ML Name.skolem}~@{text "name"} produces a Skolem name by
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   672
  adding two underscores.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   673
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   674
  \item @{ML_type Name.context} represents the context of already used
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   675
  names; the initial value is @{ML "Name.context"}.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   676
20488
wenzelm
parents: 20479
diff changeset
   677
  \item @{ML Name.declare}~@{text "name"} enters a used name into the
wenzelm
parents: 20479
diff changeset
   678
  context.
20437
wenzelm
parents: 20430
diff changeset
   679
20488
wenzelm
parents: 20479
diff changeset
   680
  \item @{ML Name.invents}~@{text "context name n"} produces @{text
wenzelm
parents: 20479
diff changeset
   681
  "n"} fresh names derived from @{text "name"}.
wenzelm
parents: 20479
diff changeset
   682
wenzelm
parents: 20479
diff changeset
   683
  \item @{ML Name.variants}~@{text "names context"} produces fresh
29761
2b658e50683a minor tuning and typographic fixes;
wenzelm
parents: 29758
diff changeset
   684
  variants of @{text "names"}; the result is entered into the context.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   685
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   686
  \end{description}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   687
*}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   688
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   689
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   690
subsection {* Indexed names *}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   691
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   692
text {*
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   693
  An \emph{indexed name} (or @{text "indexname"}) is a pair of a basic
20488
wenzelm
parents: 20479
diff changeset
   694
  name and a natural number.  This representation allows efficient
wenzelm
parents: 20479
diff changeset
   695
  renaming by incrementing the second component only.  The canonical
wenzelm
parents: 20479
diff changeset
   696
  way to rename two collections of indexnames apart from each other is
wenzelm
parents: 20479
diff changeset
   697
  this: determine the maximum index @{text "maxidx"} of the first
wenzelm
parents: 20479
diff changeset
   698
  collection, then increment all indexes of the second collection by
wenzelm
parents: 20479
diff changeset
   699
  @{text "maxidx + 1"}; the maximum index of an empty collection is
wenzelm
parents: 20479
diff changeset
   700
  @{text "-1"}.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   701
20488
wenzelm
parents: 20479
diff changeset
   702
  Occasionally, basic names and indexed names are injected into the
wenzelm
parents: 20479
diff changeset
   703
  same pair type: the (improper) indexname @{text "(x, -1)"} is used
wenzelm
parents: 20479
diff changeset
   704
  to encode basic names.
wenzelm
parents: 20479
diff changeset
   705
wenzelm
parents: 20479
diff changeset
   706
  \medskip Isabelle syntax observes the following rules for
wenzelm
parents: 20479
diff changeset
   707
  representing an indexname @{text "(x, i)"} as a packed string:
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   708
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   709
  \begin{itemize}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   710
20479
wenzelm
parents: 20476
diff changeset
   711
  \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
   712
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   713
  \item @{text "?xi"} if @{text "x"} does not end with a digit,
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   714
20488
wenzelm
parents: 20479
diff changeset
   715
  \item @{text "?x.i"} otherwise.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   716
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   717
  \end{itemize}
20470
c839b38a1f32 more on variables;
wenzelm
parents: 20452
diff changeset
   718
20488
wenzelm
parents: 20479
diff changeset
   719
  Indexnames may acquire large index numbers over time.  Results are
wenzelm
parents: 20479
diff changeset
   720
  normalized towards @{text "0"} at certain checkpoints, notably at
wenzelm
parents: 20479
diff changeset
   721
  the end of a proof.  This works by producing variants of the
wenzelm
parents: 20479
diff changeset
   722
  corresponding basic name components.  For example, the collection
wenzelm
parents: 20479
diff changeset
   723
  @{text "?x1, ?x7, ?x42"} becomes @{text "?x, ?xa, ?xb"}.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   724
*}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   725
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   726
text %mlref {*
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   727
  \begin{mldecls}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   728
  @{index_ML_type indexname} \\
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   729
  \end{mldecls}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   730
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   731
  \begin{description}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   732
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   733
  \item @{ML_type indexname} represents indexed names.  This is an
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   734
  abbreviation for @{ML_type "string * int"}.  The second component is
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   735
  usually non-negative, except for situations where @{text "(x, -1)"}
20488
wenzelm
parents: 20479
diff changeset
   736
  is used to embed basic names into this type.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   737
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   738
  \end{description}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   739
*}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   740
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   741
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   742
subsection {* Qualified names and name spaces *}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   743
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   744
text {*
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   745
  A \emph{qualified name} consists of a non-empty sequence of basic
20488
wenzelm
parents: 20479
diff changeset
   746
  name components.  The packed representation uses a dot as separator,
wenzelm
parents: 20479
diff changeset
   747
  as in ``@{text "A.b.c"}''.  The last component is called \emph{base}
wenzelm
parents: 20479
diff changeset
   748
  name, the remaining prefix \emph{qualifier} (which may be empty).
wenzelm
parents: 20479
diff changeset
   749
  The idea of qualified names is to encode nested structures by
wenzelm
parents: 20479
diff changeset
   750
  recording the access paths as qualifiers.  For example, an item
wenzelm
parents: 20479
diff changeset
   751
  named ``@{text "A.b.c"}'' may be understood as a local entity @{text
wenzelm
parents: 20479
diff changeset
   752
  "c"}, within a local structure @{text "b"}, within a global
wenzelm
parents: 20479
diff changeset
   753
  structure @{text "A"}.  Typically, name space hierarchies consist of
wenzelm
parents: 20479
diff changeset
   754
  1--2 levels of qualification, but this need not be always so.
20437
wenzelm
parents: 20430
diff changeset
   755
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   756
  The empty name is commonly used as an indication of unnamed
20488
wenzelm
parents: 20479
diff changeset
   757
  entities, whenever this makes any sense.  The basic operations on
wenzelm
parents: 20479
diff changeset
   758
  qualified names are smart enough to pass through such improper names
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   759
  unchanged.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   760
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   761
  \medskip A @{text "naming"} policy tells how to turn a name
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   762
  specification into a fully qualified internal name (by the @{text
20488
wenzelm
parents: 20479
diff changeset
   763
  "full"} operation), and how fully qualified names may be accessed
wenzelm
parents: 20479
diff changeset
   764
  externally.  For example, the default naming policy is to prefix an
wenzelm
parents: 20479
diff changeset
   765
  implicit path: @{text "full x"} produces @{text "path.x"}, and the
wenzelm
parents: 20479
diff changeset
   766
  standard accesses for @{text "path.x"} include both @{text "x"} and
wenzelm
parents: 20479
diff changeset
   767
  @{text "path.x"}.  Normally, the naming is implicit in the theory or
wenzelm
parents: 20479
diff changeset
   768
  proof context; there are separate versions of the corresponding.
20437
wenzelm
parents: 20430
diff changeset
   769
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   770
  \medskip A @{text "name space"} manages a collection of fully
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   771
  internalized names, together with a mapping between external names
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   772
  and internal names (in both directions).  The corresponding @{text
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   773
  "intern"} and @{text "extern"} operations are mostly used for
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   774
  parsing and printing only!  The @{text "declare"} operation augments
20488
wenzelm
parents: 20479
diff changeset
   775
  a name space according to the accesses determined by the naming
wenzelm
parents: 20479
diff changeset
   776
  policy.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   777
20488
wenzelm
parents: 20479
diff changeset
   778
  \medskip As a general principle, there is a separate name space for
wenzelm
parents: 20479
diff changeset
   779
  each kind of formal entity, e.g.\ logical constant, type
wenzelm
parents: 20479
diff changeset
   780
  constructor, type class, theorem.  It is usually clear from the
wenzelm
parents: 20479
diff changeset
   781
  occurrence in concrete syntax (or from the scope) which kind of
wenzelm
parents: 20479
diff changeset
   782
  entity a name refers to.  For example, the very same name @{text
wenzelm
parents: 20479
diff changeset
   783
  "c"} may be used uniformly for a constant, type constructor, and
wenzelm
parents: 20479
diff changeset
   784
  type class.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   785
20479
wenzelm
parents: 20476
diff changeset
   786
  There are common schemes to name theorems systematically, according
20488
wenzelm
parents: 20479
diff changeset
   787
  to the name of the main logical entity involved, e.g.\ @{text
wenzelm
parents: 20479
diff changeset
   788
  "c.intro"} for a canonical theorem related to constant @{text "c"}.
wenzelm
parents: 20479
diff changeset
   789
  This technique of mapping names from one space into another requires
wenzelm
parents: 20479
diff changeset
   790
  some care in order to avoid conflicts.  In particular, theorem names
wenzelm
parents: 20479
diff changeset
   791
  derived from a type constructor or type class are better suffixed in
wenzelm
parents: 20479
diff changeset
   792
  addition to the usual qualification, e.g.\ @{text "c_type.intro"}
wenzelm
parents: 20479
diff changeset
   793
  and @{text "c_class.intro"} for theorems related to type @{text "c"}
wenzelm
parents: 20479
diff changeset
   794
  and class @{text "c"}, respectively.
20437
wenzelm
parents: 20430
diff changeset
   795
*}
wenzelm
parents: 20430
diff changeset
   796
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   797
text %mlref {*
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   798
  \begin{mldecls}
30365
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   799
  @{index_ML Long_Name.base_name: "string -> string"} \\
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   800
  @{index_ML Long_Name.qualifier: "string -> string"} \\
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   801
  @{index_ML Long_Name.append: "string -> string -> string"} \\
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   802
  @{index_ML Long_Name.implode: "string list -> string"} \\
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   803
  @{index_ML Long_Name.explode: "string -> string list"} \\
20547
wenzelm
parents: 20530
diff changeset
   804
  \end{mldecls}
wenzelm
parents: 20530
diff changeset
   805
  \begin{mldecls}
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   806
  @{index_ML_type Name_Space.naming} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   807
  @{index_ML Name_Space.default_naming: Name_Space.naming} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   808
  @{index_ML Name_Space.add_path: "string -> Name_Space.naming -> Name_Space.naming"} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   809
  @{index_ML Name_Space.full_name: "Name_Space.naming -> binding -> string"} \\
20547
wenzelm
parents: 20530
diff changeset
   810
  \end{mldecls}
wenzelm
parents: 20530
diff changeset
   811
  \begin{mldecls}
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   812
  @{index_ML_type Name_Space.T} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   813
  @{index_ML Name_Space.empty: "string -> Name_Space.T"} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   814
  @{index_ML Name_Space.merge: "Name_Space.T * Name_Space.T -> Name_Space.T"} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   815
  @{index_ML Name_Space.declare: "bool -> Name_Space.naming -> binding -> Name_Space.T ->
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   816
  string * Name_Space.T"} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   817
  @{index_ML Name_Space.intern: "Name_Space.T -> string -> string"} \\
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   818
  @{index_ML Name_Space.extern: "Name_Space.T -> string -> string"} \\
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   819
  \end{mldecls}
20437
wenzelm
parents: 20430
diff changeset
   820
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   821
  \begin{description}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   822
30365
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   823
  \item @{ML Long_Name.base_name}~@{text "name"} returns the base name of a
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   824
  qualified name.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   825
30365
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   826
  \item @{ML Long_Name.qualifier}~@{text "name"} returns the qualifier
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   827
  of a qualified name.
20437
wenzelm
parents: 20430
diff changeset
   828
30365
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   829
  \item @{ML Long_Name.append}~@{text "name\<^isub>1 name\<^isub>2"}
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   830
  appends two qualified names.
20437
wenzelm
parents: 20430
diff changeset
   831
30365
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   832
  \item @{ML Long_Name.implode}~@{text "names"} and @{ML
790129514c2d adapted to structure Long_Name;
wenzelm
parents: 30281
diff changeset
   833
  Long_Name.explode}~@{text "name"} convert between the packed string
20488
wenzelm
parents: 20479
diff changeset
   834
  representation and the explicit list form of qualified names.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   835
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   836
  \item @{ML_type Name_Space.naming} represents the abstract concept of
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   837
  a naming policy.
20437
wenzelm
parents: 20430
diff changeset
   838
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   839
  \item @{ML Name_Space.default_naming} is the default naming policy.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   840
  In a theory context, this is usually augmented by a path prefix
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   841
  consisting of the theory name.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   842
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   843
  \item @{ML Name_Space.add_path}~@{text "path naming"} augments the
20488
wenzelm
parents: 20479
diff changeset
   844
  naming policy by extending its path component.
20437
wenzelm
parents: 20430
diff changeset
   845
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   846
  \item @{ML Name_Space.full_name}~@{text "naming binding"} turns a
30281
9ad15d8ed311 renamed NameSpace.base to NameSpace.base_name (in accordance with "full_name");
wenzelm
parents: 30272
diff changeset
   847
  name binding (usually a basic name) into the fully qualified
29008
d863c103ded0 adapted to changes in binding module
haftmann
parents: 26872
diff changeset
   848
  internal name, according to the given naming policy.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   849
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   850
  \item @{ML_type Name_Space.T} represents name spaces.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   851
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   852
  \item @{ML Name_Space.empty}~@{text "kind"} and @{ML Name_Space.merge}~@{text
20488
wenzelm
parents: 20479
diff changeset
   853
  "(space\<^isub>1, space\<^isub>2)"} are the canonical operations for
wenzelm
parents: 20479
diff changeset
   854
  maintaining name spaces according to theory data management
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   855
  (\secref{sec:context-data}); @{text "kind"} is a formal comment
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   856
  to characterize the purpose of a name space.
20437
wenzelm
parents: 20430
diff changeset
   857
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   858
  \item @{ML Name_Space.declare}~@{text "strict naming bindings
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   859
  space"} enters a name binding as fully qualified internal name into
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   860
  the name space, with external accesses determined by the naming
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   861
  policy.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   862
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   863
  \item @{ML Name_Space.intern}~@{text "space name"} internalizes a
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   864
  (partially qualified) external name.
20437
wenzelm
parents: 20430
diff changeset
   865
20488
wenzelm
parents: 20479
diff changeset
   866
  This operation is mostly for parsing!  Note that fully qualified
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   867
  names stemming from declarations are produced via @{ML
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   868
  "Name_Space.full_name"} and @{ML "Name_Space.declare"}
29008
d863c103ded0 adapted to changes in binding module
haftmann
parents: 26872
diff changeset
   869
  (or their derivatives for @{ML_type theory} and
20488
wenzelm
parents: 20479
diff changeset
   870
  @{ML_type Proof.context}).
20437
wenzelm
parents: 20430
diff changeset
   871
33174
1f2051f41335 adjusted to changes in corresponding ML code
haftmann
parents: 30365
diff changeset
   872
  \item @{ML Name_Space.extern}~@{text "space name"} externalizes a
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   873
  (fully qualified) internal name.
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   874
30281
9ad15d8ed311 renamed NameSpace.base to NameSpace.base_name (in accordance with "full_name");
wenzelm
parents: 30272
diff changeset
   875
  This operation is mostly for printing!  User code should not rely on
9ad15d8ed311 renamed NameSpace.base to NameSpace.base_name (in accordance with "full_name");
wenzelm
parents: 30272
diff changeset
   876
  the precise result too much.
20476
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   877
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   878
  \end{description}
6d3f144cc1bd more on names;
wenzelm
parents: 20475
diff changeset
   879
*}
30272
2d612824e642 regenerated document;
wenzelm
parents: 30270
diff changeset
   880
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   881
end