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