doc-src/IsarImplementation/Thy/Local_Theory.thy
changeset 29765 9930a0d8dd32
parent 29764 b0b6d34388e9
child 29766 da411bda1d41
equal deleted inserted replaced
29764:b0b6d34388e9 29765:9930a0d8dd32
    24   Isabelle/Pure provides target mechanisms for locales, type-classes,
    24   Isabelle/Pure provides target mechanisms for locales, type-classes,
    25   type-class instantiations, and general overloading.  In principle,
    25   type-class instantiations, and general overloading.  In principle,
    26   users can implement new targets as well, but this rather arcane
    26   users can implement new targets as well, but this rather arcane
    27   discipline is beyond the scope of this manual.  In contrast,
    27   discipline is beyond the scope of this manual.  In contrast,
    28   implementing derived definitional packages to be used within a local
    28   implementing derived definitional packages to be used within a local
    29   theory context is quite easy: the interfaces are simpler and more
    29   theory context is quite easy: the interfaces are even simpler and
    30   abstract than the underlying primitives for raw theories.
    30   more abstract than the underlying primitives for raw theories.
    31 
    31 
    32   Many definitional packages for local theories are available in
    32   Many definitional packages for local theories are available in
    33   Isabelle/Pure and Isabelle/HOL.  Even though a few old packages that
    33   Isabelle.  Although a few old packages only work for global
    34   only work for global theories might still persists, the local theory
    34   theories, the local theory interface is already the standard way of
    35   interface is already the standard way of implementing definitional
    35   implementing definitional packages in Isabelle.
    36   packages in Isabelle.
       
    37 *}
    36 *}
    38 
    37 
    39 
    38 
    40 section {* Definitional elements *}
    39 section {* Definitional elements *}
    41 
    40 
    42 text {*
    41 text {*
    43   There are separate definitional elements @{text "\<DEFINE> c \<equiv> t"}
    42   There are separate elements @{text "\<DEFINE> c \<equiv> t"} for terms, and
    44   for terms, and @{text "\<NOTE> b = thm"} for theorems.  Types are
    43   @{text "\<NOTE> b = thm"} for theorems.  Types are treated
    45   treated implicitly, according to Hindley-Milner discipline (cf.\
    44   implicitly, according to Hindley-Milner discipline (cf.\
    46   \secref{sec:variables}).  These definitional primitives essentially
    45   \secref{sec:variables}).  These definitional primitives essentially
    47   act like @{text "let"}-bindings within a local context that may
    46   act like @{text "let"}-bindings within a local context that may
    48   already contain some @{text "\<lambda>"}-bindings.  Thus we gain
    47   already contain earlier @{text "let"}-bindings and some initial
    49   \emph{dependent definitions}, relatively to an initial axiomatic
    48   @{text "\<lambda>"}-bindings.  Thus we gain \emph{dependent definitions}
    50   context.  The following diagram illustrates this idea of axiomatic
    49   that are relative to an initial axiomatic context.  The following
    51   elements versus definitional elements:
    50   diagram illustrates this idea of axiomatic elements versus
       
    51   definitional elements:
    52 
    52 
    53   \begin{center}
    53   \begin{center}
    54   \begin{tabular}{|l|l|l|}
    54   \begin{tabular}{|l|l|l|}
    55   \hline
    55   \hline
    56   & @{text "\<lambda>"}-binding & @{text "let"}-binding \\
    56   & @{text "\<lambda>"}-binding & @{text "let"}-binding \\
    64 
    64 
    65   A user package merely needs to produce suitable @{text "\<DEFINE>"}
    65   A user package merely needs to produce suitable @{text "\<DEFINE>"}
    66   and @{text "\<NOTE>"} elements according to the application.  For
    66   and @{text "\<NOTE>"} elements according to the application.  For
    67   example, a package for inductive definitions might first @{text
    67   example, a package for inductive definitions might first @{text
    68   "\<DEFINE>"} a certain predicate as some fixed-point construction,
    68   "\<DEFINE>"} a certain predicate as some fixed-point construction,
    69   prove the monotonicity of the functor involved here and @{text
    69   then @{text "\<NOTE>"} a proven result about monotonicity of the
    70   "\<NOTE>"} the result, and then produce further derived concepts via
    70   functor involved here, and then produce further derived concepts via
    71   additional @{text "\<DEFINE>"} and @{text "\<NOTE>"} elements.
    71   additional @{text "\<DEFINE>"} and @{text "\<NOTE>"} elements.
    72 
    72 
    73   The cumulative sequence of @{text "\<DEFINE>"} and @{text "\<NOTE>"}
    73   The cumulative sequence of @{text "\<DEFINE>"} and @{text "\<NOTE>"}
    74   produced at package runtime is managed by the local theory
    74   produced at package runtime is managed by the local theory
    75   infrastructure by means of an \emph{auxiliary context}.  Thus the
    75   infrastructure by means of an \emph{auxiliary context}.  Thus the
    85   \begin{center}
    85   \begin{center}
    86   \framebox{\quad auxiliary context \quad\framebox{\quad target context \quad\framebox{\quad background theory\quad}}}
    86   \framebox{\quad auxiliary context \quad\framebox{\quad target context \quad\framebox{\quad background theory\quad}}}
    87   \end{center}
    87   \end{center}
    88 
    88 
    89   \noindent When a definitional package is finished, the auxiliary
    89   \noindent When a definitional package is finished, the auxiliary
    90   context will be reset to the target context.  The target now holds
    90   context is reset to the target context.  The target now holds
    91   definitions for terms and theorems that stem from the hypothetical
    91   definitions for terms and theorems that stem from the hypothetical
    92   @{text "\<DEFINE>"} and @{text "\<NOTE>"} elements, transformed by
    92   @{text "\<DEFINE>"} and @{text "\<NOTE>"} elements, transformed by
    93   the particular target policy (see \cite[\S4-5]{Haftmann-Wenzel:2009}
    93   the particular target policy (see
    94   for details).
    94   \cite[\S4--5]{Haftmann-Wenzel:2009} for details).
    95 *}
    95 *}
    96 
    96 
    97 text %mlref {*
    97 text %mlref {*
    98   \begin{mldecls}
    98   \begin{mldecls}
       
    99   @{index_ML_type local_theory: Proof.context} \\
       
   100   @{index_ML TheoryTarget.init: "string option -> theory -> local_theory"} \\[1ex]
    99   @{index_ML LocalTheory.define: "string ->
   101   @{index_ML LocalTheory.define: "string ->
   100     (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
   102     (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
   101     (term * (string * thm)) * local_theory"} \\
   103     (term * (string * thm)) * local_theory"} \\
   102   @{index_ML LocalTheory.note: "string ->
   104   @{index_ML LocalTheory.note: "string ->
   103     Attrib.binding * thm list -> local_theory ->
   105     Attrib.binding * thm list -> local_theory ->
   104     (string * thm list) * local_theory"} \\
   106     (string * thm list) * local_theory"} \\
   105   \end{mldecls}
   107   \end{mldecls}
   106 
   108 
   107   \begin{description}
   109   \begin{description}
   108 
   110 
   109   \item @{ML LocalTheory.define}~@{text FIXME}
   111   \item @{ML_type local_theory} represents local theories.  Although
       
   112   this is merely an alias for @{ML_type Proof.context}, it is
       
   113   semantically a subtype of the same: a @{ML_type local_theory} holds
       
   114   target information as special context data.  Subtyping means that
       
   115   any value @{text "lthy:"}~@{ML_type local_theory} can be also used
       
   116   with operations on expecting a regular @{text "ctxt:"}~@{ML_type
       
   117   Proof.context}.
   110 
   118 
   111   \item @{ML LocalTheory.note}~@{text FIXME}
   119   \item @{ML TheoryTarget.init}~@{text "NONE thy"} initializes a
       
   120   trivial local theory from the given background theory.
       
   121   Alternatively, @{text "SOME name"} may be given to initialize a
       
   122   locale or class context (a fully-qualified internal name is expected
       
   123   here).  This is useful for experimentation --- normally the Isar
       
   124   toplevel already takes care to initialize particular local theory
       
   125   contexts according to user specifications.
       
   126 
       
   127   \item @{ML LocalTheory.define}~@{text "kind ((b, mx), (a, rhs))
       
   128   lthy"} defines a local entity according to the specification that is
       
   129   given relatively to the current @{text "lthy"} context.  In
       
   130   particular the term of the RHS may refer to earlier local entities
       
   131   from the auxiliary context or hypothetical parameters from the
       
   132   target context.  The result is the newly defined term (which is
       
   133   always a fixed variable with exactly the same name as specified for
       
   134   the LHS), together with an equational theorem that states the
       
   135   definition as a hypothetical fact.
       
   136 
       
   137   Unless an explicit name binding is given for the RHS, the resulting
       
   138   fact will be called @{text "b_def"}.  Any given attributes are
       
   139   applied to that same fact --- immediately in the auxiliary context
       
   140   \emph{and} in any transformations stemming from target-specific
       
   141   policies or any later interpretations of results from the target
       
   142   context (think of @{command locale} and @{command interpretation},
       
   143   for example).  This means that attributes should be usually plain
       
   144   declarations such as @{attribute simp}, while non-trivial rules like
       
   145   @{attribute simplified} are better avoided.
       
   146 
       
   147   The @{text kind} determines the theorem kind of the resulting fact.
       
   148   Typical examples are @{ML Thm.definitionK}, @{ML Thm.theoremK}, or
       
   149   @{ML Thm.internalK}.
       
   150 
       
   151   \item @{ML LocalTheory.note}~@{text "kind (a, ths) lthy"} is
       
   152   analogous to @{ML LocalTheory.define}, but defines facts instead of
       
   153   terms.  There is also a slightly more general variant @{ML
       
   154   LocalTheory.notes} that defines several facts (with attribute
       
   155   expressions) simultaneously.
       
   156 
       
   157   This is essentially the internal version of the @{command lemmas}
       
   158   command, or @{command declare} if an empty name binding is given.
   112 
   159 
   113   \end{description}
   160   \end{description}
   114 *}
   161 *}
   115 
   162 
   116 
   163