diff -r b094999e1d33 -r 37f47ea6fed1 doc-src/AxClass/Group/Group.thy --- a/doc-src/AxClass/Group/Group.thy Thu Feb 26 06:39:06 2009 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,322 +0,0 @@ - -header {* Basic group theory *} - -theory Group imports Main begin - -text {* - \medskip\noindent The meta-level type system of Isabelle supports - \emph{intersections} and \emph{inclusions} of type classes. These - directly correspond to intersections and inclusions of type - predicates in a purely set theoretic sense. This is sufficient as a - means to describe simple hierarchies of structures. As an - illustration, we use the well-known example of semigroups, monoids, - general groups and Abelian groups. -*} - -subsection {* Monoids and Groups *} - -text {* - First we declare some polymorphic constants required later for the - signature parts of our structures. -*} - -consts - times :: "'a \ 'a \ 'a" (infixl "\" 70) - invers :: "'a \ 'a" ("(_\)" [1000] 999) - one :: 'a ("\") - -text {* - \noindent Next we define class @{text monoid} of monoids with - operations @{text \} and @{text \}. Note that multiple class - axioms are allowed for user convenience --- they simply represent - the conjunction of their respective universal closures. -*} - -axclass monoid \ type - assoc: "(x \ y) \ z = x \ (y \ z)" - left_unit: "\ \ x = x" - right_unit: "x \ \ = x" - -text {* - \noindent So class @{text monoid} contains exactly those types - @{text \} where @{text "\ \ \ \ \ \ \"} and @{text "\ \ \"} - are specified appropriately, such that @{text \} is associative and - @{text \} is a left and right unit element for the @{text \} - operation. -*} - -text {* - \medskip Independently of @{text monoid}, we now define a linear - hierarchy of semigroups, general groups and Abelian groups. Note - that the names of class axioms are automatically qualified with each - class name, so we may re-use common names such as @{text assoc}. -*} - -axclass semigroup \ type - assoc: "(x \ y) \ z = x \ (y \ z)" - -axclass group \ semigroup - left_unit: "\ \ x = x" - left_inverse: "x\ \ x = \" - -axclass agroup \ group - commute: "x \ y = y \ x" - -text {* - \noindent Class @{text group} inherits associativity of @{text \} - from @{text semigroup} and adds two further group axioms. Similarly, - @{text agroup} is defined as the subset of @{text group} such that - for all of its elements @{text \}, the operation @{text "\ \ \ \ \ \ - \"} is even commutative. -*} - - -subsection {* Abstract reasoning *} - -text {* - In a sense, axiomatic type classes may be viewed as \emph{abstract - theories}. Above class definitions gives rise to abstract axioms - @{text assoc}, @{text left_unit}, @{text left_inverse}, @{text - commute}, where any of these contain a type variable @{text "'a \ - c"} that is restricted to types of the corresponding class @{text - c}. \emph{Sort constraints} like this express a logical - precondition for the whole formula. For example, @{text assoc} - states that for all @{text \}, provided that @{text "\ \ - semigroup"}, the operation @{text "\ \ \ \ \ \ \"} is associative. - - \medskip From a technical point of view, abstract axioms are just - ordinary Isabelle theorems, which may be used in proofs without - special treatment. Such ``abstract proofs'' usually yield new - ``abstract theorems''. For example, we may now derive the following - well-known laws of general groups. -*} - -theorem group_right_inverse: "x \ x\ = (\\'a\group)" -proof - - have "x \ x\ = \ \ (x \ x\)" - by (simp only: group_class.left_unit) - also have "... = \ \ x \ x\" - by (simp only: semigroup_class.assoc) - also have "... = (x\)\ \ x\ \ x \ x\" - by (simp only: group_class.left_inverse) - also have "... = (x\)\ \ (x\ \ x) \ x\" - by (simp only: semigroup_class.assoc) - also have "... = (x\)\ \ \ \ x\" - by (simp only: group_class.left_inverse) - also have "... = (x\)\ \ (\ \ x\)" - by (simp only: semigroup_class.assoc) - also have "... = (x\)\ \ x\" - by (simp only: group_class.left_unit) - also have "... = \" - by (simp only: group_class.left_inverse) - finally show ?thesis . -qed - -text {* - \noindent With @{text group_right_inverse} already available, @{text - group_right_unit}\label{thm:group-right-unit} is now established - much easier. -*} - -theorem group_right_unit: "x \ \ = (x\'a\group)" -proof - - have "x \ \ = x \ (x\ \ x)" - by (simp only: group_class.left_inverse) - also have "... = x \ x\ \ x" - by (simp only: semigroup_class.assoc) - also have "... = \ \ x" - by (simp only: group_right_inverse) - also have "... = x" - by (simp only: group_class.left_unit) - finally show ?thesis . -qed - -text {* - \medskip Abstract theorems may be instantiated to only those types - @{text \} where the appropriate class membership @{text "\ \ c"} is - known at Isabelle's type signature level. Since we have @{text - "agroup \ group \ semigroup"} by definition, all theorems of @{text - semigroup} and @{text group} are automatically inherited by @{text - group} and @{text agroup}. -*} - - -subsection {* Abstract instantiation *} - -text {* - From the definition, the @{text monoid} and @{text group} classes - have been independent. Note that for monoids, @{text right_unit} - had to be included as an axiom, but for groups both @{text - right_unit} and @{text right_inverse} are derivable from the other - axioms. With @{text group_right_unit} derived as a theorem of group - theory (see page~\pageref{thm:group-right-unit}), we may now - instantiate @{text "monoid \ semigroup"} and @{text "group \ - monoid"} properly as follows (cf.\ \figref{fig:monoid-group}). - - \begin{figure}[htbp] - \begin{center} - \small - \unitlength 0.6mm - \begin{picture}(65,90)(0,-10) - \put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}} - \put(15,50){\line(1,1){10}} \put(35,60){\line(1,-1){10}} - \put(15,5){\makebox(0,0){@{text agroup}}} - \put(15,25){\makebox(0,0){@{text group}}} - \put(15,45){\makebox(0,0){@{text semigroup}}} - \put(30,65){\makebox(0,0){@{text type}}} \put(50,45){\makebox(0,0){@{text monoid}}} - \end{picture} - \hspace{4em} - \begin{picture}(30,90)(0,0) - \put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}} - \put(15,50){\line(0,1){10}} \put(15,70){\line(0,1){10}} - \put(15,5){\makebox(0,0){@{text agroup}}} - \put(15,25){\makebox(0,0){@{text group}}} - \put(15,45){\makebox(0,0){@{text monoid}}} - \put(15,65){\makebox(0,0){@{text semigroup}}} - \put(15,85){\makebox(0,0){@{text type}}} - \end{picture} - \caption{Monoids and groups: according to definition, and by proof} - \label{fig:monoid-group} - \end{center} - \end{figure} -*} - -instance monoid \ semigroup -proof - fix x y z :: "'a\monoid" - show "x \ y \ z = x \ (y \ z)" - by (rule monoid_class.assoc) -qed - -instance group \ monoid -proof - fix x y z :: "'a\group" - show "x \ y \ z = x \ (y \ z)" - by (rule semigroup_class.assoc) - show "\ \ x = x" - by (rule group_class.left_unit) - show "x \ \ = x" - by (rule group_right_unit) -qed - -text {* - \medskip The \isakeyword{instance} command sets up an appropriate - goal that represents the class inclusion (or type arity, see - \secref{sec:inst-arity}) to be proven (see also - \cite{isabelle-isar-ref}). The initial proof step causes - back-chaining of class membership statements wrt.\ the hierarchy of - any classes defined in the current theory; the effect is to reduce - to the initial statement to a number of goals that directly - correspond to any class axioms encountered on the path upwards - through the class hierarchy. -*} - - -subsection {* Concrete instantiation \label{sec:inst-arity} *} - -text {* - So far we have covered the case of the form - \isakeyword{instance}~@{text "c\<^sub>1 \ c\<^sub>2"}, namely - \emph{abstract instantiation} --- $c@1$ is more special than @{text - "c\<^sub>1"} and thus an instance of @{text "c\<^sub>2"}. Even more - interesting for practical applications are \emph{concrete - instantiations} of axiomatic type classes. That is, certain simple - schemes @{text "(\\<^sub>1, \, \\<^sub>n) t \ c"} of class - membership may be established at the logical level and then - transferred to Isabelle's type signature level. - - \medskip As a typical example, we show that type @{typ bool} with - exclusive-or as @{text \} operation, identity as @{text \}, and - @{term False} as @{text \} forms an Abelian group. -*} - -defs (overloaded) - times_bool_def: "x \ y \ x \ (y\bool)" - inverse_bool_def: "x\ \ x\bool" - unit_bool_def: "\ \ False" - -text {* - \medskip It is important to note that above \isakeyword{defs} are - just overloaded meta-level constant definitions, where type classes - are not yet involved at all. This form of constant definition with - overloading (and optional recursion over the syntactic structure of - simple types) are admissible as definitional extensions of plain HOL - \cite{Wenzel:1997:TPHOL}. The Haskell-style type system is not - required for overloading. Nevertheless, overloaded definitions are - best applied in the context of type classes. - - \medskip Since we have chosen above \isakeyword{defs} of the generic - group operations on type @{typ bool} appropriately, the class - membership @{text "bool \ agroup"} may be now derived as follows. -*} - -instance bool :: agroup -proof (intro_classes, - unfold times_bool_def inverse_bool_def unit_bool_def) - fix x y z - show "((x \ y) \ z) = (x \ (y \ z))" by blast - show "(False \ x) = x" by blast - show "(x \ x) = False" by blast - show "(x \ y) = (y \ x)" by blast -qed - -text {* - The result of an \isakeyword{instance} statement is both expressed - as a theorem of Isabelle's meta-logic, and as a type arity of the - type signature. The latter enables type-inference system to take - care of this new instance automatically. - - \medskip We could now also instantiate our group theory classes to - many other concrete types. For example, @{text "int \ agroup"} - (e.g.\ by defining @{text \} as addition, @{text \} as negation - and @{text \} as zero) or @{text "list \ (type) semigroup"} - (e.g.\ if @{text \} is defined as list append). Thus, the - characteristic constants @{text \}, @{text \}, @{text \} - really become overloaded, i.e.\ have different meanings on different - types. -*} - - -subsection {* Lifting and Functors *} - -text {* - As already mentioned above, overloading in the simply-typed HOL - systems may include recursion over the syntactic structure of types. - That is, definitional equations @{text "c\<^sup>\ \ t"} may also - contain constants of name @{text c} on the right-hand side --- if - these have types that are structurally simpler than @{text \}. - - This feature enables us to \emph{lift operations}, say to Cartesian - products, direct sums or function spaces. Subsequently we lift - @{text \} component-wise to binary products @{typ "'a \ 'b"}. -*} - -defs (overloaded) - times_prod_def: "p \ q \ (fst p \ fst q, snd p \ snd q)" - -text {* - It is very easy to see that associativity of @{text \} on @{typ 'a} - and @{text \} on @{typ 'b} transfers to @{text \} on @{typ "'a \ - 'b"}. Hence the binary type constructor @{text \} maps semigroups - to semigroups. This may be established formally as follows. -*} - -instance * :: (semigroup, semigroup) semigroup -proof (intro_classes, unfold times_prod_def) - fix p q r :: "'a\semigroup \ 'b\semigroup" - show - "(fst (fst p \ fst q, snd p \ snd q) \ fst r, - snd (fst p \ fst q, snd p \ snd q) \ snd r) = - (fst p \ fst (fst q \ fst r, snd q \ snd r), - snd p \ snd (fst q \ fst r, snd q \ snd r))" - by (simp add: semigroup_class.assoc) -qed - -text {* - Thus, if we view class instances as ``structures'', then overloaded - constant definitions with recursion over types indirectly provide - some kind of ``functors'' --- i.e.\ mappings between abstract - theories. -*} - -end