8903
|
1 |
|
9146
|
2 |
header {* Basic group theory *}
|
8890
|
3 |
|
9146
|
4 |
theory Group = Main:
|
8890
|
5 |
|
8903
|
6 |
text {*
|
8907
|
7 |
\medskip\noindent The meta-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
|
|
24 |
times :: "'a => 'a => 'a" (infixl "\<Otimes>" 70)
|
|
25 |
inverse :: "'a => 'a" ("(_\<inv>)" [1000] 999)
|
9146
|
26 |
one :: 'a ("\<unit>")
|
8890
|
27 |
|
8903
|
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.
|
9146
|
33 |
*}
|
8890
|
34 |
|
|
35 |
axclass
|
|
36 |
monoid < "term"
|
|
37 |
assoc: "(x \<Otimes> y) \<Otimes> z = x \<Otimes> (y \<Otimes> z)"
|
|
38 |
left_unit: "\<unit> \<Otimes> x = x"
|
9146
|
39 |
right_unit: "x \<Otimes> \<unit> = x"
|
8890
|
40 |
|
8903
|
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$.
|
9146
|
46 |
*}
|
8903
|
47 |
|
|
48 |
text {*
|
|
49 |
\medskip Independently of $monoid$, we now define a linear hierarchy
|
8907
|
50 |
of semigroups, general groups and Abelian groups. Note that the
|
|
51 |
names of class axioms are automatically qualified with each class
|
|
52 |
name, so we may re-use common names such as $assoc$.
|
9146
|
53 |
*}
|
8890
|
54 |
|
|
55 |
axclass
|
|
56 |
semigroup < "term"
|
9146
|
57 |
assoc: "(x \<Otimes> y) \<Otimes> z = x \<Otimes> (y \<Otimes> z)"
|
8890
|
58 |
|
|
59 |
axclass
|
|
60 |
group < semigroup
|
|
61 |
left_unit: "\<unit> \<Otimes> x = x"
|
9146
|
62 |
left_inverse: "x\<inv> \<Otimes> x = \<unit>"
|
8890
|
63 |
|
8903
|
64 |
axclass
|
|
65 |
agroup < group
|
9146
|
66 |
commute: "x \<Otimes> y = y \<Otimes> x"
|
8903
|
67 |
|
|
68 |
text {*
|
|
69 |
\noindent Class $group$ inherits associativity of $\TIMES$ from
|
8907
|
70 |
$semigroup$ and adds two further group axioms. Similarly, $agroup$
|
8903
|
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.
|
9146
|
74 |
*}
|
8903
|
75 |
|
|
76 |
|
9146
|
77 |
subsection {* Abstract reasoning *}
|
8890
|
78 |
|
|
79 |
text {*
|
8903
|
80 |
In a sense, axiomatic type classes may be viewed as \emph{abstract
|
|
81 |
theories}. Above class definitions gives rise to abstract axioms
|
8907
|
82 |
$assoc$, $left_unit$, $left_inverse$, $commute$, where any of these
|
|
83 |
contain a type variable $\alpha :: c$ that is restricted to types of
|
8903
|
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
|
8907
|
94 |
well-known laws of general groups.
|
9146
|
95 |
*}
|
8890
|
96 |
|
9146
|
97 |
theorem group_right_inverse: "x \<Otimes> x\<inv> = (\<unit>\\<Colon>'a\\<Colon>group)"
|
|
98 |
proof -
|
|
99 |
have "x \<Otimes> x\<inv> = \<unit> \<Otimes> (x \<Otimes> x\<inv>)"
|
|
100 |
by (simp only: group.left_unit)
|
|
101 |
also have "... = \<unit> \<Otimes> x \<Otimes> x\<inv>"
|
|
102 |
by (simp only: semigroup.assoc)
|
|
103 |
also have "... = (x\<inv>)\<inv> \<Otimes> x\<inv> \<Otimes> x \<Otimes> x\<inv>"
|
|
104 |
by (simp only: group.left_inverse)
|
|
105 |
also have "... = (x\<inv>)\<inv> \<Otimes> (x\<inv> \<Otimes> x) \<Otimes> x\<inv>"
|
|
106 |
by (simp only: semigroup.assoc)
|
|
107 |
also have "... = (x\<inv>)\<inv> \<Otimes> \<unit> \<Otimes> x\<inv>"
|
|
108 |
by (simp only: group.left_inverse)
|
|
109 |
also have "... = (x\<inv>)\<inv> \<Otimes> (\<unit> \<Otimes> x\<inv>)"
|
|
110 |
by (simp only: semigroup.assoc)
|
|
111 |
also have "... = (x\<inv>)\<inv> \<Otimes> x\<inv>"
|
|
112 |
by (simp only: group.left_unit)
|
|
113 |
also have "... = \<unit>"
|
|
114 |
by (simp only: group.left_inverse)
|
|
115 |
finally show ?thesis .
|
|
116 |
qed
|
8890
|
117 |
|
|
118 |
text {*
|
8903
|
119 |
\noindent With $group_right_inverse$ already available,
|
8890
|
120 |
$group_right_unit$\label{thm:group-right-unit} is now established
|
|
121 |
much easier.
|
9146
|
122 |
*}
|
8890
|
123 |
|
9146
|
124 |
theorem group_right_unit: "x \<Otimes> \<unit> = (x\\<Colon>'a\\<Colon>group)"
|
|
125 |
proof -
|
|
126 |
have "x \<Otimes> \<unit> = x \<Otimes> (x\<inv> \<Otimes> x)"
|
|
127 |
by (simp only: group.left_inverse)
|
|
128 |
also have "... = x \<Otimes> x\<inv> \<Otimes> x"
|
|
129 |
by (simp only: semigroup.assoc)
|
|
130 |
also have "... = \<unit> \<Otimes> x"
|
|
131 |
by (simp only: group_right_inverse)
|
|
132 |
also have "... = x"
|
|
133 |
by (simp only: group.left_unit)
|
|
134 |
finally show ?thesis .
|
|
135 |
qed
|
8890
|
136 |
|
8903
|
137 |
text {*
|
|
138 |
\medskip Abstract theorems may be instantiated to only those types
|
|
139 |
$\tau$ where the appropriate class membership $\tau :: c$ is known at
|
|
140 |
Isabelle's type signature level. Since we have $agroup \subseteq
|
|
141 |
group \subseteq semigroup$ by definition, all theorems of $semigroup$
|
|
142 |
and $group$ are automatically inherited by $group$ and $agroup$.
|
9146
|
143 |
*}
|
8890
|
144 |
|
|
145 |
|
9146
|
146 |
subsection {* Abstract instantiation *}
|
8903
|
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}),
|
8907
|
154 |
we may now instantiate $monoid \subseteq semigroup$ and $group
|
|
155 |
\subseteq monoid$ properly as follows
|
8903
|
156 |
(cf.\ \figref{fig:monoid-group}).
|
|
157 |
|
|
158 |
\begin{figure}[htbp]
|
|
159 |
\begin{center}
|
|
160 |
\small
|
|
161 |
\unitlength 0.6mm
|
|
162 |
\begin{picture}(65,90)(0,-10)
|
|
163 |
\put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}}
|
|
164 |
\put(15,50){\line(1,1){10}} \put(35,60){\line(1,-1){10}}
|
|
165 |
\put(15,5){\makebox(0,0){$agroup$}}
|
|
166 |
\put(15,25){\makebox(0,0){$group$}}
|
|
167 |
\put(15,45){\makebox(0,0){$semigroup$}}
|
|
168 |
\put(30,65){\makebox(0,0){$term$}} \put(50,45){\makebox(0,0){$monoid$}}
|
|
169 |
\end{picture}
|
|
170 |
\hspace{4em}
|
|
171 |
\begin{picture}(30,90)(0,0)
|
|
172 |
\put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}}
|
|
173 |
\put(15,50){\line(0,1){10}} \put(15,70){\line(0,1){10}}
|
|
174 |
\put(15,5){\makebox(0,0){$agroup$}}
|
|
175 |
\put(15,25){\makebox(0,0){$group$}}
|
|
176 |
\put(15,45){\makebox(0,0){$monoid$}}
|
|
177 |
\put(15,65){\makebox(0,0){$semigroup$}}
|
|
178 |
\put(15,85){\makebox(0,0){$term$}}
|
|
179 |
\end{picture}
|
|
180 |
\caption{Monoids and groups: according to definition, and by proof}
|
|
181 |
\label{fig:monoid-group}
|
|
182 |
\end{center}
|
|
183 |
\end{figure}
|
9146
|
184 |
*}
|
8890
|
185 |
|
9146
|
186 |
instance monoid < semigroup
|
|
187 |
proof intro_classes
|
|
188 |
fix x y z :: "'a\\<Colon>monoid"
|
|
189 |
show "x \<Otimes> y \<Otimes> z = x \<Otimes> (y \<Otimes> z)"
|
|
190 |
by (rule monoid.assoc)
|
|
191 |
qed
|
8890
|
192 |
|
9146
|
193 |
instance group < monoid
|
|
194 |
proof intro_classes
|
|
195 |
fix x y z :: "'a\\<Colon>group"
|
|
196 |
show "x \<Otimes> y \<Otimes> z = x \<Otimes> (y \<Otimes> z)"
|
|
197 |
by (rule semigroup.assoc)
|
|
198 |
show "\<unit> \<Otimes> x = x"
|
|
199 |
by (rule group.left_unit)
|
|
200 |
show "x \<Otimes> \<unit> = x"
|
|
201 |
by (rule group_right_unit)
|
|
202 |
qed
|
8890
|
203 |
|
8903
|
204 |
text {*
|
|
205 |
\medskip The $\isakeyword{instance}$ command sets up an appropriate
|
8907
|
206 |
goal that represents the class inclusion (or type arity, see
|
|
207 |
\secref{sec:inst-arity}) to be proven
|
8903
|
208 |
(see also \cite{isabelle-isar-ref}). The $intro_classes$ proof
|
|
209 |
method does back-chaining of class membership statements wrt.\ the
|
|
210 |
hierarchy of any classes defined in the current theory; the effect is
|
8907
|
211 |
to reduce to the initial statement to a number of goals that directly
|
|
212 |
correspond to any class axioms encountered on the path upwards
|
|
213 |
through the class hierarchy.
|
9146
|
214 |
*}
|
8890
|
215 |
|
|
216 |
|
9146
|
217 |
subsection {* Concrete instantiation \label{sec:inst-arity} *}
|
8903
|
218 |
|
|
219 |
text {*
|
|
220 |
So far we have covered the case of the form
|
|
221 |
$\isakeyword{instance}~c@1 < c@2$, namely \emph{abstract
|
|
222 |
instantiation} --- $c@1$ is more special than $c@2$ and thus an
|
|
223 |
instance of $c@2$. Even more interesting for practical applications
|
|
224 |
are \emph{concrete instantiations} of axiomatic type classes. That
|
|
225 |
is, certain simple schemes $(\alpha@1, \ldots, \alpha@n)t :: c$ of
|
|
226 |
class membership may be established at the logical level and then
|
|
227 |
transferred to Isabelle's type signature level.
|
|
228 |
|
8907
|
229 |
\medskip As a typical example, we show that type $bool$ with
|
|
230 |
exclusive-or as operation $\TIMES$, identity as $\isasyminv$, and
|
|
231 |
$False$ as $1$ forms an Abelian group.
|
9146
|
232 |
*}
|
8903
|
233 |
|
8890
|
234 |
defs
|
|
235 |
times_bool_def: "x \<Otimes> y \\<equiv> x \\<noteq> (y\\<Colon>bool)"
|
|
236 |
inverse_bool_def: "x\<inv> \\<equiv> x\\<Colon>bool"
|
9146
|
237 |
unit_bool_def: "\<unit> \\<equiv> False"
|
8890
|
238 |
|
8903
|
239 |
text {*
|
|
240 |
\medskip It is important to note that above $\DEFS$ are just
|
8907
|
241 |
overloaded meta-level constant definitions, where type classes are
|
|
242 |
not yet involved at all. This form of constant definition with
|
|
243 |
overloading (and optional recursion over the syntactic structure of
|
|
244 |
simple types) are admissible as definitional extensions of plain HOL
|
|
245 |
\cite{Wenzel:1997:TPHOL}. The Haskell-style type system is not
|
|
246 |
required for overloading. Nevertheless, overloaded definitions are
|
8903
|
247 |
best applied in the context of type classes.
|
|
248 |
|
|
249 |
\medskip Since we have chosen above $\DEFS$ of the generic group
|
|
250 |
operations on type $bool$ appropriately, the class membership $bool
|
|
251 |
:: agroup$ may be now derived as follows.
|
9146
|
252 |
*}
|
8903
|
253 |
|
9146
|
254 |
instance bool :: agroup
|
8890
|
255 |
proof (intro_classes,
|
9146
|
256 |
unfold times_bool_def inverse_bool_def unit_bool_def)
|
|
257 |
fix x y z
|
|
258 |
show "((x \\<noteq> y) \\<noteq> z) = (x \\<noteq> (y \\<noteq> z))" by blast
|
|
259 |
show "(False \\<noteq> x) = x" by blast
|
|
260 |
show "(x \\<noteq> x) = False" by blast
|
|
261 |
show "(x \\<noteq> y) = (y \\<noteq> x)" by blast
|
|
262 |
qed
|
8890
|
263 |
|
8903
|
264 |
text {*
|
8907
|
265 |
The result of an $\isakeyword{instance}$ statement is both expressed
|
|
266 |
as a theorem of Isabelle's meta-logic, and as a type arity of the
|
|
267 |
type signature. The latter enables type-inference system to take
|
|
268 |
care of this new instance automatically.
|
8903
|
269 |
|
8907
|
270 |
\medskip We could now also instantiate our group theory classes to
|
|
271 |
many other concrete types. For example, $int :: agroup$ (e.g.\ by
|
|
272 |
defining $\TIMES$ as addition, $\isasyminv$ as negation and $1$ as
|
|
273 |
zero) or $list :: (term)semigroup$ (e.g.\ if $\TIMES$ is defined as
|
|
274 |
list append). Thus, the characteristic constants $\TIMES$,
|
|
275 |
$\isasyminv$, $1$ really become overloaded, i.e.\ have different
|
|
276 |
meanings on different 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.
|
|
285 |
That is, definitional equations $c^\tau \equiv t$ may also contain
|
|
286 |
constants of name $c$ on the right-hand side --- if these have types
|
|
287 |
that are structurally simpler than $\tau$.
|
|
288 |
|
|
289 |
This feature enables us to \emph{lift operations}, say to Cartesian
|
|
290 |
products, direct sums or function spaces. Subsequently we lift
|
8907
|
291 |
$\TIMES$ component-wise to binary products $\alpha \times \beta$.
|
9146
|
292 |
*}
|
8890
|
293 |
|
|
294 |
defs
|
9146
|
295 |
times_prod_def: "p \<Otimes> q \\<equiv> (fst p \<Otimes> fst q, snd p \<Otimes> snd q)"
|
8890
|
296 |
|
8903
|
297 |
text {*
|
8907
|
298 |
It is very easy to see that associativity of $\TIMES^\alpha$ and
|
8903
|
299 |
$\TIMES^\beta$ transfers to ${\TIMES}^{\alpha \times \beta}$. Hence
|
|
300 |
the binary type constructor $\times$ maps semigroups to semigroups.
|
|
301 |
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)
|
|
306 |
fix p q r :: "'a\\<Colon>semigroup \\<times> 'b\\<Colon>semigroup"
|
8890
|
307 |
show
|
|
308 |
"(fst (fst p \<Otimes> fst q, snd p \<Otimes> snd q) \<Otimes> fst r,
|
|
309 |
snd (fst p \<Otimes> fst q, snd p \<Otimes> snd q) \<Otimes> snd r) =
|
|
310 |
(fst p \<Otimes> fst (fst q \<Otimes> fst r, snd q \<Otimes> snd r),
|
9146
|
311 |
snd p \<Otimes> snd (fst q \<Otimes> fst r, snd q \<Otimes> snd r))"
|
|
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 |