| 20967 |      1 | %
 | 
|  |      2 | \begin{isabellebody}%
 | 
|  |      3 | \def\isabellecontext{Classes}%
 | 
|  |      4 | %
 | 
|  |      5 | \isadelimtheory
 | 
|  |      6 | \isanewline
 | 
|  |      7 | %
 | 
|  |      8 | \endisadelimtheory
 | 
|  |      9 | %
 | 
|  |     10 | \isatagtheory
 | 
| 22479 |     11 | %
 | 
| 20967 |     12 | \endisatagtheory
 | 
|  |     13 | {\isafoldtheory}%
 | 
|  |     14 | %
 | 
|  |     15 | \isadelimtheory
 | 
|  |     16 | %
 | 
|  |     17 | \endisadelimtheory
 | 
|  |     18 | %
 | 
|  |     19 | \isadelimML
 | 
| 22317 |     20 | %
 | 
|  |     21 | \endisadelimML
 | 
|  |     22 | %
 | 
|  |     23 | \isatagML
 | 
|  |     24 | %
 | 
|  |     25 | \endisatagML
 | 
|  |     26 | {\isafoldML}%
 | 
|  |     27 | %
 | 
|  |     28 | \isadelimML
 | 
|  |     29 | %
 | 
|  |     30 | \endisadelimML
 | 
|  |     31 | %
 | 
|  |     32 | \isadelimML
 | 
| 20967 |     33 | %
 | 
|  |     34 | \endisadelimML
 | 
|  |     35 | %
 | 
|  |     36 | \isatagML
 | 
|  |     37 | %
 | 
|  |     38 | \endisatagML
 | 
|  |     39 | {\isafoldML}%
 | 
|  |     40 | %
 | 
|  |     41 | \isadelimML
 | 
|  |     42 | %
 | 
|  |     43 | \endisadelimML
 | 
|  |     44 | %
 | 
|  |     45 | \isamarkupchapter{Haskell-style classes with Isabelle/Isar%
 | 
|  |     46 | }
 | 
|  |     47 | \isamarkuptrue%
 | 
|  |     48 | %
 | 
|  |     49 | \isamarkupsection{Introduction%
 | 
|  |     50 | }
 | 
|  |     51 | \isamarkuptrue%
 | 
|  |     52 | %
 | 
|  |     53 | \begin{isamarkuptext}%
 | 
| 22317 |     54 | Type classes were introduces by Wadler and Blott \cite{wadler89how}
 | 
|  |     55 |   into the Haskell language, to allow for a reasonable implementation
 | 
|  |     56 |   of overloading\footnote{throughout this tutorial, we are referring
 | 
|  |     57 |   to classical Haskell 1.0 type classes, not considering
 | 
|  |     58 |   later additions in expressiveness}.
 | 
|  |     59 |   As a canonical example, a polymorphic equality function
 | 
|  |     60 |   \isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} which is overloaded on different
 | 
| 22550 |     61 |   types for \isa{{\isasymalpha}}, which is achieved by splitting introduction
 | 
| 22317 |     62 |   of the \isa{eq} function from its overloaded definitions by means
 | 
|  |     63 |   of \isa{class} and \isa{instance} declarations:
 | 
|  |     64 | 
 | 
|  |     65 |   \medskip\noindent\hspace*{2ex}\isa{class\ eq\ where}\footnote{syntax here is a kind of isabellized Haskell} \\
 | 
|  |     66 |   \hspace*{4ex}\isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool}
 | 
|  |     67 | 
 | 
|  |     68 |   \medskip\noindent\hspace*{2ex}\isa{instance\ nat\ {\isasymColon}\ eq\ where} \\
 | 
|  |     69 |   \hspace*{4ex}\isa{eq\ {\isadigit{0}}\ {\isadigit{0}}\ {\isacharequal}\ True} \\
 | 
|  |     70 |   \hspace*{4ex}\isa{eq\ {\isadigit{0}}\ {\isacharunderscore}\ {\isacharequal}\ False} \\
 | 
|  |     71 |   \hspace*{4ex}\isa{eq\ {\isacharunderscore}\ {\isadigit{0}}\ {\isacharequal}\ False} \\
 | 
|  |     72 |   \hspace*{4ex}\isa{eq\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharparenleft}Suc\ m{\isacharparenright}\ {\isacharequal}\ eq\ n\ m}
 | 
|  |     73 | 
 | 
|  |     74 |   \medskip\noindent\hspace*{2ex}\isa{instance\ {\isacharparenleft}{\isasymalpha}{\isasymColon}eq{\isacharcomma}\ {\isasymbeta}{\isasymColon}eq{\isacharparenright}\ pair\ {\isasymColon}\ eq\ where} \\
 | 
| 22550 |     75 |   \hspace*{4ex}\isa{eq\ {\isacharparenleft}x{\isadigit{1}}{\isacharcomma}\ y{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ eq\ x{\isadigit{1}}\ x{\isadigit{2}}\ {\isasymand}\ eq\ y{\isadigit{1}}\ y{\isadigit{2}}}
 | 
| 22317 |     76 | 
 | 
|  |     77 |   \medskip\noindent\hspace*{2ex}\isa{class\ ord\ extends\ eq\ where} \\
 | 
|  |     78 |   \hspace*{4ex}\isa{less{\isacharunderscore}eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} \\
 | 
|  |     79 |   \hspace*{4ex}\isa{less\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool}
 | 
|  |     80 | 
 | 
| 27507 |     81 |   \medskip\noindent Type variables are annotated with (finitely many) classes;
 | 
| 22317 |     82 |   these annotations are assertions that a particular polymorphic type
 | 
|  |     83 |   provides definitions for overloaded functions.
 | 
|  |     84 | 
 | 
|  |     85 |   Indeed, type classes not only allow for simple overloading
 | 
|  |     86 |   but form a generic calculus, an instance of order-sorted
 | 
| 23956 |     87 |   algebra \cite{Nipkow-Prehofer:1993,nipkow-sorts93,Wenzel:1997:TPHOL}.
 | 
| 20967 |     88 | 
 | 
| 27507 |     89 |   From a software engineering point of view, type classes
 | 
| 22317 |     90 |   correspond to interfaces in object-oriented languages like Java;
 | 
|  |     91 |   so, it is naturally desirable that type classes do not only
 | 
| 24991 |     92 |   provide functions (class parameters) but also state specifications
 | 
| 22317 |     93 |   implementations must obey.  For example, the \isa{class\ eq}
 | 
| 22550 |     94 |   above could be given the following specification, demanding that
 | 
|  |     95 |   \isa{class\ eq} is an equivalence relation obeying reflexivity,
 | 
|  |     96 |   symmetry and transitivity:
 | 
| 22317 |     97 | 
 | 
|  |     98 |   \medskip\noindent\hspace*{2ex}\isa{class\ eq\ where} \\
 | 
|  |     99 |   \hspace*{4ex}\isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} \\
 | 
|  |    100 |   \hspace*{2ex}\isa{satisfying} \\
 | 
|  |    101 |   \hspace*{4ex}\isa{refl{\isacharcolon}\ eq\ x\ x} \\
 | 
|  |    102 |   \hspace*{4ex}\isa{sym{\isacharcolon}\ eq\ x\ y\ {\isasymlongleftrightarrow}\ eq\ x\ y} \\
 | 
|  |    103 |   \hspace*{4ex}\isa{trans{\isacharcolon}\ eq\ x\ y\ {\isasymand}\ eq\ y\ z\ {\isasymlongrightarrow}\ eq\ x\ z}
 | 
|  |    104 | 
 | 
| 27507 |    105 |   \medskip\noindent From a theoretic point of view, type classes are lightweight
 | 
| 22479 |    106 |   modules; Haskell type classes may be emulated by
 | 
| 22317 |    107 |   SML functors \cite{classes_modules}. 
 | 
|  |    108 |   Isabelle/Isar offers a discipline of type classes which brings
 | 
|  |    109 |   all those aspects together:
 | 
|  |    110 | 
 | 
|  |    111 |   \begin{enumerate}
 | 
| 25004 |    112 |     \item specifying abstract parameters together with
 | 
| 22317 |    113 |        corresponding specifications,
 | 
| 24991 |    114 |     \item instantating those abstract parameters by a particular
 | 
| 22317 |    115 |        type
 | 
|  |    116 |     \item in connection with a ``less ad-hoc'' approach to overloading,
 | 
| 23956 |    117 |     \item with a direct link to the Isabelle module system
 | 
|  |    118 |       (aka locales \cite{kammueller-locales}).
 | 
| 22317 |    119 |   \end{enumerate}
 | 
|  |    120 | 
 | 
| 22550 |    121 |   \noindent Isar type classes also directly support code generation
 | 
|  |    122 |   in a Haskell like fashion.
 | 
| 22317 |    123 | 
 | 
|  |    124 |   This tutorial demonstrates common elements of structured specifications
 | 
|  |    125 |   and abstract reasoning with type classes by the algebraic hierarchy of
 | 
|  |    126 |   semigroups, monoids and groups.  Our background theory is that of
 | 
| 23956 |    127 |   Isabelle/HOL \cite{isa-tutorial}, for which some
 | 
| 22317 |    128 |   familiarity is assumed.
 | 
|  |    129 | 
 | 
|  |    130 |   Here we merely present the look-and-feel for end users.
 | 
|  |    131 |   Internally, those are mapped to more primitive Isabelle concepts.
 | 
| 23956 |    132 |   See \cite{Haftmann-Wenzel:2006:classes} for more detail.%
 | 
| 20967 |    133 | \end{isamarkuptext}%
 | 
|  |    134 | \isamarkuptrue%
 | 
|  |    135 | %
 | 
|  |    136 | \isamarkupsection{A simple algebra example \label{sec:example}%
 | 
|  |    137 | }
 | 
|  |    138 | \isamarkuptrue%
 | 
|  |    139 | %
 | 
|  |    140 | \isamarkupsubsection{Class definition%
 | 
|  |    141 | }
 | 
|  |    142 | \isamarkuptrue%
 | 
|  |    143 | %
 | 
|  |    144 | \begin{isamarkuptext}%
 | 
| 24991 |    145 | Depending on an arbitrary type \isa{{\isasymalpha}}, class \isa{semigroup} introduces a binary operator \isa{{\isasymotimes}} that is
 | 
| 20967 |    146 |   assumed to be associative:%
 | 
|  |    147 | \end{isamarkuptext}%
 | 
|  |    148 | \isamarkuptrue%
 | 
|  |    149 | \ \ \ \ \isacommand{class}\isamarkupfalse%
 | 
| 22479 |    150 | \ semigroup\ {\isacharequal}\ type\ {\isacharplus}\isanewline
 | 
| 25200 |    151 | \ \ \ \ \ \ \isakeyword{fixes}\ mult\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \ \ \ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}{\isasymotimes}{\isachardoublequoteclose}\ {\isadigit{7}}{\isadigit{0}}{\isacharparenright}\isanewline
 | 
|  |    152 | \ \ \ \ \ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 20967 |    153 | \begin{isamarkuptext}%
 | 
|  |    154 | \noindent This \isa{{\isasymCLASS}} specification consists of two
 | 
| 24991 |    155 |   parts: the \qn{operational} part names the class parameter (\isa{{\isasymFIXES}}), the \qn{logical} part specifies properties on them
 | 
| 20967 |    156 |   (\isa{{\isasymASSUMES}}).  The local \isa{{\isasymFIXES}} and \isa{{\isasymASSUMES}} are lifted to the theory toplevel, yielding the global
 | 
| 24991 |    157 |   parameter \isa{{\isachardoublequote}mult\ {\isasymColon}\ {\isasymalpha}{\isasymColon}semigroup\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequote}} and the
 | 
| 22479 |    158 |   global theorem \isa{semigroup{\isachardot}assoc{\isacharcolon}}~\isa{{\isachardoublequote}{\isasymAnd}x\ y\ z\ {\isasymColon}\ {\isasymalpha}{\isasymColon}semigroup{\isachardot}\ {\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequote}}.%
 | 
| 20967 |    159 | \end{isamarkuptext}%
 | 
|  |    160 | \isamarkuptrue%
 | 
|  |    161 | %
 | 
|  |    162 | \isamarkupsubsection{Class instantiation \label{sec:class_inst}%
 | 
|  |    163 | }
 | 
|  |    164 | \isamarkuptrue%
 | 
|  |    165 | %
 | 
|  |    166 | \begin{isamarkuptext}%
 | 
|  |    167 | The concrete type \isa{int} is made a \isa{semigroup}
 | 
| 24991 |    168 |   instance by providing a suitable definition for the class parameter
 | 
| 25533 |    169 |   \isa{mult} and a proof for the specification of \isa{assoc}.
 | 
|  |    170 |   This is accomplished by the \isa{{\isasymINSTANTIATION}} target:%
 | 
| 20967 |    171 | \end{isamarkuptext}%
 | 
|  |    172 | \isamarkuptrue%
 | 
| 25533 |    173 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
| 20967 |    174 | \ int\ {\isacharcolon}{\isacharcolon}\ semigroup\isanewline
 | 
| 25533 |    175 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    176 | \isanewline
 | 
|  |    177 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |    178 | \isanewline
 | 
|  |    179 | \ \ \ \ \ \ mult{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}i\ {\isasymotimes}\ j\ {\isacharequal}\ i\ {\isacharplus}\ {\isacharparenleft}j{\isasymColon}int{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    180 | \isanewline
 | 
|  |    181 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
| 20967 |    182 | %
 | 
|  |    183 | \isadelimproof
 | 
| 25533 |    184 | \ %
 | 
| 20967 |    185 | \endisadelimproof
 | 
|  |    186 | %
 | 
|  |    187 | \isatagproof
 | 
|  |    188 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    189 | \isanewline
 | 
| 22317 |    190 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
| 20967 |    191 | \ i\ j\ k\ {\isacharcolon}{\isacharcolon}\ int\ \isacommand{have}\isamarkupfalse%
 | 
|  |    192 | \ {\isachardoublequoteopen}{\isacharparenleft}i\ {\isacharplus}\ j{\isacharparenright}\ {\isacharplus}\ k\ {\isacharequal}\ i\ {\isacharplus}\ {\isacharparenleft}j\ {\isacharplus}\ k{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
 | 
|  |    193 | \ simp\isanewline
 | 
| 22317 |    194 | \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
 | 
| 20967 |    195 | \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    196 | \ {\isachardoublequoteopen}{\isacharparenleft}i\ {\isasymotimes}\ j{\isacharparenright}\ {\isasymotimes}\ k\ {\isacharequal}\ i\ {\isasymotimes}\ {\isacharparenleft}j\ {\isasymotimes}\ k{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    197 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
| 20967 |    198 | \ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{{\isachardot}}\isamarkupfalse%
 | 
|  |    199 | \isanewline
 | 
|  |    200 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    201 | %
 | 
|  |    202 | \endisatagproof
 | 
|  |    203 | {\isafoldproof}%
 | 
|  |    204 | %
 | 
|  |    205 | \isadelimproof
 | 
|  |    206 | %
 | 
|  |    207 | \endisadelimproof
 | 
| 25533 |    208 | \isanewline
 | 
|  |    209 | \isanewline
 | 
|  |    210 | \ \ \ \ \isacommand{end}\isamarkupfalse%
 | 
| 20967 |    211 | %
 | 
|  |    212 | \begin{isamarkuptext}%
 | 
| 25533 |    213 | \noindent \isa{{\isasymINSTANTIATION}} allows to define class parameters
 | 
|  |    214 |   at a particular instance using common specification tools (here,
 | 
|  |    215 |   \isa{{\isasymDEFINITION}}).  The concluding \isa{{\isasymINSTANCE}}
 | 
|  |    216 |   opens a proof that the given parameters actually conform
 | 
|  |    217 |   to the class specification.  Note that the first proof step
 | 
|  |    218 |   is the \isa{default} method,
 | 
|  |    219 |   which for such instance proofs maps to the \isa{intro{\isacharunderscore}classes} method.
 | 
|  |    220 |   This boils down an instance judgement to the relevant primitive
 | 
| 22317 |    221 |   proof goals and should conveniently always be the first method applied
 | 
|  |    222 |   in an instantiation proof.
 | 
|  |    223 | 
 | 
| 25533 |    224 |   From now on, the type-checker will consider \isa{int}
 | 
|  |    225 |   as a \isa{semigroup} automatically, i.e.\ any general results
 | 
|  |    226 |   are immediately available on concrete instances.
 | 
| 22550 |    227 |   \medskip Another instance of \isa{semigroup} are the natural numbers:%
 | 
| 20967 |    228 | \end{isamarkuptext}%
 | 
|  |    229 | \isamarkuptrue%
 | 
| 25533 |    230 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
| 20967 |    231 | \ nat\ {\isacharcolon}{\isacharcolon}\ semigroup\isanewline
 | 
| 25533 |    232 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    233 | \isanewline
 | 
| 25871 |    234 | \ \ \ \ \isacommand{primrec}\isamarkupfalse%
 | 
|  |    235 | \ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline
 | 
|  |    236 | \ \ \ \ \ \ {\isachardoublequoteopen}{\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
 | 
|  |    237 | \ \ \ \ \ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ Suc\ {\isacharparenleft}m\ {\isasymotimes}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 25533 |    238 | \isanewline
 | 
|  |    239 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
| 20967 |    240 | %
 | 
|  |    241 | \isadelimproof
 | 
| 25533 |    242 | \ %
 | 
| 20967 |    243 | \endisadelimproof
 | 
|  |    244 | %
 | 
|  |    245 | \isatagproof
 | 
|  |    246 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    247 | \isanewline
 | 
|  |    248 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    249 | \ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\ \isanewline
 | 
|  |    250 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    251 | \ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 25871 |    252 | \ \ \ \ \ \ \ \ \isacommand{by}\isamarkupfalse%
 | 
|  |    253 | \ {\isacharparenleft}induct\ m{\isacharparenright}\ auto\isanewline
 | 
| 20967 |    254 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    255 | %
 | 
|  |    256 | \endisatagproof
 | 
|  |    257 | {\isafoldproof}%
 | 
|  |    258 | %
 | 
|  |    259 | \isadelimproof
 | 
|  |    260 | %
 | 
|  |    261 | \endisadelimproof
 | 
| 25533 |    262 | \isanewline
 | 
|  |    263 | \isanewline
 | 
|  |    264 | \ \ \ \ \isacommand{end}\isamarkupfalse%
 | 
| 20967 |    265 | %
 | 
| 25871 |    266 | \begin{isamarkuptext}%
 | 
|  |    267 | \noindent Note the occurence of the name \isa{mult{\isacharunderscore}nat}
 | 
|  |    268 |   in the primrec declaration;  by default, the local name of
 | 
|  |    269 |   a class operation \isa{f} to instantiate on type constructor
 | 
|  |    270 |   \isa{{\isasymkappa}} are mangled as \isa{f{\isacharunderscore}{\isasymkappa}}.  In case of uncertainty,
 | 
|  |    271 |   these names may be inspected using the \isa{{\isasymPRINTCONTEXT}} command
 | 
|  |    272 |   or the corresponding ProofGeneral button.%
 | 
|  |    273 | \end{isamarkuptext}%
 | 
|  |    274 | \isamarkuptrue%
 | 
|  |    275 | %
 | 
| 25247 |    276 | \isamarkupsubsection{Lifting and parametric types%
 | 
|  |    277 | }
 | 
|  |    278 | \isamarkuptrue%
 | 
|  |    279 | %
 | 
| 20967 |    280 | \begin{isamarkuptext}%
 | 
| 25247 |    281 | Overloaded definitions giving on class instantiation
 | 
|  |    282 |   may include recursion over the syntactic structure of types.
 | 
|  |    283 |   As a canonical example, we model product semigroups
 | 
|  |    284 |   using our simple algebra:%
 | 
| 20967 |    285 | \end{isamarkuptext}%
 | 
|  |    286 | \isamarkuptrue%
 | 
| 25533 |    287 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
| 25247 |    288 | \ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}semigroup{\isacharcomma}\ semigroup{\isacharparenright}\ semigroup\isanewline
 | 
| 25533 |    289 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    290 | \isanewline
 | 
|  |    291 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |    292 | \isanewline
 | 
|  |    293 | \ \ \ \ \ \ mult{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}p\isactrlisub {\isadigit{1}}\ {\isasymotimes}\ p\isactrlisub {\isadigit{2}}\ {\isacharequal}\ {\isacharparenleft}fst\ p\isactrlisub {\isadigit{1}}\ {\isasymotimes}\ fst\ p\isactrlisub {\isadigit{2}}{\isacharcomma}\ snd\ p\isactrlisub {\isadigit{1}}\ {\isasymotimes}\ snd\ p\isactrlisub {\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    294 | \isanewline
 | 
|  |    295 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
| 20967 |    296 | %
 | 
|  |    297 | \isadelimproof
 | 
| 25533 |    298 | \ %
 | 
| 20967 |    299 | \endisadelimproof
 | 
|  |    300 | %
 | 
|  |    301 | \isatagproof
 | 
|  |    302 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    303 | \isanewline
 | 
|  |    304 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
| 25533 |    305 | \ p\isactrlisub {\isadigit{1}}\ p\isactrlisub {\isadigit{2}}\ p\isactrlisub {\isadigit{3}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isasymColon}semigroup\ {\isasymtimes}\ {\isasymbeta}{\isasymColon}semigroup{\isachardoublequoteclose}\isanewline
 | 
| 20967 |    306 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    307 | \ {\isachardoublequoteopen}p\isactrlisub {\isadigit{1}}\ {\isasymotimes}\ p\isactrlisub {\isadigit{2}}\ {\isasymotimes}\ p\isactrlisub {\isadigit{3}}\ {\isacharequal}\ p\isactrlisub {\isadigit{1}}\ {\isasymotimes}\ {\isacharparenleft}p\isactrlisub {\isadigit{2}}\ {\isasymotimes}\ p\isactrlisub {\isadigit{3}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    308 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
|  |    309 | \ mult{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
 | 
|  |    310 | \ {\isacharparenleft}simp\ add{\isacharcolon}\ assoc{\isacharparenright}\isanewline
 | 
| 20967 |    311 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    312 | %
 | 
|  |    313 | \endisatagproof
 | 
|  |    314 | {\isafoldproof}%
 | 
|  |    315 | %
 | 
|  |    316 | \isadelimproof
 | 
|  |    317 | %
 | 
|  |    318 | \endisadelimproof
 | 
| 25533 |    319 | \ \ \ \ \ \ \isanewline
 | 
|  |    320 | \isanewline
 | 
|  |    321 | \ \ \ \ \isacommand{end}\isamarkupfalse%
 | 
| 20967 |    322 | %
 | 
| 25247 |    323 | \begin{isamarkuptext}%
 | 
|  |    324 | \noindent Associativity from product semigroups is
 | 
|  |    325 |   established using
 | 
|  |    326 |   the definition of \isa{{\isasymotimes}} on products and the hypothetical
 | 
| 27507 |    327 |   associativity of the type components;  these hypotheses
 | 
| 25247 |    328 |   are facts due to the \isa{semigroup} constraints imposed
 | 
|  |    329 |   on the type components by the \isa{instance} proposition.
 | 
|  |    330 |   Indeed, this pattern often occurs with parametric types
 | 
|  |    331 |   and type classes.%
 | 
|  |    332 | \end{isamarkuptext}%
 | 
|  |    333 | \isamarkuptrue%
 | 
|  |    334 | %
 | 
|  |    335 | \isamarkupsubsection{Subclassing%
 | 
| 20967 |    336 | }
 | 
|  |    337 | \isamarkuptrue%
 | 
|  |    338 | %
 | 
|  |    339 | \begin{isamarkuptext}%
 | 
| 22317 |    340 | We define a subclass \isa{monoidl} (a semigroup with a left-hand neutral)
 | 
| 20967 |    341 |   by extending \isa{semigroup}
 | 
| 24991 |    342 |   with one additional parameter \isa{neutral} together
 | 
| 20967 |    343 |   with its property:%
 | 
|  |    344 | \end{isamarkuptext}%
 | 
|  |    345 | \isamarkuptrue%
 | 
|  |    346 | \ \ \ \ \isacommand{class}\isamarkupfalse%
 | 
|  |    347 | \ monoidl\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline
 | 
| 25200 |    348 | \ \ \ \ \ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isachardoublequoteclose}\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline
 | 
|  |    349 | \ \ \ \ \ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}%
 | 
| 20967 |    350 | \begin{isamarkuptext}%
 | 
| 25247 |    351 | \noindent Again, we prove some instances, by
 | 
| 24991 |    352 |   providing suitable parameter definitions and proofs for the
 | 
| 27507 |    353 |   additional specifications.  Observe that instantiations
 | 
| 25533 |    354 |   for types with the same arity may be simultaneous:%
 | 
| 20967 |    355 | \end{isamarkuptext}%
 | 
|  |    356 | \isamarkuptrue%
 | 
| 25533 |    357 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
|  |    358 | \ nat\ \isakeyword{and}\ int\ {\isacharcolon}{\isacharcolon}\ monoidl\isanewline
 | 
|  |    359 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    360 | \isanewline
 | 
|  |    361 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |    362 | \isanewline
 | 
|  |    363 | \ \ \ \ \ \ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    364 | \isanewline
 | 
|  |    365 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |    366 | \isanewline
 | 
|  |    367 | \ \ \ \ \ \ neutral{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}int{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    368 | \isanewline
 | 
| 20967 |    369 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
|  |    370 | %
 | 
|  |    371 | \isadelimproof
 | 
| 25533 |    372 | \ %
 | 
| 20967 |    373 | \endisadelimproof
 | 
|  |    374 | %
 | 
|  |    375 | \isatagproof
 | 
|  |    376 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    377 | \isanewline
 | 
|  |    378 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    379 | \ n\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
 | 
|  |    380 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    381 | \ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
 | 
|  |    382 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
| 26991 |    383 | \ neutral{\isacharunderscore}nat{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
 | 
| 20967 |    384 | \ simp\isanewline
 | 
| 25533 |    385 | \ \ \ \ \isacommand{next}\isamarkupfalse%
 | 
| 20967 |    386 | \isanewline
 | 
|  |    387 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    388 | \ k\ {\isacharcolon}{\isacharcolon}\ int\isanewline
 | 
|  |    389 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    390 | \ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ k\ {\isacharequal}\ k{\isachardoublequoteclose}\isanewline
 | 
|  |    391 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
| 20967 |    392 | \ neutral{\isacharunderscore}int{\isacharunderscore}def\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
 | 
|  |    393 | \ simp\isanewline
 | 
|  |    394 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    395 | %
 | 
|  |    396 | \endisatagproof
 | 
|  |    397 | {\isafoldproof}%
 | 
|  |    398 | %
 | 
|  |    399 | \isadelimproof
 | 
|  |    400 | %
 | 
|  |    401 | \endisadelimproof
 | 
|  |    402 | \isanewline
 | 
| 25533 |    403 | \isanewline
 | 
|  |    404 | \ \ \ \ \isacommand{end}\isamarkupfalse%
 | 
|  |    405 | \isanewline
 | 
|  |    406 | \isanewline
 | 
|  |    407 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
| 25247 |    408 | \ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}monoidl{\isacharcomma}\ monoidl{\isacharparenright}\ monoidl\isanewline
 | 
| 25533 |    409 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    410 | \isanewline
 | 
|  |    411 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |    412 | \isanewline
 | 
|  |    413 | \ \ \ \ \ \ neutral{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ {\isacharparenleft}{\isasymone}{\isacharcomma}\ {\isasymone}{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    414 | \isanewline
 | 
|  |    415 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
| 20967 |    416 | %
 | 
|  |    417 | \isadelimproof
 | 
| 25533 |    418 | \ %
 | 
| 20967 |    419 | \endisadelimproof
 | 
|  |    420 | %
 | 
|  |    421 | \isatagproof
 | 
|  |    422 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    423 | \isanewline
 | 
|  |    424 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
| 25533 |    425 | \ p\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isasymColon}monoidl\ {\isasymtimes}\ {\isasymbeta}{\isasymColon}monoidl{\isachardoublequoteclose}\isanewline
 | 
| 20967 |    426 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    427 | \ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
 | 
|  |    428 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
|  |    429 | \ neutral{\isacharunderscore}prod{\isacharunderscore}def\ mult{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
 | 
|  |    430 | \ {\isacharparenleft}simp\ add{\isacharcolon}\ neutl{\isacharparenright}\isanewline
 | 
| 20967 |    431 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    432 | %
 | 
|  |    433 | \endisatagproof
 | 
|  |    434 | {\isafoldproof}%
 | 
|  |    435 | %
 | 
|  |    436 | \isadelimproof
 | 
|  |    437 | %
 | 
|  |    438 | \endisadelimproof
 | 
| 25533 |    439 | \isanewline
 | 
|  |    440 | \isanewline
 | 
|  |    441 | \ \ \ \isacommand{end}\isamarkupfalse%
 | 
| 20967 |    442 | %
 | 
|  |    443 | \begin{isamarkuptext}%
 | 
| 22317 |    444 | \noindent Fully-fledged monoids are modelled by another subclass
 | 
| 24991 |    445 |   which does not add new parameters but tightens the specification:%
 | 
| 20967 |    446 | \end{isamarkuptext}%
 | 
|  |    447 | \isamarkuptrue%
 | 
|  |    448 | \ \ \ \ \isacommand{class}\isamarkupfalse%
 | 
|  |    449 | \ monoid\ {\isacharequal}\ monoidl\ {\isacharplus}\isanewline
 | 
| 25247 |    450 | \ \ \ \ \ \ \isakeyword{assumes}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
 | 
|  |    451 | \isanewline
 | 
| 25533 |    452 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
|  |    453 | \ nat\ \isakeyword{and}\ int\ {\isacharcolon}{\isacharcolon}\ monoid\ \isanewline
 | 
|  |    454 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    455 | \isanewline
 | 
| 20967 |    456 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
|  |    457 | %
 | 
|  |    458 | \isadelimproof
 | 
| 25533 |    459 | \ %
 | 
| 20967 |    460 | \endisadelimproof
 | 
|  |    461 | %
 | 
|  |    462 | \isatagproof
 | 
|  |    463 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    464 | \isanewline
 | 
|  |    465 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    466 | \ n\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
 | 
|  |    467 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    468 | \ {\isachardoublequoteopen}n\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
 | 
|  |    469 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
| 25871 |    470 | \ neutral{\isacharunderscore}nat{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
 | 
|  |    471 | \ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
 | 
| 25533 |    472 | \ \ \ \ \isacommand{next}\isamarkupfalse%
 | 
| 20967 |    473 | \isanewline
 | 
|  |    474 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    475 | \ k\ {\isacharcolon}{\isacharcolon}\ int\isanewline
 | 
|  |    476 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
| 25247 |    477 | \ {\isachardoublequoteopen}k\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ k{\isachardoublequoteclose}\isanewline
 | 
|  |    478 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
| 20967 |    479 | \ neutral{\isacharunderscore}int{\isacharunderscore}def\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
 | 
|  |    480 | \ simp\isanewline
 | 
| 25247 |    481 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    482 | %
 | 
|  |    483 | \endisatagproof
 | 
|  |    484 | {\isafoldproof}%
 | 
|  |    485 | %
 | 
|  |    486 | \isadelimproof
 | 
|  |    487 | %
 | 
|  |    488 | \endisadelimproof
 | 
| 20967 |    489 | \isanewline
 | 
| 25533 |    490 | \isanewline
 | 
|  |    491 | \ \ \ \ \isacommand{end}\isamarkupfalse%
 | 
|  |    492 | \isanewline
 | 
|  |    493 | \isanewline
 | 
|  |    494 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
|  |    495 | \ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}monoid{\isacharcomma}\ monoid{\isacharparenright}\ monoid\isanewline
 | 
|  |    496 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    497 | \isanewline
 | 
| 25247 |    498 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
|  |    499 | %
 | 
|  |    500 | \isadelimproof
 | 
| 25533 |    501 | \ %
 | 
| 25247 |    502 | \endisadelimproof
 | 
|  |    503 | %
 | 
|  |    504 | \isatagproof
 | 
|  |    505 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    506 | \ \isanewline
 | 
|  |    507 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
| 25533 |    508 | \ p\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isasymColon}monoid\ {\isasymtimes}\ {\isasymbeta}{\isasymColon}monoid{\isachardoublequoteclose}\isanewline
 | 
| 25247 |    509 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
|  |    510 | \ {\isachardoublequoteopen}p\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
 | 
|  |    511 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
|  |    512 | \ neutral{\isacharunderscore}prod{\isacharunderscore}def\ mult{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
 | 
|  |    513 | \ {\isacharparenleft}simp\ add{\isacharcolon}\ neutr{\isacharparenright}\isanewline
 | 
| 20967 |    514 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    515 | %
 | 
|  |    516 | \endisatagproof
 | 
|  |    517 | {\isafoldproof}%
 | 
|  |    518 | %
 | 
|  |    519 | \isadelimproof
 | 
|  |    520 | %
 | 
|  |    521 | \endisadelimproof
 | 
| 25533 |    522 | \isanewline
 | 
|  |    523 | \isanewline
 | 
|  |    524 | \ \ \ \ \isacommand{end}\isamarkupfalse%
 | 
| 22317 |    525 | %
 | 
|  |    526 | \begin{isamarkuptext}%
 | 
|  |    527 | \noindent To finish our small algebra example, we add a \isa{group} class
 | 
|  |    528 |   with a corresponding instance:%
 | 
|  |    529 | \end{isamarkuptext}%
 | 
|  |    530 | \isamarkuptrue%
 | 
| 20967 |    531 | \ \ \ \ \isacommand{class}\isamarkupfalse%
 | 
|  |    532 | \ group\ {\isacharequal}\ monoidl\ {\isacharplus}\isanewline
 | 
| 25200 |    533 | \ \ \ \ \ \ \isakeyword{fixes}\ inverse\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \ \ \ {\isacharparenleft}{\isachardoublequoteopen}{\isacharparenleft}{\isacharunderscore}{\isasymdiv}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbrackleft}{\isadigit{1}}{\isadigit{0}}{\isadigit{0}}{\isadigit{0}}{\isacharbrackright}\ {\isadigit{9}}{\isadigit{9}}{\isadigit{9}}{\isacharparenright}\isanewline
 | 
|  |    534 | \ \ \ \ \ \ \isakeyword{assumes}\ invl{\isacharcolon}\ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
 | 
| 20967 |    535 | \isanewline
 | 
| 25533 |    536 | \ \ \ \ \isacommand{instantiation}\isamarkupfalse%
 | 
| 20967 |    537 | \ int\ {\isacharcolon}{\isacharcolon}\ group\isanewline
 | 
| 25533 |    538 | \ \ \ \ \isakeyword{begin}\isanewline
 | 
|  |    539 | \isanewline
 | 
|  |    540 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |    541 | \isanewline
 | 
|  |    542 | \ \ \ \ \ \ inverse{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}i{\isasymdiv}\ {\isacharequal}\ {\isacharminus}\ {\isacharparenleft}i{\isasymColon}int{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |    543 | \isanewline
 | 
|  |    544 | \ \ \ \ \isacommand{instance}\isamarkupfalse%
 | 
| 20967 |    545 | %
 | 
|  |    546 | \isadelimproof
 | 
| 25533 |    547 | \ %
 | 
| 20967 |    548 | \endisadelimproof
 | 
|  |    549 | %
 | 
|  |    550 | \isatagproof
 | 
|  |    551 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    552 | \isanewline
 | 
|  |    553 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    554 | \ i\ {\isacharcolon}{\isacharcolon}\ int\isanewline
 | 
|  |    555 | \ \ \ \ \ \ \isacommand{have}\isamarkupfalse%
 | 
|  |    556 | \ {\isachardoublequoteopen}{\isacharminus}i\ {\isacharplus}\ i\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
 | 
|  |    557 | \ simp\isanewline
 | 
|  |    558 | \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
 | 
|  |    559 | \ \isacommand{show}\isamarkupfalse%
 | 
| 22550 |    560 | \ {\isachardoublequoteopen}i{\isasymdiv}\ {\isasymotimes}\ i\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
 | 
| 25247 |    561 | \ \ \ \ \ \ \ \ \isacommand{unfolding}\isamarkupfalse%
 | 
|  |    562 | \ mult{\isacharunderscore}int{\isacharunderscore}def\ neutral{\isacharunderscore}int{\isacharunderscore}def\ inverse{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{{\isachardot}}\isamarkupfalse%
 | 
| 20967 |    563 | \isanewline
 | 
|  |    564 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    565 | %
 | 
|  |    566 | \endisatagproof
 | 
|  |    567 | {\isafoldproof}%
 | 
|  |    568 | %
 | 
|  |    569 | \isadelimproof
 | 
|  |    570 | %
 | 
|  |    571 | \endisadelimproof
 | 
| 25533 |    572 | \isanewline
 | 
|  |    573 | \isanewline
 | 
|  |    574 | \ \ \ \ \isacommand{end}\isamarkupfalse%
 | 
| 20967 |    575 | %
 | 
| 22317 |    576 | \isamarkupsection{Type classes as locales%
 | 
|  |    577 | }
 | 
|  |    578 | \isamarkuptrue%
 | 
|  |    579 | %
 | 
|  |    580 | \isamarkupsubsection{A look behind the scene%
 | 
|  |    581 | }
 | 
|  |    582 | \isamarkuptrue%
 | 
|  |    583 | %
 | 
|  |    584 | \begin{isamarkuptext}%
 | 
| 22479 |    585 | The example above gives an impression how Isar type classes work
 | 
|  |    586 |   in practice.  As stated in the introduction, classes also provide
 | 
|  |    587 |   a link to Isar's locale system.  Indeed, the logical core of a class
 | 
|  |    588 |   is nothing else than a locale:%
 | 
|  |    589 | \end{isamarkuptext}%
 | 
|  |    590 | \isamarkuptrue%
 | 
|  |    591 | \isacommand{class}\isamarkupfalse%
 | 
|  |    592 | \ idem\ {\isacharequal}\ type\ {\isacharplus}\isanewline
 | 
|  |    593 | \ \ \isakeyword{fixes}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\isanewline
 | 
|  |    594 | \ \ \isakeyword{assumes}\ idem{\isacharcolon}\ {\isachardoublequoteopen}f\ {\isacharparenleft}f\ x{\isacharparenright}\ {\isacharequal}\ f\ x{\isachardoublequoteclose}%
 | 
|  |    595 | \begin{isamarkuptext}%
 | 
|  |    596 | \noindent essentially introduces the locale%
 | 
|  |    597 | \end{isamarkuptext}%
 | 
|  |    598 | \isamarkuptrue%
 | 
| 22317 |    599 | %
 | 
| 22479 |    600 | \isadelimML
 | 
|  |    601 | %
 | 
|  |    602 | \endisadelimML
 | 
|  |    603 | %
 | 
|  |    604 | \isatagML
 | 
|  |    605 | %
 | 
|  |    606 | \endisatagML
 | 
|  |    607 | {\isafoldML}%
 | 
|  |    608 | %
 | 
|  |    609 | \isadelimML
 | 
|  |    610 | %
 | 
|  |    611 | \endisadelimML
 | 
|  |    612 | \isacommand{locale}\isamarkupfalse%
 | 
|  |    613 | \ idem\ {\isacharequal}\isanewline
 | 
|  |    614 | \ \ \isakeyword{fixes}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\isanewline
 | 
|  |    615 | \ \ \isakeyword{assumes}\ idem{\isacharcolon}\ {\isachardoublequoteopen}f\ {\isacharparenleft}f\ x{\isacharparenright}\ {\isacharequal}\ f\ x{\isachardoublequoteclose}%
 | 
|  |    616 | \begin{isamarkuptext}%
 | 
| 22550 |    617 | \noindent together with corresponding constant(s):%
 | 
| 22479 |    618 | \end{isamarkuptext}%
 | 
|  |    619 | \isamarkuptrue%
 | 
|  |    620 | \isacommand{consts}\isamarkupfalse%
 | 
| 22550 |    621 | \ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}%
 | 
|  |    622 | \begin{isamarkuptext}%
 | 
|  |    623 | \noindent The connection to the type system is done by means
 | 
|  |    624 |   of a primitive axclass%
 | 
|  |    625 | \end{isamarkuptext}%
 | 
|  |    626 | \isamarkuptrue%
 | 
| 22479 |    627 | \isacommand{axclass}\isamarkupfalse%
 | 
|  |    628 | \ idem\ {\isacharless}\ type\isanewline
 | 
|  |    629 | \ \ idem{\isacharcolon}\ {\isachardoublequoteopen}f\ {\isacharparenleft}f\ x{\isacharparenright}\ {\isacharequal}\ f\ x{\isachardoublequoteclose}%
 | 
|  |    630 | \begin{isamarkuptext}%
 | 
| 22649 |    631 | \noindent together with a corresponding interpretation:%
 | 
| 22479 |    632 | \end{isamarkuptext}%
 | 
|  |    633 | \isamarkuptrue%
 | 
|  |    634 | \isacommand{interpretation}\isamarkupfalse%
 | 
|  |    635 | \ idem{\isacharunderscore}class{\isacharcolon}\isanewline
 | 
| 25533 |    636 | \ \ idem\ {\isacharbrackleft}{\isachardoublequoteopen}f\ {\isasymColon}\ {\isacharparenleft}{\isasymalpha}{\isasymColon}idem{\isacharparenright}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}{\isacharbrackright}\isanewline
 | 
| 22479 |    637 | %
 | 
|  |    638 | \isadelimproof
 | 
|  |    639 | %
 | 
|  |    640 | \endisadelimproof
 | 
|  |    641 | %
 | 
|  |    642 | \isatagproof
 | 
|  |    643 | \isacommand{by}\isamarkupfalse%
 | 
| 22550 |    644 | \ unfold{\isacharunderscore}locales\ {\isacharparenleft}rule\ idem{\isacharparenright}%
 | 
| 22479 |    645 | \endisatagproof
 | 
|  |    646 | {\isafoldproof}%
 | 
|  |    647 | %
 | 
|  |    648 | \isadelimproof
 | 
|  |    649 | %
 | 
|  |    650 | \endisadelimproof
 | 
|  |    651 | %
 | 
|  |    652 | \isadelimML
 | 
|  |    653 | %
 | 
|  |    654 | \endisadelimML
 | 
|  |    655 | %
 | 
|  |    656 | \isatagML
 | 
|  |    657 | %
 | 
|  |    658 | \endisatagML
 | 
|  |    659 | {\isafoldML}%
 | 
|  |    660 | %
 | 
|  |    661 | \isadelimML
 | 
|  |    662 | %
 | 
|  |    663 | \endisadelimML
 | 
|  |    664 | %
 | 
|  |    665 | \begin{isamarkuptext}%
 | 
|  |    666 | This give you at hand the full power of the Isabelle module system;
 | 
|  |    667 |   conclusions in locale \isa{idem} are implicitly propagated
 | 
|  |    668 |   to class \isa{idem}.%
 | 
| 22317 |    669 | \end{isamarkuptext}%
 | 
|  |    670 | \isamarkuptrue%
 | 
|  |    671 | %
 | 
| 20967 |    672 | \isamarkupsubsection{Abstract reasoning%
 | 
|  |    673 | }
 | 
|  |    674 | \isamarkuptrue%
 | 
|  |    675 | %
 | 
|  |    676 | \begin{isamarkuptext}%
 | 
| 22479 |    677 | Isabelle locales enable reasoning at a general level, while results
 | 
| 20967 |    678 |   are implicitly transferred to all instances.  For example, we can
 | 
|  |    679 |   now establish the \isa{left{\isacharunderscore}cancel} lemma for groups, which
 | 
| 25247 |    680 |   states that the function \isa{{\isacharparenleft}x\ {\isasymotimes}{\isacharparenright}} is injective:%
 | 
| 20967 |    681 | \end{isamarkuptext}%
 | 
|  |    682 | \isamarkuptrue%
 | 
|  |    683 | \ \ \ \ \isacommand{lemma}\isamarkupfalse%
 | 
| 25200 |    684 | \ {\isacharparenleft}\isakeyword{in}\ group{\isacharparenright}\ left{\isacharunderscore}cancel{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequoteclose}\isanewline
 | 
| 20967 |    685 | %
 | 
|  |    686 | \isadelimproof
 | 
|  |    687 | \ \ \ \ %
 | 
|  |    688 | \endisadelimproof
 | 
|  |    689 | %
 | 
|  |    690 | \isatagproof
 | 
|  |    691 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    692 | \isanewline
 | 
| 25247 |    693 | \ \ \ \ \ \ \isacommand{assume}\isamarkupfalse%
 | 
| 25200 |    694 | \ {\isachardoublequoteopen}x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z{\isachardoublequoteclose}\isanewline
 | 
| 22479 |    695 | \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
 | 
| 20967 |    696 | \ \isacommand{have}\isamarkupfalse%
 | 
| 25200 |    697 | \ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ {\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isacharequal}\ x{\isasymdiv}\ {\isasymotimes}\ {\isacharparenleft}x\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
 | 
| 20967 |    698 | \ simp\isanewline
 | 
| 22479 |    699 | \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
 | 
| 20967 |    700 | \ \isacommand{have}\isamarkupfalse%
 | 
| 25200 |    701 | \ {\isachardoublequoteopen}{\isacharparenleft}x{\isasymdiv}\ {\isasymotimes}\ x{\isacharparenright}\ {\isasymotimes}\ y\ {\isacharequal}\ {\isacharparenleft}x{\isasymdiv}\ {\isasymotimes}\ x{\isacharparenright}\ {\isasymotimes}\ z{\isachardoublequoteclose}\ \isacommand{using}\isamarkupfalse%
 | 
| 20967 |    702 | \ assoc\ \isacommand{by}\isamarkupfalse%
 | 
|  |    703 | \ simp\isanewline
 | 
| 22479 |    704 | \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
 | 
| 20967 |    705 | \ \isacommand{show}\isamarkupfalse%
 | 
|  |    706 | \ {\isachardoublequoteopen}y\ {\isacharequal}\ z{\isachardoublequoteclose}\ \isacommand{using}\isamarkupfalse%
 | 
|  |    707 | \ neutl\ \isakeyword{and}\ invl\ \isacommand{by}\isamarkupfalse%
 | 
|  |    708 | \ simp\isanewline
 | 
|  |    709 | \ \ \ \ \isacommand{next}\isamarkupfalse%
 | 
|  |    710 | \isanewline
 | 
| 25247 |    711 | \ \ \ \ \ \ \isacommand{assume}\isamarkupfalse%
 | 
| 20967 |    712 | \ {\isachardoublequoteopen}y\ {\isacharequal}\ z{\isachardoublequoteclose}\isanewline
 | 
| 22479 |    713 | \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
 | 
| 20967 |    714 | \ \isacommand{show}\isamarkupfalse%
 | 
| 25200 |    715 | \ {\isachardoublequoteopen}x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
 | 
| 20967 |    716 | \ simp\isanewline
 | 
|  |    717 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    718 | %
 | 
|  |    719 | \endisatagproof
 | 
|  |    720 | {\isafoldproof}%
 | 
|  |    721 | %
 | 
|  |    722 | \isadelimproof
 | 
|  |    723 | %
 | 
|  |    724 | \endisadelimproof
 | 
|  |    725 | %
 | 
|  |    726 | \begin{isamarkuptext}%
 | 
|  |    727 | \noindent Here the \qt{\isa{{\isasymIN}\ group}} target specification
 | 
|  |    728 |   indicates that the result is recorded within that context for later
 | 
| 22479 |    729 |   use.  This local theorem is also lifted to the global one \isa{group{\isachardot}left{\isacharunderscore}cancel{\isacharcolon}} \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z\ {\isasymColon}\ {\isasymalpha}{\isasymColon}group{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}.  Since type \isa{int} has been made an instance of
 | 
|  |    730 |   \isa{group} before, we may refer to that fact as well: \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z\ {\isasymColon}\ int{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}.%
 | 
| 20967 |    731 | \end{isamarkuptext}%
 | 
|  |    732 | \isamarkuptrue%
 | 
|  |    733 | %
 | 
| 23956 |    734 | \isamarkupsubsection{Derived definitions%
 | 
|  |    735 | }
 | 
|  |    736 | \isamarkuptrue%
 | 
|  |    737 | %
 | 
|  |    738 | \begin{isamarkuptext}%
 | 
|  |    739 | Isabelle locales support a concept of local definitions
 | 
|  |    740 |   in locales:%
 | 
|  |    741 | \end{isamarkuptext}%
 | 
|  |    742 | \isamarkuptrue%
 | 
| 25871 |    743 | \ \ \ \ \isacommand{primrec}\isamarkupfalse%
 | 
| 23956 |    744 | \ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\isanewline
 | 
|  |    745 | \ \ \ \ \ \ pow{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
 | 
| 25200 |    746 | \ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
 | 
|  |    747 | \ \ \ \ \ \ {\isacharbar}\ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x{\isachardoublequoteclose}%
 | 
| 23956 |    748 | \begin{isamarkuptext}%
 | 
|  |    749 | \noindent If the locale \isa{group} is also a class, this local
 | 
|  |    750 |   definition is propagated onto a global definition of
 | 
|  |    751 |   \isa{{\isachardoublequote}pow{\isacharunderscore}nat\ {\isasymColon}\ nat\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoid\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoid{\isachardoublequote}}
 | 
|  |    752 |   with corresponding theorems
 | 
|  |    753 | 
 | 
|  |    754 |   \isa{pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}\isasep\isanewline%
 | 
|  |    755 | pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x}.
 | 
|  |    756 | 
 | 
|  |    757 |   \noindent As you can see from this example, for local
 | 
|  |    758 |   definitions you may use any specification tool
 | 
|  |    759 |   which works together with locales (e.g. \cite{krauss2006}).%
 | 
|  |    760 | \end{isamarkuptext}%
 | 
|  |    761 | \isamarkuptrue%
 | 
|  |    762 | %
 | 
| 25247 |    763 | \isamarkupsubsection{A functor analogy%
 | 
|  |    764 | }
 | 
|  |    765 | \isamarkuptrue%
 | 
|  |    766 | %
 | 
|  |    767 | \begin{isamarkuptext}%
 | 
|  |    768 | We introduced Isar classes by analogy to type classes
 | 
|  |    769 |   functional programming;  if we reconsider this in the
 | 
|  |    770 |   context of what has been said about type classes and locales,
 | 
|  |    771 |   we can drive this analogy further by stating that type
 | 
|  |    772 |   classes essentially correspond to functors which have
 | 
|  |    773 |   a canonical interpretation as type classes.
 | 
|  |    774 |   Anyway, there is also the possibility of other interpretations.
 | 
|  |    775 |   For example, also \isa{list}s form a monoid with
 | 
|  |    776 |   \isa{op\ {\isacharat}} and \isa{{\isacharbrackleft}{\isacharbrackright}} as operations, but it
 | 
|  |    777 |   seems inappropriate to apply to lists
 | 
| 27507 |    778 |   the same operations as for genuinely algebraic types.
 | 
| 25247 |    779 |   In such a case, we simply can do a particular interpretation
 | 
|  |    780 |   of monoids for lists:%
 | 
|  |    781 | \end{isamarkuptext}%
 | 
|  |    782 | \isamarkuptrue%
 | 
|  |    783 | \ \ \ \ \isacommand{interpretation}\isamarkupfalse%
 | 
|  |    784 | \ list{\isacharunderscore}monoid{\isacharcolon}\ monoid\ {\isacharbrackleft}{\isachardoublequoteopen}op\ {\isacharat}{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}{\isacharbrackright}\isanewline
 | 
|  |    785 | %
 | 
|  |    786 | \isadelimproof
 | 
|  |    787 | \ \ \ \ \ \ %
 | 
|  |    788 | \endisadelimproof
 | 
|  |    789 | %
 | 
|  |    790 | \isatagproof
 | 
|  |    791 | \isacommand{by}\isamarkupfalse%
 | 
|  |    792 | \ unfold{\isacharunderscore}locales\ auto%
 | 
|  |    793 | \endisatagproof
 | 
|  |    794 | {\isafoldproof}%
 | 
|  |    795 | %
 | 
|  |    796 | \isadelimproof
 | 
|  |    797 | %
 | 
|  |    798 | \endisadelimproof
 | 
|  |    799 | %
 | 
|  |    800 | \begin{isamarkuptext}%
 | 
|  |    801 | \noindent This enables us to apply facts on monoids
 | 
|  |    802 |   to lists, e.g. \isa{{\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ x\ {\isacharequal}\ x}.
 | 
|  |    803 | 
 | 
|  |    804 |   When using this interpretation pattern, it may also
 | 
|  |    805 |   be appropriate to map derived definitions accordingly:%
 | 
|  |    806 | \end{isamarkuptext}%
 | 
|  |    807 | \isamarkuptrue%
 | 
|  |    808 | \ \ \ \ \isacommand{fun}\isamarkupfalse%
 | 
|  |    809 | \isanewline
 | 
| 25533 |    810 | \ \ \ \ \ \ replicate\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isasymalpha}\ list\ {\isasymRightarrow}\ {\isasymalpha}\ list{\isachardoublequoteclose}\isanewline
 | 
| 25247 |    811 | \ \ \ \ \isakeyword{where}\isanewline
 | 
|  |    812 | \ \ \ \ \ \ {\isachardoublequoteopen}replicate\ {\isadigit{0}}\ {\isacharunderscore}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
 | 
|  |    813 | \ \ \ \ \ \ {\isacharbar}\ {\isachardoublequoteopen}replicate\ {\isacharparenleft}Suc\ n{\isacharparenright}\ xs\ {\isacharequal}\ xs\ {\isacharat}\ replicate\ n\ xs{\isachardoublequoteclose}\isanewline
 | 
|  |    814 | \isanewline
 | 
|  |    815 | \ \ \ \ \isacommand{interpretation}\isamarkupfalse%
 | 
|  |    816 | \ list{\isacharunderscore}monoid{\isacharcolon}\ monoid\ {\isacharbrackleft}{\isachardoublequoteopen}op\ {\isacharat}{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}{\isacharbrackright}\ \isakeyword{where}\isanewline
 | 
|  |    817 | \ \ \ \ \ \ {\isachardoublequoteopen}monoid{\isachardot}pow{\isacharunderscore}nat\ {\isacharparenleft}op\ {\isacharat}{\isacharparenright}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ replicate{\isachardoublequoteclose}\isanewline
 | 
|  |    818 | %
 | 
|  |    819 | \isadelimproof
 | 
|  |    820 | \ \ \ \ %
 | 
|  |    821 | \endisadelimproof
 | 
|  |    822 | %
 | 
|  |    823 | \isatagproof
 | 
|  |    824 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    825 | \isanewline
 | 
|  |    826 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    827 | \ n\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
 | 
|  |    828 | \ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
 | 
|  |    829 | \ {\isachardoublequoteopen}monoid{\isachardot}pow{\isacharunderscore}nat\ {\isacharparenleft}op\ {\isacharat}{\isacharparenright}\ {\isacharbrackleft}{\isacharbrackright}\ n\ {\isacharequal}\ replicate\ n{\isachardoublequoteclose}\isanewline
 | 
|  |    830 | \ \ \ \ \ \ \ \ \isacommand{by}\isamarkupfalse%
 | 
|  |    831 | \ {\isacharparenleft}induct\ n{\isacharparenright}\ auto\isanewline
 | 
|  |    832 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    833 | %
 | 
|  |    834 | \endisatagproof
 | 
|  |    835 | {\isafoldproof}%
 | 
|  |    836 | %
 | 
|  |    837 | \isadelimproof
 | 
|  |    838 | %
 | 
|  |    839 | \endisadelimproof
 | 
|  |    840 | %
 | 
| 24991 |    841 | \isamarkupsubsection{Additional subclass relations%
 | 
|  |    842 | }
 | 
|  |    843 | \isamarkuptrue%
 | 
|  |    844 | %
 | 
|  |    845 | \begin{isamarkuptext}%
 | 
|  |    846 | Any \isa{group} is also a \isa{monoid};  this
 | 
| 25247 |    847 |   can be made explicit by claiming an additional
 | 
|  |    848 |   subclass relation,
 | 
| 24991 |    849 |   together with a proof of the logical difference:%
 | 
|  |    850 | \end{isamarkuptext}%
 | 
|  |    851 | \isamarkuptrue%
 | 
|  |    852 | \ \ \ \ \isacommand{subclass}\isamarkupfalse%
 | 
|  |    853 | \ {\isacharparenleft}\isakeyword{in}\ group{\isacharparenright}\ monoid\isanewline
 | 
|  |    854 | %
 | 
|  |    855 | \isadelimproof
 | 
|  |    856 | \ \ \ \ %
 | 
|  |    857 | \endisadelimproof
 | 
|  |    858 | %
 | 
|  |    859 | \isatagproof
 | 
|  |    860 | \isacommand{proof}\isamarkupfalse%
 | 
|  |    861 | \ unfold{\isacharunderscore}locales\isanewline
 | 
|  |    862 | \ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
 | 
|  |    863 | \ x\isanewline
 | 
|  |    864 | \ \ \ \ \ \ \isacommand{from}\isamarkupfalse%
 | 
|  |    865 | \ invl\ \isacommand{have}\isamarkupfalse%
 | 
| 25200 |    866 | \ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
 | 
| 24991 |    867 | \ simp\isanewline
 | 
|  |    868 | \ \ \ \ \ \ \isacommand{with}\isamarkupfalse%
 | 
|  |    869 | \ assoc\ {\isacharbrackleft}symmetric{\isacharbrackright}\ neutl\ invl\ \isacommand{have}\isamarkupfalse%
 | 
| 25200 |    870 | \ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ {\isacharparenleft}x\ {\isasymotimes}\ {\isasymone}{\isacharparenright}\ {\isacharequal}\ x{\isasymdiv}\ {\isasymotimes}\ x{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
 | 
| 24991 |    871 | \ simp\isanewline
 | 
|  |    872 | \ \ \ \ \ \ \isacommand{with}\isamarkupfalse%
 | 
|  |    873 | \ left{\isacharunderscore}cancel\ \isacommand{show}\isamarkupfalse%
 | 
| 25200 |    874 | \ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
 | 
| 24991 |    875 | \ simp\isanewline
 | 
|  |    876 | \ \ \ \ \isacommand{qed}\isamarkupfalse%
 | 
|  |    877 | %
 | 
|  |    878 | \endisatagproof
 | 
|  |    879 | {\isafoldproof}%
 | 
|  |    880 | %
 | 
|  |    881 | \isadelimproof
 | 
|  |    882 | %
 | 
|  |    883 | \endisadelimproof
 | 
|  |    884 | %
 | 
|  |    885 | \begin{isamarkuptext}%
 | 
| 25200 |    886 | \noindent The logical proof is carried out on the locale level
 | 
| 24991 |    887 |   and thus conveniently is opened using the \isa{unfold{\isacharunderscore}locales}
 | 
|  |    888 |   method which only leaves the logical differences still
 | 
| 25200 |    889 |   open to proof to the user.  Afterwards it is propagated
 | 
| 24991 |    890 |   to the type system, making \isa{group} an instance of
 | 
| 25247 |    891 |   \isa{monoid} by adding an additional edge
 | 
|  |    892 |   to the graph of subclass relations
 | 
|  |    893 |   (cf.\ \figref{fig:subclass}).
 | 
|  |    894 | 
 | 
|  |    895 |   \begin{figure}[htbp]
 | 
|  |    896 |    \begin{center}
 | 
|  |    897 |      \small
 | 
|  |    898 |      \unitlength 0.6mm
 | 
|  |    899 |      \begin{picture}(40,60)(0,0)
 | 
|  |    900 |        \put(20,60){\makebox(0,0){\isa{semigroup}}}
 | 
|  |    901 |        \put(20,40){\makebox(0,0){\isa{monoidl}}}
 | 
|  |    902 |        \put(00,20){\makebox(0,0){\isa{monoid}}}
 | 
|  |    903 |        \put(40,00){\makebox(0,0){\isa{group}}}
 | 
|  |    904 |        \put(20,55){\vector(0,-1){10}}
 | 
|  |    905 |        \put(15,35){\vector(-1,-1){10}}
 | 
|  |    906 |        \put(25,35){\vector(1,-3){10}}
 | 
|  |    907 |      \end{picture}
 | 
|  |    908 |      \hspace{8em}
 | 
|  |    909 |      \begin{picture}(40,60)(0,0)
 | 
|  |    910 |        \put(20,60){\makebox(0,0){\isa{semigroup}}}
 | 
|  |    911 |        \put(20,40){\makebox(0,0){\isa{monoidl}}}
 | 
|  |    912 |        \put(00,20){\makebox(0,0){\isa{monoid}}}
 | 
|  |    913 |        \put(40,00){\makebox(0,0){\isa{group}}}
 | 
|  |    914 |        \put(20,55){\vector(0,-1){10}}
 | 
|  |    915 |        \put(15,35){\vector(-1,-1){10}}
 | 
|  |    916 |        \put(05,15){\vector(3,-1){30}}
 | 
|  |    917 |      \end{picture}
 | 
|  |    918 |      \caption{Subclass relationship of monoids and groups:
 | 
|  |    919 |         before and after establishing the relationship
 | 
|  |    920 |         \isa{group\ {\isasymsubseteq}\ monoid};  transitive edges left out.}
 | 
|  |    921 |      \label{fig:subclass}
 | 
|  |    922 |    \end{center}
 | 
|  |    923 |   \end{figure}
 | 
|  |    924 | 
 | 
|  |    925 |   For illustration, a derived definition
 | 
| 24991 |    926 |   in \isa{group} which uses \isa{pow{\isacharunderscore}nat}:%
 | 
|  |    927 | \end{isamarkuptext}%
 | 
|  |    928 | \isamarkuptrue%
 | 
|  |    929 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |    930 | \ {\isacharparenleft}\isakeyword{in}\ group{\isacharparenright}\isanewline
 | 
|  |    931 | \ \ \ \ \ \ pow{\isacharunderscore}int\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}int\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
 | 
|  |    932 | \ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}int\ k\ x\ {\isacharequal}\ {\isacharparenleft}if\ k\ {\isachargreater}{\isacharequal}\ {\isadigit{0}}\isanewline
 | 
|  |    933 | \ \ \ \ \ \ \ \ then\ pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ k{\isacharparenright}\ x\isanewline
 | 
| 25200 |    934 | \ \ \ \ \ \ \ \ else\ {\isacharparenleft}pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ {\isacharparenleft}{\isacharminus}\ k{\isacharparenright}{\isacharparenright}\ x{\isacharparenright}{\isasymdiv}{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 24991 |    935 | \begin{isamarkuptext}%
 | 
| 25247 |    936 | \noindent yields the global definition of
 | 
| 24991 |    937 |   \isa{{\isachardoublequote}pow{\isacharunderscore}int\ {\isasymColon}\ int\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}group\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}group{\isachardoublequote}}
 | 
|  |    938 |   with the corresponding theorem \isa{pow{\isacharunderscore}int\ k\ x\ {\isacharequal}\ {\isacharparenleft}if\ {\isadigit{0}}\ {\isasymle}\ k\ then\ pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ k{\isacharparenright}\ x\ else\ {\isacharparenleft}pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ {\isacharparenleft}{\isacharminus}\ k{\isacharparenright}{\isacharparenright}\ x{\isacharparenright}{\isasymdiv}{\isacharparenright}}.%
 | 
|  |    939 | \end{isamarkuptext}%
 | 
|  |    940 | \isamarkuptrue%
 | 
|  |    941 | %
 | 
| 25868 |    942 | \isamarkupsubsection{A note on syntax%
 | 
|  |    943 | }
 | 
|  |    944 | \isamarkuptrue%
 | 
|  |    945 | %
 | 
|  |    946 | \begin{isamarkuptext}%
 | 
|  |    947 | As a commodity, class context syntax allows to refer
 | 
| 27507 |    948 |   to local class operations and their global counterparts
 | 
| 25868 |    949 |   uniformly;  type inference resolves ambiguities.  For example:%
 | 
|  |    950 | \end{isamarkuptext}%
 | 
|  |    951 | \isamarkuptrue%
 | 
|  |    952 | \isacommand{context}\isamarkupfalse%
 | 
|  |    953 | \ semigroup\isanewline
 | 
|  |    954 | \isakeyword{begin}\isanewline
 | 
|  |    955 | \isanewline
 | 
|  |    956 | \isacommand{term}\isamarkupfalse%
 | 
|  |    957 | \ {\isachardoublequoteopen}x\ {\isasymotimes}\ y{\isachardoublequoteclose}\ %
 | 
|  |    958 | \isamarkupcmt{example 1%
 | 
|  |    959 | }
 | 
|  |    960 | \isanewline
 | 
|  |    961 | \isacommand{term}\isamarkupfalse%
 | 
|  |    962 | \ {\isachardoublequoteopen}{\isacharparenleft}x{\isasymColon}nat{\isacharparenright}\ {\isasymotimes}\ y{\isachardoublequoteclose}\ %
 | 
|  |    963 | \isamarkupcmt{example 2%
 | 
|  |    964 | }
 | 
|  |    965 | \isanewline
 | 
|  |    966 | \isanewline
 | 
|  |    967 | \isacommand{end}\isamarkupfalse%
 | 
|  |    968 | \isanewline
 | 
|  |    969 | \isanewline
 | 
|  |    970 | \isacommand{term}\isamarkupfalse%
 | 
|  |    971 | \ {\isachardoublequoteopen}x\ {\isasymotimes}\ y{\isachardoublequoteclose}\ %
 | 
|  |    972 | \isamarkupcmt{example 3%
 | 
|  |    973 | }
 | 
|  |    974 | %
 | 
|  |    975 | \begin{isamarkuptext}%
 | 
|  |    976 | \noindent Here in example 1, the term refers to the local class operation
 | 
|  |    977 |   \isa{mult\ {\isacharbrackleft}{\isasymalpha}{\isacharbrackright}}, whereas in example 2 the type constraint
 | 
|  |    978 |   enforces the global class operation \isa{mult\ {\isacharbrackleft}nat{\isacharbrackright}}.
 | 
|  |    979 |   In the global context in example 3, the reference is
 | 
|  |    980 |   to the polymorphic global class operation \isa{mult\ {\isacharbrackleft}{\isacharquery}{\isasymalpha}\ {\isasymColon}\ semigroup{\isacharbrackright}}.%
 | 
|  |    981 | \end{isamarkuptext}%
 | 
|  |    982 | \isamarkuptrue%
 | 
|  |    983 | %
 | 
| 25247 |    984 | \isamarkupsection{Type classes and code generation%
 | 
| 20967 |    985 | }
 | 
|  |    986 | \isamarkuptrue%
 | 
|  |    987 | %
 | 
|  |    988 | \begin{isamarkuptext}%
 | 
| 22317 |    989 | Turning back to the first motivation for type classes,
 | 
|  |    990 |   namely overloading, it is obvious that overloading
 | 
| 25533 |    991 |   stemming from \isa{{\isasymCLASS}} statements and
 | 
|  |    992 |   \isa{{\isasymINSTANTIATION}}
 | 
|  |    993 |   targets naturally maps to Haskell type classes.
 | 
| 22317 |    994 |   The code generator framework \cite{isabelle-codegen} 
 | 
|  |    995 |   takes this into account.  Concerning target languages
 | 
|  |    996 |   lacking type classes (e.g.~SML), type classes
 | 
|  |    997 |   are implemented by explicit dictionary construction.
 | 
| 23956 |    998 |   For example, lets go back to the power function:%
 | 
| 20967 |    999 | \end{isamarkuptext}%
 | 
|  |   1000 | \isamarkuptrue%
 | 
|  |   1001 | \ \ \ \ \isacommand{definition}\isamarkupfalse%
 | 
|  |   1002 | \isanewline
 | 
| 22317 |   1003 | \ \ \ \ \ \ example\ {\isacharcolon}{\isacharcolon}\ int\ \isakeyword{where}\isanewline
 | 
| 20967 |   1004 | \ \ \ \ \ \ {\isachardoublequoteopen}example\ {\isacharequal}\ pow{\isacharunderscore}int\ {\isadigit{1}}{\isadigit{0}}\ {\isacharparenleft}{\isacharminus}{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}%
 | 
|  |   1005 | \begin{isamarkuptext}%
 | 
| 22317 |   1006 | \noindent This maps to Haskell as:%
 | 
| 20967 |   1007 | \end{isamarkuptext}%
 | 
|  |   1008 | \isamarkuptrue%
 | 
| 24628 |   1009 | \isacommand{export{\isacharunderscore}code}\isamarkupfalse%
 | 
|  |   1010 | \ example\ \isakeyword{in}\ Haskell\ \isakeyword{module{\isacharunderscore}name}\ Classes\ \isakeyword{file}\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}{\isachardoublequoteclose}%
 | 
| 20967 |   1011 | \begin{isamarkuptext}%
 | 
| 22317 |   1012 | \lsthaskell{Thy/code_examples/Classes.hs}
 | 
|  |   1013 | 
 | 
|  |   1014 |   \noindent The whole code in SML with explicit dictionary passing:%
 | 
|  |   1015 | \end{isamarkuptext}%
 | 
|  |   1016 | \isamarkuptrue%
 | 
| 24628 |   1017 | \isacommand{export{\isacharunderscore}code}\isamarkupfalse%
 | 
|  |   1018 | \ example\ \isakeyword{in}\ SML\ \isakeyword{module{\isacharunderscore}name}\ Classes\ \isakeyword{file}\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}classes{\isachardot}ML{\isachardoublequoteclose}%
 | 
| 22317 |   1019 | \begin{isamarkuptext}%
 | 
|  |   1020 | \lstsml{Thy/code_examples/classes.ML}%
 | 
| 20967 |   1021 | \end{isamarkuptext}%
 | 
|  |   1022 | \isamarkuptrue%
 | 
|  |   1023 | %
 | 
|  |   1024 | \isadelimtheory
 | 
|  |   1025 | %
 | 
|  |   1026 | \endisadelimtheory
 | 
|  |   1027 | %
 | 
|  |   1028 | \isatagtheory
 | 
|  |   1029 | \isacommand{end}\isamarkupfalse%
 | 
|  |   1030 | %
 | 
|  |   1031 | \endisatagtheory
 | 
|  |   1032 | {\isafoldtheory}%
 | 
|  |   1033 | %
 | 
|  |   1034 | \isadelimtheory
 | 
|  |   1035 | %
 | 
|  |   1036 | \endisadelimtheory
 | 
|  |   1037 | \isanewline
 | 
|  |   1038 | \end{isabellebody}%
 | 
|  |   1039 | %%% Local Variables:
 | 
|  |   1040 | %%% mode: latex
 | 
|  |   1041 | %%% TeX-master: "root"
 | 
|  |   1042 | %%% End:
 |