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