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