doc-src/AxClass/Group/Group.thy
changeset 8903 78d6e47469e4
parent 8890 9a44d8d98731
child 8907 813fabceec00
equal deleted inserted replaced
8902:a705822f4e2a 8903:78d6e47469e4
       
     1 
       
     2 header {* Basic group theory *};
     1 
     3 
     2 theory Group = Main:;
     4 theory Group = Main:;
     3 
     5 
       
     6 text {*
       
     7  \medskip\noindent The meta type system of Isabelle supports
       
     8  \emph{intersections} and \emph{inclusions} of type classes. These
       
     9  directly correspond to intersections and inclusions of type
       
    10  predicates in a purely set theoretic sense. This is sufficient as a
       
    11  means to describe simple hierarchies of structures.  As an
       
    12  illustration, we use the well-known example of semigroups, monoids,
       
    13  general groups and abelian groups.
       
    14 *};
       
    15 
       
    16 subsection {* Monoids and Groups *};
       
    17 
       
    18 text {*
       
    19  First we declare some polymorphic constants required later for the
       
    20  signature parts of our structures.
       
    21 *};
     4 
    22 
     5 consts
    23 consts
     6   times :: "'a => 'a => 'a"    (infixl "\<Otimes>" 70)
    24   times :: "'a => 'a => 'a"    (infixl "\<Otimes>" 70)
     7   inverse :: "'a => 'a"        ("(_\<inv>)" [1000] 999)
    25   inverse :: "'a => 'a"        ("(_\<inv>)" [1000] 999)
     8   one :: 'a                    ("\<unit>");
    26   one :: 'a                    ("\<unit>");
     9 
    27 
       
    28 text {*
       
    29  \noindent Next we define class $monoid$ of monoids with operations
       
    30  $\TIMES$ and $1$.  Note that multiple class axioms are allowed for
       
    31  user convenience --- they simply represent the conjunction of their
       
    32  respective universal closures.
       
    33 *};
    10 
    34 
    11 axclass
    35 axclass
    12   monoid < "term"
    36   monoid < "term"
    13   assoc:      "(x \<Otimes> y) \<Otimes> z = x \<Otimes> (y \<Otimes> z)"
    37   assoc:      "(x \<Otimes> y) \<Otimes> z = x \<Otimes> (y \<Otimes> z)"
    14   left_unit:  "\<unit> \<Otimes> x = x"
    38   left_unit:  "\<unit> \<Otimes> x = x"
    15   right_unit: "x \<Otimes> \<unit> = x";
    39   right_unit: "x \<Otimes> \<unit> = x";
    16 
    40 
       
    41 text {*
       
    42  \noindent So class $monoid$ contains exactly those types $\tau$ where
       
    43  $\TIMES :: \tau \To \tau \To \tau$ and $1 :: \tau$ are specified
       
    44  appropriately, such that $\TIMES$ is associative and $1$ is a left
       
    45  and right unit element for $\TIMES$.
       
    46 *};
       
    47 
       
    48 text {*
       
    49  \medskip Independently of $monoid$, we now define a linear hierarchy
       
    50  of semigroups, general groups and abelian groups.  Note that the
       
    51  names of class axioms are automatically qualified with the class
       
    52  name; thus we may re-use common names such as $assoc$.
       
    53 *};
    17 
    54 
    18 axclass
    55 axclass
    19   semigroup < "term"
    56   semigroup < "term"
    20   assoc: "(x \<Otimes> y) \<Otimes> z = x \<Otimes> (y \<Otimes> z)";
    57   assoc: "(x \<Otimes> y) \<Otimes> z = x \<Otimes> (y \<Otimes> z)";
    21 
    58 
    22 axclass
    59 axclass
    23   group < semigroup
    60   group < semigroup
    24   left_unit:    "\<unit> \<Otimes> x = x"
    61   left_unit:    "\<unit> \<Otimes> x = x"
    25   left_inverse: "inverse x \<Otimes> x = \<unit>";
    62   left_inverse: "inverse x \<Otimes> x = \<unit>";
    26 
    63 
    27 
    64 axclass
    28 text {*
    65   agroup < group
    29  The group axioms only state the properties of left unit and inverse,
    66   commute: "x \<Otimes> y = y \<Otimes> x";
    30  the right versions may be derived as follows.
    67 
    31 *};
    68 text {*
    32 
    69  \noindent Class $group$ inherits associativity of $\TIMES$ from
    33 theorem group_right_inverse: "x \<Otimes> x\<inv> = (\<unit>::'a::group)";
    70  $semigroup$ and adds the other two group axioms. Similarly, $agroup$
       
    71  is defined as the subset of $group$ such that for all of its elements
       
    72  $\tau$, the operation $\TIMES :: \tau \To \tau \To \tau$ is even
       
    73  commutative.
       
    74 *};
       
    75 
       
    76 
       
    77 subsection {* Abstract reasoning *};
       
    78 
       
    79 text {*
       
    80  In a sense, axiomatic type classes may be viewed as \emph{abstract
       
    81  theories}.  Above class definitions gives rise to abstract axioms
       
    82  $assoc$, $left_unit$, $left_inverse$, $commute$, where all of these
       
    83  contain type a variable $\alpha :: c$ that is restricted to types of
       
    84  the corresponding class $c$.  \emph{Sort constraints} like this
       
    85  express a logical precondition for the whole formula.  For example,
       
    86  $assoc$ states that for all $\tau$, provided that $\tau ::
       
    87  semigroup$, the operation $\TIMES :: \tau \To \tau \To \tau$ is
       
    88  associative.
       
    89 
       
    90  \medskip From a technical point of view, abstract axioms are just
       
    91  ordinary Isabelle theorems, which may be used in proofs without
       
    92  special treatment.  Such ``abstract proofs'' usually yield new
       
    93  ``abstract theorems''.  For example, we may now derive the following
       
    94  laws of general groups.
       
    95 *};
       
    96 
       
    97 theorem group_right_inverse: "x \<Otimes> x\<inv> = (\<unit>\<Colon>'a\<Colon>group)";
    34 proof -;
    98 proof -;
    35   have "x \<Otimes> x\<inv> = \<unit> \<Otimes> (x \<Otimes> x\<inv>)";
    99   have "x \<Otimes> x\<inv> = \<unit> \<Otimes> (x \<Otimes> x\<inv>)";
    36     by (simp only: group.left_unit);
   100     by (simp only: group.left_unit);
    37   also; have "... = (\<unit> \<Otimes> x) \<Otimes> x\<inv>";
   101   also; have "... = (\<unit> \<Otimes> x) \<Otimes> x\<inv>";
    38     by (simp only: semigroup.assoc);
   102     by (simp only: semigroup.assoc);
    50     by (simp only: group.left_inverse);
   114     by (simp only: group.left_inverse);
    51   finally; show ?thesis; .;
   115   finally; show ?thesis; .;
    52 qed;
   116 qed;
    53 
   117 
    54 text {*
   118 text {*
    55  With $group_right_inverse$ already available,
   119  \noindent With $group_right_inverse$ already available,
    56  $group_right_unit$\label{thm:group-right-unit} is now established
   120  $group_right_unit$\label{thm:group-right-unit} is now established
    57  much easier.
   121  much easier.
    58 *};
   122 *};
    59 
   123 
    60 theorem group_right_unit: "x \<Otimes> \<unit> = (x::'a::group)";
   124 theorem group_right_unit: "x \<Otimes> \<unit> = (x\<Colon>'a\<Colon>group)";
    61 proof -;
   125 proof -;
    62   have "x \<Otimes> \<unit> = x \<Otimes> (x\<inv> \<Otimes> x)";
   126   have "x \<Otimes> \<unit> = x \<Otimes> (x\<inv> \<Otimes> x)";
    63     by (simp only: group.left_inverse);
   127     by (simp only: group.left_inverse);
    64   also; have "... = x \<Otimes> x\<inv> \<Otimes> x";
   128   also; have "... = x \<Otimes> x\<inv> \<Otimes> x";
    65     by (simp only: semigroup.assoc);
   129     by (simp only: semigroup.assoc);
    68   also; have "... = x";
   132   also; have "... = x";
    69     by (simp only: group.left_unit);
   133     by (simp only: group.left_unit);
    70   finally; show ?thesis; .;
   134   finally; show ?thesis; .;
    71 qed;
   135 qed;
    72 
   136 
    73 
   137 text {*
    74 axclass
   138  \medskip Abstract theorems may be instantiated to only those types
    75   agroup < group
   139  $\tau$ where the appropriate class membership $\tau :: c$ is known at
    76   commute: "x \<Otimes> y = y \<Otimes> x";
   140  Isabelle's type signature level.  Since we have $agroup \subseteq
    77 
   141  group \subseteq semigroup$ by definition, all theorems of $semigroup$
    78 
   142  and $group$ are automatically inherited by $group$ and $agroup$.
       
   143 *};
       
   144 
       
   145 
       
   146 subsection {* Abstract instantiation *};
       
   147 
       
   148 text {*
       
   149  From the definition, the $monoid$ and $group$ classes have been
       
   150  independent.  Note that for monoids, $right_unit$ had to be included
       
   151  as an axiom, but for groups both $right_unit$ and $right_inverse$ are
       
   152  derivable from the other axioms.  With $group_right_unit$ derived as
       
   153  a theorem of group theory (see page~\pageref{thm:group-right-unit}),
       
   154  we may now instantiate $group \subseteq monoid$ properly as follows
       
   155  (cf.\ \figref{fig:monoid-group}).
       
   156 
       
   157  \begin{figure}[htbp]
       
   158    \begin{center}
       
   159      \small
       
   160      \unitlength 0.6mm
       
   161      \begin{picture}(65,90)(0,-10)
       
   162        \put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}}
       
   163        \put(15,50){\line(1,1){10}} \put(35,60){\line(1,-1){10}}
       
   164        \put(15,5){\makebox(0,0){$agroup$}}
       
   165        \put(15,25){\makebox(0,0){$group$}}
       
   166        \put(15,45){\makebox(0,0){$semigroup$}}
       
   167        \put(30,65){\makebox(0,0){$term$}} \put(50,45){\makebox(0,0){$monoid$}}
       
   168      \end{picture}
       
   169      \hspace{4em}
       
   170      \begin{picture}(30,90)(0,0)
       
   171        \put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}}
       
   172        \put(15,50){\line(0,1){10}} \put(15,70){\line(0,1){10}}
       
   173        \put(15,5){\makebox(0,0){$agroup$}}
       
   174        \put(15,25){\makebox(0,0){$group$}}
       
   175        \put(15,45){\makebox(0,0){$monoid$}}
       
   176        \put(15,65){\makebox(0,0){$semigroup$}}
       
   177        \put(15,85){\makebox(0,0){$term$}}
       
   178      \end{picture}
       
   179      \caption{Monoids and groups: according to definition, and by proof}
       
   180      \label{fig:monoid-group}
       
   181    \end{center}
       
   182  \end{figure}
       
   183 
       
   184  We know by definition that $\TIMES$ is associative for monoids, i.e.\
       
   185  $monoid \subseteq semigroup$. Furthermore we have $assoc$,
       
   186  $left_unit$ and $right_unit$ for groups (where $right_unit$ is
       
   187  derivable from the group axioms), that is $group \subseteq monoid$.
       
   188  Thus we may establish the following class instantiations.
       
   189 *};
    79 
   190 
    80 instance monoid < semigroup;
   191 instance monoid < semigroup;
    81 proof intro_classes;
   192 proof intro_classes;
    82   fix x y z :: "'a::monoid";
   193   fix x y z :: "'a\<Colon>monoid";
    83   show "x \<Otimes> y \<Otimes> z = x \<Otimes> (y \<Otimes> z)";
   194   show "x \<Otimes> y \<Otimes> z = x \<Otimes> (y \<Otimes> z)";
    84     by (rule monoid.assoc);
   195     by (rule monoid.assoc);
    85 qed;
   196 qed;
    86 
   197 
    87 
       
    88 instance group < monoid;
   198 instance group < monoid;
    89 proof intro_classes;
   199 proof intro_classes;
    90   fix x y z :: "'a::group";
   200   fix x y z :: "'a\<Colon>group";
    91   show "x \<Otimes> y \<Otimes> z = x \<Otimes> (y \<Otimes> z)";
   201   show "x \<Otimes> y \<Otimes> z = x \<Otimes> (y \<Otimes> z)";
    92     by (rule semigroup.assoc);
   202     by (rule semigroup.assoc);
    93   show "\<unit> \<Otimes> x = x";
   203   show "\<unit> \<Otimes> x = x";
    94     by (rule group.left_unit);
   204     by (rule group.left_unit);
    95   show "x \<Otimes> \<unit> = x";
   205   show "x \<Otimes> \<unit> = x";
    96     by (rule group_right_unit);
   206     by (rule group_right_unit);
    97 qed;
   207 qed;
    98 
   208 
    99 
   209 text {*
       
   210  \medskip The $\isakeyword{instance}$ command sets up an appropriate
       
   211  goal that represents the class inclusion (or type arity) to be proven
       
   212  (see also \cite{isabelle-isar-ref}).  The $intro_classes$ proof
       
   213  method does back-chaining of class membership statements wrt.\ the
       
   214  hierarchy of any classes defined in the current theory; the effect is
       
   215  to reduce to any logical class axioms as subgoals.
       
   216 *};
       
   217 
       
   218 
       
   219 subsection {* Concrete instantiation *};
       
   220 
       
   221 text {*
       
   222  So far we have covered the case of the form
       
   223  $\isakeyword{instance}~c@1 < c@2$, namely \emph{abstract
       
   224  instantiation} --- $c@1$ is more special than $c@2$ and thus an
       
   225  instance of $c@2$.  Even more interesting for practical applications
       
   226  are \emph{concrete instantiations} of axiomatic type classes.  That
       
   227  is, certain simple schemes $(\alpha@1, \ldots, \alpha@n)t :: c$ of
       
   228  class membership may be established at the logical level and then
       
   229  transferred to Isabelle's type signature level.
       
   230 
       
   231  \medskip
       
   232 
       
   233  As an typical example, we show that type $bool$ with exclusive-or as
       
   234  operation $\TIMES$, identity as $\isasyminv$, and $False$ as $1$
       
   235  forms an Abelian group.
       
   236 *};
   100 
   237 
   101 defs
   238 defs
   102   times_bool_def:   "x \<Otimes> y \\<equiv> x \\<noteq> (y\\<Colon>bool)"
   239   times_bool_def:   "x \<Otimes> y \\<equiv> x \\<noteq> (y\\<Colon>bool)"
   103   inverse_bool_def: "x\<inv> \\<equiv> x\\<Colon>bool"
   240   inverse_bool_def: "x\<inv> \\<equiv> x\\<Colon>bool"
   104   unit_bool_def:    "\<unit> \\<equiv> False";
   241   unit_bool_def:    "\<unit> \\<equiv> False";
       
   242 
       
   243 text {*
       
   244  \medskip It is important to note that above $\DEFS$ are just
       
   245  overloaded meta-level constant definitions.  In particular, type
       
   246  classes are not yet involved at all!  This form of constant
       
   247  definition with overloading (and optional recursion over the
       
   248  syntactic structure of simple types) are admissible of proper
       
   249  definitional extensions in any version of HOL
       
   250  \cite{Wenzel:1997:TPHOL}.  Nevertheless, overloaded definitions are
       
   251  best applied in the context of type classes.
       
   252 
       
   253  \medskip Since we have chosen above $\DEFS$ of the generic group
       
   254  operations on type $bool$ appropriately, the class membership $bool
       
   255  :: agroup$ may be now derived as follows.
       
   256 *};
   105 
   257 
   106 instance bool :: agroup;
   258 instance bool :: agroup;
   107 proof (intro_classes,
   259 proof (intro_classes,
   108     unfold times_bool_def inverse_bool_def unit_bool_def);
   260     unfold times_bool_def inverse_bool_def unit_bool_def);
   109   fix x y z :: bool;
   261   fix x y z :: bool;
   111   show "(False \\<noteq> x) = x"; by blast;
   263   show "(False \\<noteq> x) = x"; by blast;
   112   show "(x \\<noteq> x) = False"; by blast;
   264   show "(x \\<noteq> x) = False"; by blast;
   113   show "(x \\<noteq> y) = (y \\<noteq> x)"; by blast;
   265   show "(x \\<noteq> y) = (y \\<noteq> x)"; by blast;
   114 qed;
   266 qed;
   115 
   267 
       
   268 text {*
       
   269  The result of $\isakeyword{instance}$ is both expressed as a theorem
       
   270  of Isabelle's meta-logic, and a type arity statement of the type
       
   271  signature.  The latter enables the type-inference system to take care
       
   272  of this new instance automatically.
       
   273 
       
   274  \medskip In a similarly fashion, we could also instantiate our group
       
   275  theory classes to many other concrete types.  For example, $int ::
       
   276  agroup$ (e.g.\ by defining $\TIMES$ as addition, $\isasyminv$ as
       
   277  negation and $1$ as zero) or $list :: (term)semigroup$ (e.g.\ if
       
   278  $\TIMES$ is defined as list append).  Thus, the characteristic
       
   279  constants $\TIMES$, $\isasyminv$, $1$ really become overloaded, i.e.\
       
   280  have different meanings on different types.
       
   281 *};
       
   282 
       
   283 
       
   284 subsection {* Lifting and Functors *};
       
   285 
       
   286 text {*
       
   287  As already mentioned above, overloading in the simply-typed HOL
       
   288  systems may include recursion over the syntactic structure of types.
       
   289  That is, definitional equations $c^\tau \equiv t$ may also contain
       
   290  constants of name $c$ on the right-hand side --- if these have types
       
   291  that are structurally simpler than $\tau$.
       
   292 
       
   293  This feature enables us to \emph{lift operations}, say to Cartesian
       
   294  products, direct sums or function spaces.  Subsequently we lift
       
   295  $\TIMES$ component-wise to binary Cartesian products $\alpha \times
       
   296  \beta$.
       
   297 *};
   116 
   298 
   117 defs
   299 defs
   118   prod_prod_def: "p \<Otimes> q \\<equiv> (fst p \<Otimes> fst q, snd p \<Otimes> snd q)";
   300   prod_prod_def: "p \<Otimes> q \\<equiv> (fst p \<Otimes> fst q, snd p \<Otimes> snd q)";
   119 
   301 
       
   302 text {*
       
   303  It is very easy to see that associativity of $\TIMES^\alpha$,
       
   304  $\TIMES^\beta$ transfers to ${\TIMES}^{\alpha \times \beta}$.  Hence
       
   305  the binary type constructor $\times$ maps semigroups to semigroups.
       
   306  This may be established formally as follows.
       
   307 *};
       
   308 
   120 instance * :: (semigroup, semigroup) semigroup;
   309 instance * :: (semigroup, semigroup) semigroup;
   121 proof (intro_classes, unfold prod_prod_def);
   310 proof (intro_classes, unfold prod_prod_def);
   122   fix p q r :: "'a::semigroup * 'b::semigroup";
   311   fix p q r :: "'a\<Colon>semigroup * 'b\<Colon>semigroup";
   123   show
   312   show
   124     "(fst (fst p \<Otimes> fst q, snd p \<Otimes> snd q) \<Otimes> fst r,
   313     "(fst (fst p \<Otimes> fst q, snd p \<Otimes> snd q) \<Otimes> fst r,
   125       snd (fst p \<Otimes> fst q, snd p \<Otimes> snd q) \<Otimes> snd r) =
   314       snd (fst p \<Otimes> fst q, snd p \<Otimes> snd q) \<Otimes> snd r) =
   126        (fst p \<Otimes> fst (fst q \<Otimes> fst r, snd q \<Otimes> snd r),
   315        (fst p \<Otimes> fst (fst q \<Otimes> fst r, snd q \<Otimes> snd r),
   127         snd p \<Otimes> snd (fst q \<Otimes> fst r, snd q \<Otimes> snd r))";
   316         snd p \<Otimes> snd (fst q \<Otimes> fst r, snd q \<Otimes> snd r))";
   128     by (simp add: semigroup.assoc);
   317     by (simp add: semigroup.assoc);
   129 qed;
   318 qed;
   130 
   319 
       
   320 text {*
       
   321  Thus, if we view class instances as ``structures'', then overloaded
       
   322  constant definitions with primitive recursion over types indirectly
       
   323  provide some kind of ``functors'' --- i.e.\ mappings between abstract
       
   324  theories.
       
   325 *};
       
   326 
   131 end;
   327 end;