src/Doc/Isar_Ref/Spec.thy
changeset 61477 e467ae7aa808
parent 61459 5f2ddeb15c06
child 61493 0debd22f0c0e
equal deleted inserted replaced
61476:1884c40f1539 61477:e467ae7aa808
    35   an explicit proof as justification (e.g.\ @{command function} and
    35   an explicit proof as justification (e.g.\ @{command function} and
    36   @{command termination} in Isabelle/HOL). Plain statements like @{command
    36   @{command termination} in Isabelle/HOL). Plain statements like @{command
    37   theorem} or @{command lemma} are merely a special case of that, defining a
    37   theorem} or @{command lemma} are merely a special case of that, defining a
    38   theorem from a given proposition and its proof.
    38   theorem from a given proposition and its proof.
    39 
    39 
    40   The theory body may be sub-structured by means of \emph{local theory
    40   The theory body may be sub-structured by means of \<^emph>\<open>local theory
    41   targets}, such as @{command "locale"} and @{command "class"}. It is also
    41   targets\<close>, such as @{command "locale"} and @{command "class"}. It is also
    42   possible to use @{command context}~@{keyword "begin"}~\dots~@{command end}
    42   possible to use @{command context}~@{keyword "begin"}~\dots~@{command end}
    43   blocks to delimited a local theory context: a \emph{named context} to
    43   blocks to delimited a local theory context: a \<^emph>\<open>named context\<close> to
    44   augment a locale or class specification, or an \emph{unnamed context} to
    44   augment a locale or class specification, or an \<^emph>\<open>unnamed context\<close> to
    45   refer to local parameters and assumptions that are discharged later. See
    45   refer to local parameters and assumptions that are discharged later. See
    46   \secref{sec:target} for more details.
    46   \secref{sec:target} for more details.
    47 
    47 
    48   \<^medskip>
    48   \<^medskip>
    49   A theory is commenced by the @{command "theory"} command, which
    49   A theory is commenced by the @{command "theory"} command, which
   129   A local theory target is a specification context that is managed
   129   A local theory target is a specification context that is managed
   130   separately within the enclosing theory. Contexts may introduce parameters
   130   separately within the enclosing theory. Contexts may introduce parameters
   131   (fixed variables) and assumptions (hypotheses). Definitions and theorems
   131   (fixed variables) and assumptions (hypotheses). Definitions and theorems
   132   depending on the context may be added incrementally later on.
   132   depending on the context may be added incrementally later on.
   133 
   133 
   134   \emph{Named contexts} refer to locales (cf.\ \secref{sec:locale}) or
   134   \<^emph>\<open>Named contexts\<close> refer to locales (cf.\ \secref{sec:locale}) or
   135   type classes (cf.\ \secref{sec:class}); the name ``@{text "-"}''
   135   type classes (cf.\ \secref{sec:class}); the name ``@{text "-"}''
   136   signifies the global theory context.
   136   signifies the global theory context.
   137 
   137 
   138   \emph{Unnamed contexts} may introduce additional parameters and
   138   \<^emph>\<open>Unnamed contexts\<close> may introduce additional parameters and
   139   assumptions, and results produced in the context are generalized
   139   assumptions, and results produced in the context are generalized
   140   accordingly.  Such auxiliary contexts may be nested within other
   140   accordingly.  Such auxiliary contexts may be nested within other
   141   targets, like @{command "locale"}, @{command "class"}, @{command
   141   targets, like @{command "locale"}, @{command "class"}, @{command
   142   "instantiation"}, @{command "overloading"}.
   142   "instantiation"}, @{command "overloading"}.
   143 
   143 
   231   in @{text "this_rule [intro] that_rule [elim]"} for example.
   231   in @{text "this_rule [intro] that_rule [elim]"} for example.
   232   Configuration options (\secref{sec:config}) are special declaration
   232   Configuration options (\secref{sec:config}) are special declaration
   233   attributes that operate on the context without a theorem, as in
   233   attributes that operate on the context without a theorem, as in
   234   @{text "[[show_types = false]]"} for example.
   234   @{text "[[show_types = false]]"} for example.
   235 
   235 
   236   Expressions of this form may be defined as \emph{bundled
   236   Expressions of this form may be defined as \<^emph>\<open>bundled
   237   declarations} in the context, and included in other situations later
   237   declarations\<close> in the context, and included in other situations later
   238   on.  Including declaration bundles augments a local context casually
   238   on.  Including declaration bundles augments a local context casually
   239   without logical dependencies, which is in contrast to locales and
   239   without logical dependencies, which is in contrast to locales and
   240   locale interpretation (\secref{sec:locale}).
   240   locale interpretation (\secref{sec:locale}).
   241 
   241 
   242   @{rail \<open>
   242   @{rail \<open>
   451   instances.
   451   instances.
   452 
   452 
   453   A locale may be opened with the purpose of appending to its list of
   453   A locale may be opened with the purpose of appending to its list of
   454   declarations (cf.\ \secref{sec:target}).  When opening a locale
   454   declarations (cf.\ \secref{sec:target}).  When opening a locale
   455   declarations from all dependencies are collected and are presented
   455   declarations from all dependencies are collected and are presented
   456   as a local theory.  In this process, which is called \emph{roundup},
   456   as a local theory.  In this process, which is called \<^emph>\<open>roundup\<close>,
   457   redundant locale instances are omitted.  A locale instance is
   457   redundant locale instances are omitted.  A locale instance is
   458   redundant if it is subsumed by an instance encountered earlier.  A
   458   redundant if it is subsumed by an instance encountered earlier.  A
   459   more detailed description of this process is available elsewhere
   459   more detailed description of this process is available elsewhere
   460   @{cite Ballarin2014}.
   460   @{cite Ballarin2014}.
   461 \<close>
   461 \<close>
   462 
   462 
   463 
   463 
   464 subsection \<open>Locale expressions \label{sec:locale-expr}\<close>
   464 subsection \<open>Locale expressions \label{sec:locale-expr}\<close>
   465 
   465 
   466 text \<open>
   466 text \<open>
   467   A \emph{locale expression} denotes a context composed of instances
   467   A \<^emph>\<open>locale expression\<close> denotes a context composed of instances
   468   of existing locales.  The context consists of the declaration
   468   of existing locales.  The context consists of the declaration
   469   elements from the locale instances.  Redundant locale instances are
   469   elements from the locale instances.  Redundant locale instances are
   470   omitted according to roundup.
   470   omitted according to roundup.
   471 
   471 
   472   @{rail \<open>
   472   @{rail \<open>
   663     @{command_def "print_interps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
   663     @{command_def "print_interps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
   664   \end{matharray}
   664   \end{matharray}
   665 
   665 
   666   Locales may be instantiated, and the resulting instantiated
   666   Locales may be instantiated, and the resulting instantiated
   667   declarations added to the current context.  This requires proof (of
   667   declarations added to the current context.  This requires proof (of
   668   the instantiated specification) and is called \emph{locale
   668   the instantiated specification) and is called \<^emph>\<open>locale
   669   interpretation}.  Interpretation is possible in locales (@{command
   669   interpretation\<close>.  Interpretation is possible in locales (@{command
   670   "sublocale"}), global and local theories (@{command
   670   "sublocale"}), global and local theories (@{command
   671   "interpretation"}) and also within proof bodies (@{command
   671   "interpretation"}) and also within proof bodies (@{command
   672   "interpret"}).
   672   "interpret"}).
   673 
   673 
   674   @{rail \<open>
   674   @{rail \<open>
   721   turned into schematic variables in the generated declarations.  In
   721   turned into schematic variables in the generated declarations.  In
   722   order to use a free variable whose name is already bound in the
   722   order to use a free variable whose name is already bound in the
   723   context --- for example, because a constant of that name exists ---
   723   context --- for example, because a constant of that name exists ---
   724   add it to the @{keyword "for"} clause.
   724   add it to the @{keyword "for"} clause.
   725 
   725 
   726   The equations @{text eqns} yield \emph{rewrite morphisms}, which are
   726   The equations @{text eqns} yield \<^emph>\<open>rewrite morphisms\<close>, which are
   727   unfolded during post-processing and are useful for interpreting
   727   unfolded during post-processing and are useful for interpreting
   728   concepts introduced through definitions.  The equations must be
   728   concepts introduced through definitions.  The equations must be
   729   proved.
   729   proved.
   730 
   730 
   731   \<^descr> @{command "interpret"}~@{text "expr \<WHERE> eqns"} interprets
   731   \<^descr> @{command "interpret"}~@{text "expr \<WHERE> eqns"} interprets
   793 
   793 
   794   \begin{warn}
   794   \begin{warn}
   795     Since attributes are applied to interpreted theorems,
   795     Since attributes are applied to interpreted theorems,
   796     interpretation may modify the context of common proof tools, e.g.\
   796     interpretation may modify the context of common proof tools, e.g.\
   797     the Simplifier or Classical Reasoner.  As the behavior of such
   797     the Simplifier or Classical Reasoner.  As the behavior of such
   798     tools is \emph{not} stable under interpretation morphisms, manual
   798     tools is \<^emph>\<open>not\<close> stable under interpretation morphisms, manual
   799     declarations might have to be added to the target context of the
   799     declarations might have to be added to the target context of the
   800     interpretation to revert such declarations.
   800     interpretation to revert such declarations.
   801   \end{warn}
   801   \end{warn}
   802 
   802 
   803   \begin{warn}
   803   \begin{warn}
   819   available by importing theory @{file "~~/src/Tools/Permanent_Interpretation.thy"}
   819   available by importing theory @{file "~~/src/Tools/Permanent_Interpretation.thy"}
   820   and provides
   820   and provides
   821 
   821 
   822   \<^enum> a unified view on arbitrary suitable local theories as interpretation target;
   822   \<^enum> a unified view on arbitrary suitable local theories as interpretation target;
   823 
   823 
   824   \<^enum> rewrite morphisms by means of \emph{rewrite definitions}.
   824   \<^enum> rewrite morphisms by means of \<^emph>\<open>rewrite definitions\<close>.
   825 
   825 
   826   
   826   
   827   \begin{matharray}{rcl}
   827   \begin{matharray}{rcl}
   828     @{command_def "permanent_interpretation"} & : & @{text "local_theory \<rightarrow> proof(prove)"}
   828     @{command_def "permanent_interpretation"} & : & @{text "local_theory \<rightarrow> proof(prove)"}
   829   \end{matharray}
   829   \end{matharray}
   855   (where @{command "permanent_interpretation"} is technically
   855   (where @{command "permanent_interpretation"} is technically
   856   corresponding to @{command "sublocale"}).  Unnamed contexts
   856   corresponding to @{command "sublocale"}).  Unnamed contexts
   857   (see \secref{sec:target}) are not admissible since they are
   857   (see \secref{sec:target}) are not admissible since they are
   858   non-permanent due to their very nature.  
   858   non-permanent due to their very nature.  
   859 
   859 
   860   In additions to \emph{rewrite morphisms} specified by @{text eqns},
   860   In additions to \<^emph>\<open>rewrite morphisms\<close> specified by @{text eqns},
   861   also \emph{rewrite definitions} may be specified.  Semantically, a
   861   also \<^emph>\<open>rewrite definitions\<close> may be specified.  Semantically, a
   862   rewrite definition
   862   rewrite definition
   863   
   863   
   864     \<^item> produces a corresponding definition in
   864     \<^item> produces a corresponding definition in
   865     the local theory's underlying target \emph{and}
   865     the local theory's underlying target \<^emph>\<open>and\<close>
   866 
   866 
   867     \<^item> augments the rewrite morphism with the equation
   867     \<^item> augments the rewrite morphism with the equation
   868     stemming from the symmetric of the corresponding definition.
   868     stemming from the symmetric of the corresponding definition.
   869   
   869   
   870   This is technically different to to a naive combination
   870   This is technically different to to a naive combination
   893     @{command_def "print_classes"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
   893     @{command_def "print_classes"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
   894     @{command_def "class_deps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
   894     @{command_def "class_deps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
   895     @{method_def intro_classes} & : & @{text method} \\
   895     @{method_def intro_classes} & : & @{text method} \\
   896   \end{matharray}
   896   \end{matharray}
   897 
   897 
   898   A class is a particular locale with \emph{exactly one} type variable
   898   A class is a particular locale with \<^emph>\<open>exactly one\<close> type variable
   899   @{text \<alpha>}.  Beyond the underlying locale, a corresponding type class
   899   @{text \<alpha>}.  Beyond the underlying locale, a corresponding type class
   900   is established which is interpreted logically as axiomatic type
   900   is established which is interpreted logically as axiomatic type
   901   class @{cite "Wenzel:1997:TPHOL"} whose logical content are the
   901   class @{cite "Wenzel:1997:TPHOL"} whose logical content are the
   902   assumptions of the locale.  Thus, classes provide the full
   902   assumptions of the locale.  Thus, classes provide the full
   903   generality of locales combined with the commodity of type classes
   903   generality of locales combined with the commodity of type classes
   927   a new class @{text c}, inheriting from @{text superclasses}.  This
   927   a new class @{text c}, inheriting from @{text superclasses}.  This
   928   introduces a locale @{text c} with import of all locales @{text
   928   introduces a locale @{text c} with import of all locales @{text
   929   superclasses}.
   929   superclasses}.
   930 
   930 
   931   Any @{element "fixes"} in @{text body} are lifted to the global
   931   Any @{element "fixes"} in @{text body} are lifted to the global
   932   theory level (\emph{class operations} @{text "f\<^sub>1, \<dots>,
   932   theory level (\<^emph>\<open>class operations\<close> @{text "f\<^sub>1, \<dots>,
   933   f\<^sub>n"} of class @{text c}), mapping the local type parameter
   933   f\<^sub>n"} of class @{text c}), mapping the local type parameter
   934   @{text \<alpha>} to a schematic type variable @{text "?\<alpha> :: c"}.
   934   @{text \<alpha>} to a schematic type variable @{text "?\<alpha> :: c"}.
   935 
   935 
   936   Likewise, @{element "assumes"} in @{text body} are also lifted,
   936   Likewise, @{element "assumes"} in @{text body} are also lifted,
   937   mapping each local parameter @{text "f :: \<tau>[\<alpha>]"} to its
   937   mapping each local parameter @{text "f :: \<tau>[\<alpha>]"} to its
  1023 
  1023 
  1024 subsection \<open>Co-regularity of type classes and arities\<close>
  1024 subsection \<open>Co-regularity of type classes and arities\<close>
  1025 
  1025 
  1026 text \<open>The class relation together with the collection of
  1026 text \<open>The class relation together with the collection of
  1027   type-constructor arities must obey the principle of
  1027   type-constructor arities must obey the principle of
  1028   \emph{co-regularity} as defined below.
  1028   \<^emph>\<open>co-regularity\<close> as defined below.
  1029 
  1029 
  1030   \<^medskip>
  1030   \<^medskip>
  1031   For the subsequent formulation of co-regularity we assume
  1031   For the subsequent formulation of co-regularity we assume
  1032   that the class relation is closed by transitivity and reflexivity.
  1032   that the class relation is closed by transitivity and reflexivity.
  1033   Moreover the collection of arities @{text "t :: (\<^vec>s)c"} is
  1033   Moreover the collection of arities @{text "t :: (\<^vec>s)c"} is
  1267     ;
  1267     ;
  1268     @@{command typedecl} @{syntax typespec} @{syntax mixfix}?
  1268     @@{command typedecl} @{syntax typespec} @{syntax mixfix}?
  1269   \<close>}
  1269   \<close>}
  1270 
  1270 
  1271   \<^descr> @{command "type_synonym"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t = \<tau>"} introduces a
  1271   \<^descr> @{command "type_synonym"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t = \<tau>"} introduces a
  1272   \emph{type synonym} @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} for the existing type @{text
  1272   \<^emph>\<open>type synonym\<close> @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} for the existing type @{text
  1273   "\<tau>"}. Unlike the semantic type definitions in Isabelle/HOL, type synonyms
  1273   "\<tau>"}. Unlike the semantic type definitions in Isabelle/HOL, type synonyms
  1274   are merely syntactic abbreviations without any logical significance.
  1274   are merely syntactic abbreviations without any logical significance.
  1275   Internally, type synonyms are fully expanded.
  1275   Internally, type synonyms are fully expanded.
  1276   
  1276   
  1277   \<^descr> @{command "typedecl"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} declares a new
  1277   \<^descr> @{command "typedecl"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} declares a new