src/HOL/ex/Locales.thy
changeset 13538 359c085c4def
parent 13457 7ddcf40a80b3
child 14254 342634f38451
equal deleted inserted replaced
13537:f506eb568121 13538:359c085c4def
    17 subsection {* Overview *}
    17 subsection {* Overview *}
    18 
    18 
    19 text {*
    19 text {*
    20   Locales provide a mechanism for encapsulating local contexts.  The
    20   Locales provide a mechanism for encapsulating local contexts.  The
    21   original version due to Florian Kammüller \cite{Kamm-et-al:1999}
    21   original version due to Florian Kammüller \cite{Kamm-et-al:1999}
    22   refers directly to the raw meta-logic of Isabelle.  Semantically, a
    22   refers directly to Isabelle's meta-logic \cite{Paulson:1989}, which
    23   locale is just a (curried) predicate of the pure meta-logic
    23   is minimal higher-order logic with connectives @{text "\<And>"}
    24   \cite{Paulson:1989}.
    24   (universal quantification), @{text "\<Longrightarrow>"} (implication), and @{text
    25 
    25   "\<equiv>"} (equality).
    26   The present version for Isabelle/Isar builds on top of the rich
    26 
    27   infrastructure of proof contexts
    27   From this perspective, a locale is essentially a meta-level
    28   \cite{Wenzel:1999,Wenzel:2001:isar-ref,Wenzel:2001:Thesis},
    28   predicate, together with some infrastructure to manage the
    29   achieving a tight integration with Isar proof texts, and a slightly
    29   abstracted parameters (@{text "\<And>"}), assumptions (@{text "\<Longrightarrow>"}), and
    30   more abstract view of the underlying concepts.  An Isar proof
    30   definitions for (@{text "\<equiv>"}) in a reasonable way during the proof
    31   context encapsulates certain language elements that correspond to
    31   process.  This simple predicate view also provides a solid
    32   @{text \<And>} (universal quantification), @{text \<Longrightarrow>} (implication), and
    32   semantical basis for our specification concepts to be developed
    33   @{text "\<equiv>"} (definitions) of the pure Isabelle framework.  Moreover,
    33   later.
    34   there are extra-logical concepts like term abbreviations or local
    34 
    35   theorem attributes (declarations of simplification rules etc.) that
    35   \medskip The present version of locales for Isabelle/Isar builds on
    36   are indispensable in realistic applications.
    36   top of the rich infrastructure of proof contexts
    37 
    37   \cite{Wenzel:1999,Wenzel:2001:isar-ref,Wenzel:2001:Thesis}, which in
    38   Locales support concrete syntax, providing a localized version of
    38   turn is based on the same meta-logic.  Thus we achieve a tight
       
    39   integration with Isar proof texts, and a slightly more abstract view
       
    40   of the underlying logical concepts.  An Isar proof context
       
    41   encapsulates certain language elements that correspond to @{text
       
    42   "\<And>/\<Longrightarrow>/\<equiv>"} at the level of structure proof texts.  Moreover, there are
       
    43   extra-logical concepts like term abbreviations or local theorem
       
    44   attributes (declarations of simplification rules etc.) that are
       
    45   useful in applications (e.g.\ consider standard simplification rules
       
    46   declared in a group context).
       
    47 
       
    48   Locales also support concrete syntax, i.e.\ a localized version of
    39   the existing concept of mixfix annotations of Isabelle
    49   the existing concept of mixfix annotations of Isabelle
    40   \cite{Paulson:1994:Isabelle}.  Furthermore, there is a separate
    50   \cite{Paulson:1994:Isabelle}.  Furthermore, there is a separate
    41   concept of ``implicit structures'' that admits to refer to
    51   concept of ``implicit structures'' that admits to refer to
    42   particular locale parameters in a casual manner (essentially derived
    52   particular locale parameters in a casual manner (basically a
    43   from the basic idea of ``anti-quotations'' or generalized de-Bruijn
    53   simplified version of the idea of ``anti-quotations'', or
    44   indexes demonstrated in \cite[\S13--14]{Wenzel:2001:Isar-examples}).
    54   generalized de-Bruijn indexes as demonstrated elsewhere
       
    55   \cite[\S13--14]{Wenzel:2001:Isar-examples}).
    45 
    56 
    46   Implicit structures work particular well together with extensible
    57   Implicit structures work particular well together with extensible
    47   records in HOL \cite{Naraschewski-Wenzel:1998} (the
    58   records in HOL \cite{Naraschewski-Wenzel:1998} (without the
    48   ``object-oriented'' features discussed there are not required here).
    59   ``object-oriented'' features discussed there as well).  Thus we
    49   Thus we shall essentially use record types to represent polymorphic
    60   achieve a specification technique where record type schemes
    50   signatures of mathematical structures, while locales describe their
    61   represent polymorphic signatures of mathematical structures, and
    51   logical properties as a predicate.  Due to type inference of
    62   actual locales describe the corresponding logical properties.
    52   simply-typed records we are able to give succinct specifications,
    63   Semantically speaking, such abstract mathematical structures are
    53   without caring about signature morphisms ourselves.  Further
    64   just predicates over record types.  Due to type inference of
    54   eye-candy is provided by the independent concept of ``indexed
    65   simply-typed records (which subsumes structural subtyping) we arrive
    55   concrete syntax'' for record selectors.
    66   at succinct specification texts --- ``signature morphisms''
    56 
    67   degenerate to implicit type-instantiations.  Additional eye-candy is
    57   Operations for building up locale contexts from existing definitions
    68   provided by the separate concept of ``indexed concrete syntax'' used
    58   cover common operations of \emph{merge} (disjunctive union in
    69   for record selectors, so we get close to informal mathematical
    59   canonical order) and \emph{rename} (of term parameters; types are
    70   notation.
    60   always inferred automatically).
    71 
    61 
    72   \medskip Operations for building up locale contexts from existing
    62   \medskip Note that the following further concepts are still
    73   ones include \emph{merge} (disjoint union) and \emph{rename} (of
    63   \emph{absent}:
    74   term parameters only, types are inferred automatically).  Here we
       
    75   draw from existing traditions of algebraic specification languages.
       
    76   A structured specification corresponds to a directed acyclic graph
       
    77   of potentially renamed nodes (due to distributivity renames may
       
    78   pushed inside of merges).  The result is a ``flattened'' list of
       
    79   primitive context elements in canonical order (corresponding to
       
    80   left-to-right reading of merges, while suppressing duplicates).
       
    81 
       
    82   \medskip The present version of Isabelle/Isar locales still lacks
       
    83   some important specification concepts.
       
    84 
    64   \begin{itemize}
    85   \begin{itemize}
    65 
    86 
    66   \item Specific language elements for \emph{instantiation} of
    87   \item Separate language elements for \emph{instantiation} of
    67   locales.
    88   locales.
    68 
    89 
    69   Currently users may simulate this to some extend by having primitive
    90   Currently users may simulate this to some extend by having primitive
    70   Isabelle/Isar operations (@{text of} for substitution and @{text OF}
    91   Isabelle/Isar operations (@{text of} for substitution and @{text OF}
    71   for composition, \cite{Wenzel:2001:isar-ref}) act on the
    92   for composition, \cite{Wenzel:2001:isar-ref}) act on the
    72   automatically exported results stemming from different contexts.
    93   automatically exported results stemming from different contexts.
    73 
    94 
    74   \item Interpretation of locales, analogous to ``functors'' in
    95   \item Interpretation of locales (think of ``views'', ``functors''
    75   abstract algebra.
    96   etc.).
    76 
    97 
    77   In principle one could directly work with functions over structures
    98   In principle one could directly work with functions over structures
    78   (extensible records), and predicates being derived from locale
    99   (extensible records), and predicates being derived from locale
    79   definitions.
   100   definitions.
    80 
   101 
    83   \medskip Subsequently, we demonstrate some readily available
   104   \medskip Subsequently, we demonstrate some readily available
    84   concepts of Isabelle/Isar locales by some simple examples of
   105   concepts of Isabelle/Isar locales by some simple examples of
    85   abstract algebraic reasoning.
   106   abstract algebraic reasoning.
    86 *}
   107 *}
    87 
   108 
       
   109 
    88 subsection {* Local contexts as mathematical structures *}
   110 subsection {* Local contexts as mathematical structures *}
    89 
   111 
    90 text {*
   112 text {*
    91   The following definitions of @{text group} and @{text abelian_group}
   113   The following definitions of @{text group_context} and @{text
    92   merely encapsulate local parameters (with private syntax) and
   114   abelian_group_context} merely encapsulate local parameters (with
    93   assumptions; local definitions may be given as well, but are not
   115   private syntax) and assumptions; local definitions of derived
    94   shown here.
   116   concepts could be given, too, but are unused below.
    95 *}
   117 *}
    96 
   118 
    97 locale group_context =
   119 locale group_context =
    98   fixes prod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"    (infixl "\<cdot>" 70)
   120   fixes prod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"    (infixl "\<cdot>" 70)
    99     and inv :: "'a \<Rightarrow> 'a"    ("(_\<inv>)" [1000] 999)
   121     and inv :: "'a \<Rightarrow> 'a"    ("(_\<inv>)" [1000] 999)
   107 
   129 
   108 text {*
   130 text {*
   109   \medskip We may now prove theorems within a local context, just by
   131   \medskip We may now prove theorems within a local context, just by
   110   including a directive ``@{text "(\<IN> name)"}'' in the goal
   132   including a directive ``@{text "(\<IN> name)"}'' in the goal
   111   specification.  The final result will be stored within the named
   133   specification.  The final result will be stored within the named
   112   locale; a second copy is exported to the enclosing theory context.
   134   locale, still holding the context; a second copy is exported to the
       
   135   enclosing theory context (with qualified name).
   113 *}
   136 *}
   114 
   137 
   115 theorem (in group_context)
   138 theorem (in group_context)
   116   right_inv: "x \<cdot> x\<inv> = \<one>"
   139   right_inv: "x \<cdot> x\<inv> = \<one>"
   117 proof -
   140 proof -
   135   also have "\<dots> = x" by (simp only: left_one)
   158   also have "\<dots> = x" by (simp only: left_one)
   136   finally show ?thesis .
   159   finally show ?thesis .
   137 qed
   160 qed
   138 
   161 
   139 text {*
   162 text {*
   140   \medskip Apart from the named context we may also refer to further
   163   Facts like @{text right_one} are available @{text group_context} as
   141   context elements (parameters, assumptions, etc.) in a casual manner;
   164   stated above.  The exported version looses the additional
   142   these are discharged when the proof is finished.
   165   infrastructure of Isar proof contexts (syntax etc.) retaining only
       
   166   the pure logical content: @{thm [source] group_context.right_one}
       
   167   becomes @{thm group_context.right_one} (in Isabelle outermost @{text
       
   168   \<And>} quantification is replaced by schematic variables).
       
   169 
       
   170   \medskip Apart from a named locale we may also refer to further
       
   171   context elements (parameters, assumptions, etc.) in an ad-hoc
       
   172   fashion, just for this particular statement.  In the result (local
       
   173   or global), any additional elements are discharged as usual.
   143 *}
   174 *}
   144 
   175 
   145 theorem (in group_context)
   176 theorem (in group_context)
   146   assumes eq: "e \<cdot> x = x"
   177   assumes eq: "e \<cdot> x = x"
   147   shows one_equality: "\<one> = e"
   178   shows one_equality: "\<one> = e"
   369   For example, the direct product of semigroups works as follows.
   400   For example, the direct product of semigroups works as follows.
   370 *}
   401 *}
   371 
   402 
   372 constdefs
   403 constdefs
   373   semigroup_product :: "'a semigroup \<Rightarrow> 'b semigroup \<Rightarrow> ('a \<times> 'b) semigroup"
   404   semigroup_product :: "'a semigroup \<Rightarrow> 'b semigroup \<Rightarrow> ('a \<times> 'b) semigroup"
   374   "semigroup_product S T \<equiv> \<lparr>prod = \<lambda>p q. (prod S (fst p) (fst q), prod T (snd p) (snd q))\<rparr>"
   405   "semigroup_product S T \<equiv>
       
   406     \<lparr>prod = \<lambda>p q. (prod S (fst p) (fst q), prod T (snd p) (snd q))\<rparr>"
   375 
   407 
   376 lemma semigroup_product [intro]:
   408 lemma semigroup_product [intro]:
   377   assumes S: "semigroup S"
   409   assumes S: "semigroup S"
   378     and T: "semigroup T"
   410     and T: "semigroup T"
   379   shows "semigroup (semigroup_product S T)"
   411   shows "semigroup (semigroup_product S T)"
   383       prod S (fst p) (prod S (fst q) (fst r))"
   415       prod S (fst p) (prod S (fst q) (fst r))"
   384     by (rule semigroup.assoc [OF S])
   416     by (rule semigroup.assoc [OF S])
   385   moreover have "prod T (prod T (snd p) (snd q)) (snd r) =
   417   moreover have "prod T (prod T (snd p) (snd q)) (snd r) =
   386       prod T (snd p) (prod T (snd q) (snd r))"
   418       prod T (snd p) (prod T (snd q) (snd r))"
   387     by (rule semigroup.assoc [OF T])
   419     by (rule semigroup.assoc [OF T])
   388   ultimately show "prod (semigroup_product S T) (prod (semigroup_product S T) p q) r =
   420   ultimately
       
   421   show "prod (semigroup_product S T) (prod (semigroup_product S T) p q) r =
   389       prod (semigroup_product S T) p (prod (semigroup_product S T) q r)"
   422       prod (semigroup_product S T) p (prod (semigroup_product S T) q r)"
   390     by (simp add: semigroup_product_def)
   423     by (simp add: semigroup_product_def)
   391 qed
   424 qed
   392 
   425 
   393 text {*
   426 text {*
   440 proof
   473 proof
   441   show "semigroup I"
   474   show "semigroup I"
   442   proof -
   475   proof -
   443     let ?G' = "semigroup.truncate G" and ?H' = "semigroup.truncate H"
   476     let ?G' = "semigroup.truncate G" and ?H' = "semigroup.truncate H"
   444     have "prod (semigroup_product ?G' ?H') = prod I"
   477     have "prod (semigroup_product ?G' ?H') = prod I"
   445       by (simp add: I_def group_product_def semigroup_product_def group.defs semigroup.defs)
   478       by (simp add: I_def group_product_def group.defs
       
   479 	semigroup_product_def semigroup.defs)
   446     moreover
   480     moreover
   447     have "semigroup ?G'" and "semigroup ?H'"
   481     have "semigroup ?G'" and "semigroup ?H'"
   448       using prems by (simp_all add: semigroup_def semigroup.defs)
   482       using prems by (simp_all add: semigroup_def semigroup.defs)
   449     then have "semigroup (semigroup_product ?G' ?H')" ..
   483     then have "semigroup (semigroup_product ?G' ?H')" ..
   450     ultimately show ?thesis by (simp add: I_def semigroup_def)
   484     ultimately show ?thesis by (simp add: I_def semigroup_def)
   455     have "(fst p)\<inv>\<^sub>1 \<cdot>\<^sub>1 fst p = \<one>\<^sub>1"
   489     have "(fst p)\<inv>\<^sub>1 \<cdot>\<^sub>1 fst p = \<one>\<^sub>1"
   456       by (rule G.left_inv)
   490       by (rule G.left_inv)
   457     moreover have "(snd p)\<inv>\<^sub>2 \<cdot>\<^sub>2 snd p = \<one>\<^sub>2"
   491     moreover have "(snd p)\<inv>\<^sub>2 \<cdot>\<^sub>2 snd p = \<one>\<^sub>2"
   458       by (rule H.left_inv)
   492       by (rule H.left_inv)
   459     ultimately show "p\<inv>\<^sub>3 \<cdot>\<^sub>3 p = \<one>\<^sub>3"
   493     ultimately show "p\<inv>\<^sub>3 \<cdot>\<^sub>3 p = \<one>\<^sub>3"
   460       by (simp add: I_def group_product_def semigroup_product_def group.defs semigroup.defs)
   494       by (simp add: I_def group_product_def group.defs
       
   495 	semigroup_product_def semigroup.defs)
   461     have "\<one>\<^sub>1 \<cdot>\<^sub>1 fst p = fst p" by (rule G.left_one)
   496     have "\<one>\<^sub>1 \<cdot>\<^sub>1 fst p = fst p" by (rule G.left_one)
   462     moreover have "\<one>\<^sub>2 \<cdot>\<^sub>2 snd p = snd p" by (rule H.left_one)
   497     moreover have "\<one>\<^sub>2 \<cdot>\<^sub>2 snd p = snd p" by (rule H.left_one)
   463     ultimately show "\<one>\<^sub>3 \<cdot>\<^sub>3 p = p"
   498     ultimately show "\<one>\<^sub>3 \<cdot>\<^sub>3 p = p"
   464       by (simp add: I_def group_product_def semigroup_product_def group.defs semigroup.defs)
   499       by (simp add: I_def group_product_def group.defs
       
   500 	semigroup_product_def semigroup.defs)
   465   qed
   501   qed
   466 qed
   502 qed
   467 
   503 
   468 theorem group_product: "group G \<Longrightarrow> group H \<Longrightarrow> group (group_product G H)"
   504 theorem group_product: "group G \<Longrightarrow> group H \<Longrightarrow> group (group_product G H)"
   469   by (rule group_product_aux) (assumption | rule group.axioms)+
   505   by (rule group_product_aux) (assumption | rule group.axioms)+