src/HOL/ex/Locales.thy
changeset 12105 1e4451999200
parent 12099 8504c948fae2
child 12356 ce0961b1f536
equal deleted inserted replaced
12104:c058fd42b5fc 12105:1e4451999200
     7 header {* Basic use of locales in Isabelle/Isar *}
     7 header {* Basic use of locales in Isabelle/Isar *}
     8 
     8 
     9 theory Locales = Main:
     9 theory Locales = Main:
    10 
    10 
    11 text {*
    11 text {*
    12   Locales provide a mechanism for encapsulating local contexts.  While
    12   Locales provide a mechanism for encapsulating local contexts.  The
    13   the original version by Florian Kamm\"uller refers to the raw
    13   original version due to Florian Kamm\"uller \cite{Kamm-et-al:1999}
    14   meta-logic, the present one of Isabelle/Isar builds on top of the
    14   refers directly to the raw meta-logic of Isabelle.  The present
    15   rich infrastructure of Isar proof contexts.  Subsequently, we
    15   version for Isabelle/Isar
    16   demonstrate basic use of locales to model mathematical structures
    16   \cite{Wenzel:1999,Wenzel:2001:isar-ref,Wenzel:2001:Thesis} builds on
    17   (by the inevitable group theory example).
    17   top of the rich infrastructure of proof contexts instead; this also
       
    18   covers essential extra-logical concepts like term abbreviations or
       
    19   local theorem attributes (e.g.\ declarations of simplification
       
    20   rules).
       
    21 
       
    22   Subsequently, we demonstrate basic use of locales to model
       
    23   mathematical structures (by the inevitable group theory example).
    18 *}
    24 *}
    19 
    25 
    20 text_raw {*
    26 text_raw {*
    21   \newcommand{\isasyminv}{\isasyminverse}
    27   \newcommand{\isasyminv}{\isasyminverse}
    22   \newcommand{\isasymone}{\isamath{1}}
    28   \newcommand{\isasymone}{\isamath{1}}
    26 
    32 
    27 subsection {* Local contexts as mathematical structures *}
    33 subsection {* Local contexts as mathematical structures *}
    28 
    34 
    29 text {*
    35 text {*
    30   The following definitions of @{text group} and @{text abelian_group}
    36   The following definitions of @{text group} and @{text abelian_group}
    31   simply encapsulate local parameters (with private syntax) and
    37   merely encapsulate local parameters (with private syntax) and
    32   assumptions; local definitions may be given as well, but are not
    38   assumptions; local definitions may be given as well, but are not
    33   shown here.
    39   shown here.
    34 *}
    40 *}
    35 
    41 
    36 locale group =
    42 locale group =
    74   also have "\<dots> = x" by (simp only: left_one)
    80   also have "\<dots> = x" by (simp only: left_one)
    75   finally show ?thesis .
    81   finally show ?thesis .
    76 qed
    82 qed
    77 
    83 
    78 text {*
    84 text {*
    79   \medskip Apart from the named locale context we may also refer to
    85   \medskip Apart from the named context we may also refer to further
    80   further ad-hoc elements (parameters, assumptions, etc.); these are
    86   context elements (parameters, assumptions, etc.) in a casual manner;
    81   discharged when the proof is finished.
    87   these are discharged when the proof is finished.
    82 *}
    88 *}
    83 
    89 
    84 theorem (in group)
    90 theorem (in group)
    85   (assumes eq: "e \<cdot> x = x")
    91   (assumes eq: "e \<cdot> x = x")
    86   one_equality: "\<one> = e"
    92   one_equality: "\<one> = e"
   117     finally show ?thesis .
   123     finally show ?thesis .
   118   qed
   124   qed
   119 qed
   125 qed
   120 
   126 
   121 text {*
   127 text {*
   122   \medskip Results are automatically propagated through the hierarchy
   128   \medskip Established results are automatically propagated through
   123   of locales.  Below we establish a trivial fact of commutative
   129   the hierarchy of locales.  Below we establish a trivial fact in
   124   groups, while referring both to theorems of @{text group} and the
   130   commutative groups, while referring both to theorems of @{text
   125   characteristic assumption of @{text abelian_group}.
   131   group} and the additional assumption of @{text abelian_group}.
   126 *}
   132 *}
   127 
   133 
   128 theorem (in abelian_group)
   134 theorem (in abelian_group)
   129   inv_prod': "(x \<cdot> y)\<inv> = x\<inv> \<cdot> y\<inv>"
   135   inv_prod': "(x \<cdot> y)\<inv> = x\<inv> \<cdot> y\<inv>"
   130 proof -
   136 proof -
   132   also have "\<dots> = x\<inv> \<cdot> y\<inv>" by (rule commute)
   138   also have "\<dots> = x\<inv> \<cdot> y\<inv>" by (rule commute)
   133   finally show ?thesis .
   139   finally show ?thesis .
   134 qed
   140 qed
   135 
   141 
   136 text {*
   142 text {*
   137   \medskip Some further facts of general group theory follow.
   143   \medskip Some further properties of inversion in general group
       
   144   theory follow.
   138 *}
   145 *}
   139 
   146 
   140 theorem (in group)
   147 theorem (in group)
   141   inv_inv: "(x\<inv>)\<inv> = x"
   148   inv_inv: "(x\<inv>)\<inv> = x"
   142 proof (rule inv_equality)
   149 proof (rule inv_equality)
   173   terminology of local parameters is affected the associated syntax
   180   terminology of local parameters is affected the associated syntax
   174   would have to be changed as well, which is hard to achieve formally.
   181   would have to be changed as well, which is hard to achieve formally.
   175 *}
   182 *}
   176 
   183 
   177 
   184 
   178 subsection {* Referencing explicit structures implicitly *}
   185 subsection {* Explicit structures referenced implicitly *}
   179 
   186 
   180 text {*
   187 text {*
   181   The issue of multiple parameters raised above may be easily
   188   The issue of multiple parameters raised above may be easily
   182   addressed by stating properties over a record of group operations,
   189   addressed by stating properties over a record of group operations,
   183   instead of the individual constituents.
   190   instead of the individual constituents.  See
       
   191   \cite{Naraschewski-Wenzel:1998,Nipkow-et-al:2001:HOL} on
       
   192   (extensible) record types in Isabelle/HOL.
   184 *}
   193 *}
   185 
   194 
   186 record 'a group =
   195 record 'a group =
   187   prod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   196   prod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   188   inv :: "'a \<Rightarrow> 'a"
   197   inv :: "'a \<Rightarrow> 'a"
   195 
   204 
   196   In the subsequent formulation of group structures we go one step
   205   In the subsequent formulation of group structures we go one step
   197   further by using \emph{implicit} references to the underlying
   206   further by using \emph{implicit} references to the underlying
   198   abstract entity.  To this end we define global syntax, which is
   207   abstract entity.  To this end we define global syntax, which is
   199   translated to refer to the ``current'' structure at hand, denoted by
   208   translated to refer to the ``current'' structure at hand, denoted by
   200   the dummy item ``@{text \<struct>}'' according to the builtin syntax
   209   the dummy item ``@{text \<struct>}'' (according to the builtin syntax
   201   mechanism for locales.
   210   mechanism for locales).
   202 *}
   211 *}
   203 
   212 
   204 syntax
   213 syntax
   205   "_prod" :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"    (infixl "\<cdot>" 70)
   214   "_prod" :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"    (infixl "\<cdot>" 70)
   206   "_inv" :: "'a \<Rightarrow> 'a"    ("(_\<inv>)" [1000] 999)
   215   "_inv" :: "'a \<Rightarrow> 'a"    ("(_\<inv>)" [1000] 999)
   210   "x\<inv>"  \<rightleftharpoons>  "inv \<struct> x"
   219   "x\<inv>"  \<rightleftharpoons>  "inv \<struct> x"
   211   "\<one>"  \<rightleftharpoons>  "one \<struct>"
   220   "\<one>"  \<rightleftharpoons>  "one \<struct>"
   212 
   221 
   213 text {*
   222 text {*
   214   The following locale definition introduces a single parameter, which
   223   The following locale definition introduces a single parameter, which
   215   is declared as ``\isakeyword{structure}''.
   224   is declared as a ``\isakeyword{structure}''. In its context the
       
   225   dummy ``@{text \<struct>}'' refers to this very parameter, independently of
       
   226   the present naming.
   216 *}
   227 *}
   217 
   228 
   218 locale group_struct =
   229 locale group_struct =
   219   fixes G :: "'a group"    (structure)
   230   fixes G :: "'a group"    (structure)
   220   assumes assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
   231   assumes assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
   221     and left_inv: "x\<inv> \<cdot> x = \<one>"
   232     and left_inv: "x\<inv> \<cdot> x = \<one>"
   222     and left_one: "\<one> \<cdot> x = x"
   233     and left_one: "\<one> \<cdot> x = x"
   223 
   234 
   224 text {*
   235 text {*
   225   In its context the dummy ``@{text \<struct>}'' refers to this very
   236   We may now proceed to prove results within @{text group_struct} just
   226   parameter, independently of the present naming.  We may now proceed
   237   as before for @{text group}.  Proper treatment of ``@{text \<struct>}'' is
   227   to prove results within @{text group_struct} just as before for
   238   hidden in concrete syntax, so the proof text is exactly the same as
   228   @{text group}.  Proper treatment of ``@{text \<struct>}'' is hidden in
   239   before.
   229   concrete syntax, so the proof text is exactly the same as for @{text
       
   230   group} given before.
       
   231 *}
   240 *}
   232 
   241 
   233 theorem (in group_struct)
   242 theorem (in group_struct)
   234   right_inv: "x \<cdot> x\<inv> = \<one>"
   243   right_inv: "x \<cdot> x\<inv> = \<one>"
   235 proof -
   244 proof -
   276   "\<one>\<^sub>3"  \<rightleftharpoons>  "one \<struct>\<^sub>3"
   285   "\<one>\<^sub>3"  \<rightleftharpoons>  "one \<struct>\<^sub>3"
   277 
   286 
   278 text {*
   287 text {*
   279   \medskip The following synthetic example demonstrates how to refer
   288   \medskip The following synthetic example demonstrates how to refer
   280   to several structures of type @{text group} succinctly; one might
   289   to several structures of type @{text group} succinctly; one might
   281   also think of working with renamed versions of the @{text
   290   also think of working with several renamed versions of the @{text
   282   group_struct} locale above.
   291   group_struct} locale above.
   283 *}
   292 *}
   284 
   293 
   285 lemma
   294 lemma
   286   (fixes G :: "'a group" (structure)
   295   (fixes G :: "'a group" (structure)