|
1 |
|
2 header {* Semigroups *}; |
|
3 |
1 theory Semigroups = Main:; |
4 theory Semigroups = Main:; |
2 |
5 |
3 constdefs |
6 text {* |
4 is_assoc :: "('a \\<Rightarrow> 'a \\<Rightarrow> 'a) \\<Rightarrow> bool" |
7 \medskip\noindent An axiomatic type class is simply a class of types |
5 "is_assoc f \\<equiv> \\<forall>x y z. f (f x y) z = f x (f y z)"; |
8 that all meet certain \emph{axioms}. Thus, type classes may be also |
|
9 understood as type predicates --- i.e.\ abstractions over a single |
|
10 type argument $\alpha$. Class axioms typically contain polymorphic |
|
11 constants that depend on this type $\alpha$. These |
|
12 \emph{characteristic constants} behave like operations associated |
|
13 with the ``carrier'' type $\alpha$. |
|
14 |
|
15 We illustrate these basic concepts by the following formulation of |
|
16 semigroups. |
|
17 *}; |
6 |
18 |
7 consts |
19 consts |
8 plus :: "'a \\<Rightarrow> 'a \\<Rightarrow> 'a" (infixl "\<Oplus>" 65); |
20 times :: "'a \\<Rightarrow> 'a \\<Rightarrow> 'a" (infixl "\<Otimes>" 70); |
|
21 axclass |
|
22 semigroup < "term" |
|
23 assoc: "(x \<Otimes> y) \<Otimes> z = x \<Otimes> (y \<Otimes> z)"; |
|
24 |
|
25 text {* |
|
26 \noindent Above we have first declared a polymorphic constant $\TIMES |
|
27 :: \alpha \To \alpha \To \alpha$ and then defined the class |
|
28 $semigroup$ of all types $\tau$ such that $\TIMES :: \tau \To \tau |
|
29 \To \tau$ is indeed an associative operator. The $assoc$ axiom |
|
30 contains exactly one type variable, which is invisible in the above |
|
31 presentation, though. Also note that free term variables (like $x$, |
|
32 $y$, $z$) are allowed for user convenience --- conceptually all of |
|
33 these are bound by outermost universal quantifiers. |
|
34 |
|
35 \medskip In general, type classes may be used to describe |
|
36 \emph{structures} with exactly one carrier $\alpha$ and a fixed |
|
37 \emph{signature}. Different signatures require different classes. |
|
38 Subsequently, class $plus_semigroup$ represents semigroups of the |
|
39 form $(\tau, \PLUS^\tau)$, while $semigroup$ above would correspond |
|
40 to semigroups $(\tau, \TIMES^\tau)$. |
|
41 *}; |
|
42 |
|
43 consts |
|
44 plus :: "'a \\<Rightarrow> 'a \\<Rightarrow> 'a" (infixl "\<Oplus>" 70); |
9 axclass |
45 axclass |
10 plus_semigroup < "term" |
46 plus_semigroup < "term" |
11 assoc: "is_assoc (op \<Oplus>)"; |
47 assoc: "(x \<Oplus> y) \<Oplus> z = x \<Oplus> (y \<Oplus> z)"; |
12 |
48 |
13 consts |
49 text {* |
14 times :: "'a \\<Rightarrow> 'a \\<Rightarrow> 'a" (infixl "\<Otimes>" 65); |
50 \noindent Even if classes $plus_semigroup$ and $times_semigroup$ both |
15 axclass |
51 represent semigroups in a sense, they are certainly not the same! |
16 times_semigroup < "term" |
52 *}; |
17 assoc: "is_assoc (op \<Otimes>)"; |
|
18 |
53 |
19 end; |
54 end; |