src/Doc/Implementation/Local_Theory.thy
author wenzelm
Fri, 13 Nov 2015 14:49:30 +0100
changeset 61656 cfabbc083977
parent 61493 0debd22f0c0e
child 61854 38b049cd3aad
permissions -rw-r--r--
more uniform jEdit properties;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61656
cfabbc083977 more uniform jEdit properties;
wenzelm
parents: 61493
diff changeset
     1
(*:maxLineLen=78:*)
cfabbc083977 more uniform jEdit properties;
wenzelm
parents: 61493
diff changeset
     2
29755
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 20520
diff changeset
     3
theory Local_Theory
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 20520
diff changeset
     4
imports Base
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 20520
diff changeset
     5
begin
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
     6
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
     7
chapter \<open>Local theory specifications \label{ch:local-theory}\<close>
29759
bcb79ddf57da removed rudiments of glossary;
wenzelm
parents: 29755
diff changeset
     8
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
     9
text \<open>
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    10
  A \<^emph>\<open>local theory\<close> combines aspects of both theory and proof
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    11
  context (cf.\ \secref{sec:context}), such that definitional
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    12
  specifications may be given relatively to parameters and
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    13
  assumptions.  A local theory is represented as a regular proof
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    14
  context, augmented by administrative data about the \<^emph>\<open>target
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    15
  context\<close>.
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    16
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    17
  The target is usually derived from the background theory by adding
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    18
  local \<open>\<FIX>\<close> and \<open>\<ASSUME>\<close> elements, plus
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    19
  suitable modifications of non-logical context data (e.g.\ a special
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    20
  type-checking discipline).  Once initialized, the target is ready to
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    21
  absorb definitional primitives: \<open>\<DEFINE>\<close> for terms and
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    22
  \<open>\<NOTE>\<close> for theorems.  Such definitions may get
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    23
  transformed in a target-specific way, but the programming interface
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    24
  hides such details.
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    25
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    26
  Isabelle/Pure provides target mechanisms for locales, type-classes,
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    27
  type-class instantiations, and general overloading.  In principle,
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    28
  users can implement new targets as well, but this rather arcane
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    29
  discipline is beyond the scope of this manual.  In contrast,
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    30
  implementing derived definitional packages to be used within a local
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    31
  theory context is quite easy: the interfaces are even simpler and
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    32
  more abstract than the underlying primitives for raw theories.
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    33
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    34
  Many definitional packages for local theories are available in
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    35
  Isabelle.  Although a few old packages only work for global
39839
08f59175e541 misc tuning;
wenzelm
parents: 38466
diff changeset
    36
  theories, the standard way of implementing definitional packages in
08f59175e541 misc tuning;
wenzelm
parents: 38466
diff changeset
    37
  Isabelle is via the local theory interface.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    38
\<close>
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    39
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    40
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    41
section \<open>Definitional elements\<close>
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
    42
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    43
text \<open>
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    44
  There are separate elements \<open>\<DEFINE> c \<equiv> t\<close> for terms, and
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    45
  \<open>\<NOTE> b = thm\<close> for theorems.  Types are treated
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    46
  implicitly, according to Hindley-Milner discipline (cf.\
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    47
  \secref{sec:variables}).  These definitional primitives essentially
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    48
  act like \<open>let\<close>-bindings within a local context that may
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    49
  already contain earlier \<open>let\<close>-bindings and some initial
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    50
  \<open>\<lambda>\<close>-bindings.  Thus we gain \<^emph>\<open>dependent definitions\<close>
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    51
  that are relative to an initial axiomatic context.  The following
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    52
  diagram illustrates this idea of axiomatic elements versus
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    53
  definitional elements:
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    54
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    55
  \begin{center}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    56
  \begin{tabular}{|l|l|l|}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    57
  \hline
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    58
  & \<open>\<lambda>\<close>-binding & \<open>let\<close>-binding \\
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    59
  \hline
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    60
  types & fixed \<open>\<alpha>\<close> & arbitrary \<open>\<beta>\<close> \\
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    61
  terms & \<open>\<FIX> x :: \<tau>\<close> & \<open>\<DEFINE> c \<equiv> t\<close> \\
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    62
  theorems & \<open>\<ASSUME> a: A\<close> & \<open>\<NOTE> b = \<^BG>B\<^EN>\<close> \\
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    63
  \hline
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    64
  \end{tabular}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    65
  \end{center}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    66
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    67
  A user package merely needs to produce suitable \<open>\<DEFINE>\<close>
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    68
  and \<open>\<NOTE>\<close> elements according to the application.  For
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    69
  example, a package for inductive definitions might first \<open>\<DEFINE>\<close> a certain predicate as some fixed-point construction,
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    70
  then \<open>\<NOTE>\<close> a proven result about monotonicity of the
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    71
  functor involved here, and then produce further derived concepts via
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    72
  additional \<open>\<DEFINE>\<close> and \<open>\<NOTE>\<close> elements.
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    73
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    74
  The cumulative sequence of \<open>\<DEFINE>\<close> and \<open>\<NOTE>\<close>
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    75
  produced at package runtime is managed by the local theory
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    76
  infrastructure by means of an \<^emph>\<open>auxiliary context\<close>.  Thus the
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    77
  system holds up the impression of working within a fully abstract
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    78
  situation with hypothetical entities: \<open>\<DEFINE> c \<equiv> t\<close>
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    79
  always results in a literal fact \<open>\<^BG>c \<equiv> t\<^EN>\<close>, where
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    80
  \<open>c\<close> is a fixed variable \<open>c\<close>.  The details about
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    81
  global constants, name spaces etc. are handled internally.
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    82
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    83
  So the general structure of a local theory is a sandwich of three
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    84
  layers:
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    85
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    86
  \begin{center}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    87
  \framebox{\quad auxiliary context \quad\framebox{\quad target context \quad\framebox{\quad background theory\quad}}}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    88
  \end{center}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    89
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39849
diff changeset
    90
  When a definitional package is finished, the auxiliary context is
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39849
diff changeset
    91
  reset to the target context.  The target now holds definitions for
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    92
  terms and theorems that stem from the hypothetical \<open>\<DEFINE>\<close> and \<open>\<NOTE>\<close> elements, transformed by the
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57181
diff changeset
    93
  particular target policy (see @{cite \<open>\S4--5\<close> "Haftmann-Wenzel:2009"}
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    94
  for details).\<close>
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    95
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    96
text %mlref \<open>
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
    97
  \begin{mldecls}
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
    98
  @{index_ML_type local_theory: Proof.context} \\
57181
2d13bf9ea77b dropped obscure and unused ad-hoc before_exit hook for named targets
haftmann
parents: 56420
diff changeset
    99
  @{index_ML Named_Target.init: "string -> theory -> local_theory"} \\[1ex]
33834
7c06e19f717c adapted local theory operations -- eliminated odd kind;
wenzelm
parents: 33672
diff changeset
   100
  @{index_ML Local_Theory.define: "(binding * mixfix) * (Attrib.binding * term) ->
7c06e19f717c adapted local theory operations -- eliminated odd kind;
wenzelm
parents: 33672
diff changeset
   101
    local_theory -> (term * (string * thm)) * local_theory"} \\
33672
8bde36ec8eb1 updated Local_Theory and Theory_Target;
wenzelm
parents: 30272
diff changeset
   102
  @{index_ML Local_Theory.note: "Attrib.binding * thm list ->
8bde36ec8eb1 updated Local_Theory and Theory_Target;
wenzelm
parents: 30272
diff changeset
   103
    local_theory -> (string * thm list) * local_theory"} \\
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
   104
  \end{mldecls}
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
   105
61439
2bf52eec4e8a more symbols;
wenzelm
parents: 58618
diff changeset
   106
  \<^descr> Type @{ML_type local_theory} represents local theories.
39864
wenzelm
parents: 39861
diff changeset
   107
  Although this is merely an alias for @{ML_type Proof.context}, it is
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   108
  semantically a subtype of the same: a @{ML_type local_theory} holds
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   109
  target information as special context data.  Subtyping means that
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   110
  any value \<open>lthy:\<close>~@{ML_type local_theory} can be also used
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   111
  with operations on expecting a regular \<open>ctxt:\<close>~@{ML_type
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   112
  Proof.context}.
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   113
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   114
  \<^descr> @{ML Named_Target.init}~\<open>before_exit name thy\<close>
41621
55b16bd82142 updated Named_Target.init;
wenzelm
parents: 39877
diff changeset
   115
  initializes a local theory derived from the given background theory.
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   116
  An empty name refers to a \<^emph>\<open>global theory\<close> context, and a
41621
55b16bd82142 updated Named_Target.init;
wenzelm
parents: 39877
diff changeset
   117
  non-empty name refers to a @{command locale} or @{command class}
55b16bd82142 updated Named_Target.init;
wenzelm
parents: 39877
diff changeset
   118
  context (a fully-qualified internal name is expected here).  This is
55b16bd82142 updated Named_Target.init;
wenzelm
parents: 39877
diff changeset
   119
  useful for experimentation --- normally the Isar toplevel already
57181
2d13bf9ea77b dropped obscure and unused ad-hoc before_exit hook for named targets
haftmann
parents: 56420
diff changeset
   120
  takes care to initialize the local theory context.
29764
b0b6d34388e9 some text on local theory specifications;
wenzelm
parents: 29759
diff changeset
   121
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   122
  \<^descr> @{ML Local_Theory.define}~\<open>((b, mx), (a, rhs))
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   123
  lthy\<close> defines a local entity according to the specification that is
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   124
  given relatively to the current \<open>lthy\<close> context.  In
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   125
  particular the term of the RHS may refer to earlier local entities
29766
wenzelm
parents: 29765
diff changeset
   126
  from the auxiliary context, or hypothetical parameters from the
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   127
  target context.  The result is the newly defined term (which is
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   128
  always a fixed variable with exactly the same name as specified for
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   129
  the LHS), together with an equational theorem that states the
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   130
  definition as a hypothetical fact.
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   131
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   132
  Unless an explicit name binding is given for the RHS, the resulting
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   133
  fact will be called \<open>b_def\<close>.  Any given attributes are
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   134
  applied to that same fact --- immediately in the auxiliary context
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   135
  \<^emph>\<open>and\<close> in any transformed versions stemming from target-specific
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   136
  policies or any later interpretations of results from the target
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   137
  context (think of @{command locale} and @{command interpretation},
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   138
  for example).  This means that attributes should be usually plain
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   139
  declarations such as @{attribute simp}, while non-trivial rules like
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   140
  @{attribute simplified} are better avoided.
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   141
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   142
  \<^descr> @{ML Local_Theory.note}~\<open>(a, ths) lthy\<close> is
33672
8bde36ec8eb1 updated Local_Theory and Theory_Target;
wenzelm
parents: 30272
diff changeset
   143
  analogous to @{ML Local_Theory.define}, but defines facts instead of
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   144
  terms.  There is also a slightly more general variant @{ML
33672
8bde36ec8eb1 updated Local_Theory and Theory_Target;
wenzelm
parents: 30272
diff changeset
   145
  Local_Theory.notes} that defines several facts (with attribute
29765
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   146
  expressions) simultaneously.
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   147
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   148
  This is essentially the internal version of the @{command lemmas}
9930a0d8dd32 more on local theories;
wenzelm
parents: 29764
diff changeset
   149
  command, or @{command declare} if an empty name binding is given.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   150
\<close>
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   151
20451
27ea2ba48fa3 misc cleanup;
wenzelm
parents: 18537
diff changeset
   152
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   153
section \<open>Morphisms and declarations \label{sec:morphisms}\<close>
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   154
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   155
text \<open>
52422
wenzelm
parents: 48985
diff changeset
   156
  %FIXME
39877
1206e88f1284 more refs;
wenzelm
parents: 39864
diff changeset
   157
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57181
diff changeset
   158
  See also @{cite "Chaieb-Wenzel:2007"}.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   159
\<close>
30272
2d612824e642 regenerated document;
wenzelm
parents: 30270
diff changeset
   160
18537
2681f9e34390 "The Isabelle/Isar Implementation" manual;
wenzelm
parents:
diff changeset
   161
end