doc-src/Classes/Thy/document/Classes.tex
author wenzelm
Tue, 03 Mar 2009 19:30:43 +0100
changeset 30229 9861257b18e6
parent 30213 doc-src/IsarAdvanced/Classes/Thy/document/Classes.tex@9e245d524997
parent 30210 doc-src/IsarAdvanced/Classes/Thy/document/Classes.tex@853abb4853cc
child 30732 461ee3e49ad3
permissions -rw-r--r--
merged
haftmann@20967
     1
%
haftmann@20967
     2
\begin{isabellebody}%
haftmann@20967
     3
\def\isabellecontext{Classes}%
haftmann@20967
     4
%
haftmann@20967
     5
\isadelimtheory
haftmann@20967
     6
%
haftmann@20967
     7
\endisadelimtheory
haftmann@20967
     8
%
haftmann@20967
     9
\isatagtheory
haftmann@28565
    10
\isacommand{theory}\isamarkupfalse%
haftmann@28565
    11
\ Classes\isanewline
haftmann@28565
    12
\isakeyword{imports}\ Main\ Setup\isanewline
haftmann@28565
    13
\isakeyword{begin}%
haftmann@20967
    14
\endisatagtheory
haftmann@20967
    15
{\isafoldtheory}%
haftmann@20967
    16
%
haftmann@20967
    17
\isadelimtheory
haftmann@20967
    18
%
haftmann@20967
    19
\endisadelimtheory
haftmann@20967
    20
%
haftmann@20967
    21
\isamarkupsection{Introduction%
haftmann@20967
    22
}
haftmann@20967
    23
\isamarkuptrue%
haftmann@20967
    24
%
haftmann@20967
    25
\begin{isamarkuptext}%
haftmann@22317
    26
Type classes were introduces by Wadler and Blott \cite{wadler89how}
haftmann@22317
    27
  into the Haskell language, to allow for a reasonable implementation
haftmann@22317
    28
  of overloading\footnote{throughout this tutorial, we are referring
haftmann@22317
    29
  to classical Haskell 1.0 type classes, not considering
haftmann@22317
    30
  later additions in expressiveness}.
haftmann@22317
    31
  As a canonical example, a polymorphic equality function
haftmann@22317
    32
  \isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} which is overloaded on different
haftmann@22550
    33
  types for \isa{{\isasymalpha}}, which is achieved by splitting introduction
haftmann@22317
    34
  of the \isa{eq} function from its overloaded definitions by means
haftmann@22317
    35
  of \isa{class} and \isa{instance} declarations:
haftmann@30210
    36
  \footnote{syntax here is a kind of isabellized Haskell}
haftmann@20967
    37
haftmann@28565
    38
  \begin{quote}
haftmann@22317
    39
haftmann@30210
    40
  \noindent\isa{class\ eq\ where} \\
haftmann@28565
    41
  \hspace*{2ex}\isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool}
haftmann@22317
    42
haftmann@28565
    43
  \medskip\noindent\isa{instance\ nat\ {\isasymColon}\ eq\ where} \\
haftmann@28565
    44
  \hspace*{2ex}\isa{eq\ {\isadigit{0}}\ {\isadigit{0}}\ {\isacharequal}\ True} \\
haftmann@28565
    45
  \hspace*{2ex}\isa{eq\ {\isadigit{0}}\ {\isacharunderscore}\ {\isacharequal}\ False} \\
haftmann@28565
    46
  \hspace*{2ex}\isa{eq\ {\isacharunderscore}\ {\isadigit{0}}\ {\isacharequal}\ False} \\
haftmann@28565
    47
  \hspace*{2ex}\isa{eq\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharparenleft}Suc\ m{\isacharparenright}\ {\isacharequal}\ eq\ n\ m}
haftmann@22317
    48
haftmann@28948
    49
  \medskip\noindent\isa{instance\ {\isacharparenleft}{\isasymalpha}{\isasymColon}eq{\isacharcomma}\ {\isasymbeta}{\isasymColon}eq{\isacharparenright}\ pair\ {\isasymColon}\ eq\ where} \\
haftmann@28565
    50
  \hspace*{2ex}\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}}}
haftmann@22317
    51
haftmann@28565
    52
  \medskip\noindent\isa{class\ ord\ extends\ eq\ where} \\
haftmann@28565
    53
  \hspace*{2ex}\isa{less{\isacharunderscore}eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} \\
haftmann@28565
    54
  \hspace*{2ex}\isa{less\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool}
haftmann@28565
    55
haftmann@28565
    56
  \end{quote}
haftmann@28565
    57
haftmann@28565
    58
  \noindent Type variables are annotated with (finitely many) classes;
haftmann@22317
    59
  these annotations are assertions that a particular polymorphic type
haftmann@22317
    60
  provides definitions for overloaded functions.
haftmann@22317
    61
haftmann@22317
    62
  Indeed, type classes not only allow for simple overloading
haftmann@22317
    63
  but form a generic calculus, an instance of order-sorted
haftmann@23956
    64
  algebra \cite{Nipkow-Prehofer:1993,nipkow-sorts93,Wenzel:1997:TPHOL}.
haftmann@22317
    65
haftmann@28540
    66
  From a software engeneering point of view, type classes
haftmann@28540
    67
  roughly correspond to interfaces in object-oriented languages like Java;
haftmann@22317
    68
  so, it is naturally desirable that type classes do not only
haftmann@24991
    69
  provide functions (class parameters) but also state specifications
haftmann@22317
    70
  implementations must obey.  For example, the \isa{class\ eq}
haftmann@22550
    71
  above could be given the following specification, demanding that
haftmann@22550
    72
  \isa{class\ eq} is an equivalence relation obeying reflexivity,
haftmann@22550
    73
  symmetry and transitivity:
haftmann@22317
    74
haftmann@28565
    75
  \begin{quote}
haftmann@22317
    76
haftmann@28565
    77
  \noindent\isa{class\ eq\ where} \\
haftmann@28565
    78
  \hspace*{2ex}\isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} \\
haftmann@28565
    79
  \isa{satisfying} \\
haftmann@28565
    80
  \hspace*{2ex}\isa{refl{\isacharcolon}\ eq\ x\ x} \\
haftmann@28565
    81
  \hspace*{2ex}\isa{sym{\isacharcolon}\ eq\ x\ y\ {\isasymlongleftrightarrow}\ eq\ x\ y} \\
haftmann@28565
    82
  \hspace*{2ex}\isa{trans{\isacharcolon}\ eq\ x\ y\ {\isasymand}\ eq\ y\ z\ {\isasymlongrightarrow}\ eq\ x\ z}
haftmann@28565
    83
haftmann@28565
    84
  \end{quote}
haftmann@28565
    85
haftmann@28565
    86
  \noindent From a theoretic point of view, type classes are lightweight
haftmann@22479
    87
  modules; Haskell type classes may be emulated by
haftmann@22317
    88
  SML functors \cite{classes_modules}. 
haftmann@22317
    89
  Isabelle/Isar offers a discipline of type classes which brings
haftmann@22317
    90
  all those aspects together:
haftmann@22317
    91
haftmann@22317
    92
  \begin{enumerate}
haftmann@25004
    93
    \item specifying abstract parameters together with
haftmann@22317
    94
       corresponding specifications,
haftmann@28540
    95
    \item instantiating those abstract parameters by a particular
haftmann@22317
    96
       type
haftmann@22317
    97
    \item in connection with a ``less ad-hoc'' approach to overloading,
haftmann@23956
    98
    \item with a direct link to the Isabelle module system
haftmann@23956
    99
      (aka locales \cite{kammueller-locales}).
haftmann@22317
   100
  \end{enumerate}
haftmann@22317
   101
haftmann@22550
   102
  \noindent Isar type classes also directly support code generation
haftmann@22550
   103
  in a Haskell like fashion.
haftmann@22317
   104
haftmann@22317
   105
  This tutorial demonstrates common elements of structured specifications
haftmann@22317
   106
  and abstract reasoning with type classes by the algebraic hierarchy of
haftmann@22317
   107
  semigroups, monoids and groups.  Our background theory is that of
haftmann@23956
   108
  Isabelle/HOL \cite{isa-tutorial}, for which some
haftmann@22317
   109
  familiarity is assumed.
haftmann@22317
   110
haftmann@22317
   111
  Here we merely present the look-and-feel for end users.
haftmann@22317
   112
  Internally, those are mapped to more primitive Isabelle concepts.
haftmann@23956
   113
  See \cite{Haftmann-Wenzel:2006:classes} for more detail.%
haftmann@20967
   114
\end{isamarkuptext}%
haftmann@20967
   115
\isamarkuptrue%
haftmann@20967
   116
%
haftmann@20967
   117
\isamarkupsection{A simple algebra example \label{sec:example}%
haftmann@20967
   118
}
haftmann@20967
   119
\isamarkuptrue%
haftmann@20967
   120
%
haftmann@20967
   121
\isamarkupsubsection{Class definition%
haftmann@20967
   122
}
haftmann@20967
   123
\isamarkuptrue%
haftmann@20967
   124
%
haftmann@20967
   125
\begin{isamarkuptext}%
haftmann@28565
   126
Depending on an arbitrary type \isa{{\isasymalpha}}, class \isa{semigroup} introduces a binary operator \isa{{\isacharparenleft}{\isasymotimes}{\isacharparenright}} that is
haftmann@20967
   127
  assumed to be associative:%
haftmann@20967
   128
\end{isamarkuptext}%
haftmann@20967
   129
\isamarkuptrue%
haftmann@28565
   130
%
haftmann@28565
   131
\isadelimquote
haftmann@28565
   132
%
haftmann@28565
   133
\endisadelimquote
haftmann@28565
   134
%
haftmann@28565
   135
\isatagquote
haftmann@28565
   136
\isacommand{class}\isamarkupfalse%
haftmann@29705
   137
\ semigroup\ {\isacharequal}\isanewline
haftmann@28565
   138
\ \ \isakeyword{fixes}\ mult\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \ \ \ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}{\isasymotimes}{\isachardoublequoteclose}\ {\isadigit{7}}{\isadigit{0}}{\isacharparenright}\isanewline
haftmann@28565
   139
\ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}%
haftmann@28565
   140
\endisatagquote
haftmann@28565
   141
{\isafoldquote}%
haftmann@28565
   142
%
haftmann@28565
   143
\isadelimquote
haftmann@28565
   144
%
haftmann@28565
   145
\endisadelimquote
haftmann@28565
   146
%
haftmann@20967
   147
\begin{isamarkuptext}%
haftmann@28565
   148
\noindent This \hyperlink{command.class}{\mbox{\isa{\isacommand{class}}}} specification consists of two
haftmann@28565
   149
  parts: the \qn{operational} part names the class parameter
haftmann@28565
   150
  (\hyperlink{element.fixes}{\mbox{\isa{\isakeyword{fixes}}}}), the \qn{logical} part specifies properties on them
haftmann@28565
   151
  (\hyperlink{element.assumes}{\mbox{\isa{\isakeyword{assumes}}}}).  The local \hyperlink{element.fixes}{\mbox{\isa{\isakeyword{fixes}}}} and
haftmann@28565
   152
  \hyperlink{element.assumes}{\mbox{\isa{\isakeyword{assumes}}}} are lifted to the theory toplevel,
haftmann@28565
   153
  yielding the global
haftmann@24991
   154
  parameter \isa{{\isachardoublequote}mult\ {\isasymColon}\ {\isasymalpha}{\isasymColon}semigroup\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequote}} and the
haftmann@28565
   155
  global theorem \hyperlink{fact.semigroup.assoc:}{\mbox{\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}}.%
haftmann@20967
   156
\end{isamarkuptext}%
haftmann@20967
   157
\isamarkuptrue%
haftmann@20967
   158
%
haftmann@20967
   159
\isamarkupsubsection{Class instantiation \label{sec:class_inst}%
haftmann@20967
   160
}
haftmann@20967
   161
\isamarkuptrue%
haftmann@20967
   162
%
haftmann@20967
   163
\begin{isamarkuptext}%
haftmann@20967
   164
The concrete type \isa{int} is made a \isa{semigroup}
haftmann@24991
   165
  instance by providing a suitable definition for the class parameter
haftmann@28565
   166
  \isa{{\isacharparenleft}{\isasymotimes}{\isacharparenright}} and a proof for the specification of \hyperlink{fact.assoc}{\mbox{\isa{assoc}}}.
haftmann@28565
   167
  This is accomplished by the \hyperlink{command.instantiation}{\mbox{\isa{\isacommand{instantiation}}}} target:%
haftmann@20967
   168
\end{isamarkuptext}%
haftmann@20967
   169
\isamarkuptrue%
haftmann@28565
   170
%
haftmann@28565
   171
\isadelimquote
haftmann@28565
   172
%
haftmann@28565
   173
\endisadelimquote
haftmann@28565
   174
%
haftmann@28565
   175
\isatagquote
haftmann@28565
   176
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   177
\ int\ {\isacharcolon}{\isacharcolon}\ semigroup\isanewline
haftmann@28565
   178
\isakeyword{begin}\isanewline
haftmann@25533
   179
\isanewline
haftmann@28565
   180
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   181
\isanewline
haftmann@28565
   182
\ \ mult{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}i\ {\isasymotimes}\ j\ {\isacharequal}\ i\ {\isacharplus}\ {\isacharparenleft}j{\isasymColon}int{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   183
\isanewline
haftmann@28565
   184
\isacommand{instance}\isamarkupfalse%
haftmann@28565
   185
\ \isacommand{proof}\isamarkupfalse%
haftmann@20967
   186
\isanewline
haftmann@28565
   187
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@20967
   188
\ i\ j\ k\ {\isacharcolon}{\isacharcolon}\ int\ \isacommand{have}\isamarkupfalse%
haftmann@20967
   189
\ {\isachardoublequoteopen}{\isacharparenleft}i\ {\isacharplus}\ j{\isacharparenright}\ {\isacharplus}\ k\ {\isacharequal}\ i\ {\isacharplus}\ {\isacharparenleft}j\ {\isacharplus}\ k{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   190
\ simp\isanewline
haftmann@28565
   191
\ \ \isacommand{then}\isamarkupfalse%
haftmann@20967
   192
\ \isacommand{show}\isamarkupfalse%
haftmann@25247
   193
\ {\isachardoublequoteopen}{\isacharparenleft}i\ {\isasymotimes}\ j{\isacharparenright}\ {\isasymotimes}\ k\ {\isacharequal}\ i\ {\isasymotimes}\ {\isacharparenleft}j\ {\isasymotimes}\ k{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28566
   194
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@20967
   195
\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{{\isachardot}}\isamarkupfalse%
haftmann@20967
   196
\isanewline
haftmann@28565
   197
\isacommand{qed}\isamarkupfalse%
haftmann@28565
   198
\isanewline
haftmann@28565
   199
\isanewline
haftmann@28565
   200
\isacommand{end}\isamarkupfalse%
haftmann@20967
   201
%
haftmann@28565
   202
\endisatagquote
haftmann@28565
   203
{\isafoldquote}%
haftmann@20967
   204
%
haftmann@28565
   205
\isadelimquote
haftmann@20967
   206
%
haftmann@28565
   207
\endisadelimquote
haftmann@20967
   208
%
haftmann@20967
   209
\begin{isamarkuptext}%
haftmann@28565
   210
\noindent \hyperlink{command.instantiation}{\mbox{\isa{\isacommand{instantiation}}}} allows to define class parameters
haftmann@25533
   211
  at a particular instance using common specification tools (here,
haftmann@28565
   212
  \hyperlink{command.definition}{\mbox{\isa{\isacommand{definition}}}}).  The concluding \hyperlink{command.instance}{\mbox{\isa{\isacommand{instance}}}}
haftmann@25533
   213
  opens a proof that the given parameters actually conform
haftmann@25533
   214
  to the class specification.  Note that the first proof step
haftmann@28565
   215
  is the \hyperlink{method.default}{\mbox{\isa{default}}} method,
haftmann@28565
   216
  which for such instance proofs maps to the \hyperlink{method.intro-classes}{\mbox{\isa{intro{\isacharunderscore}classes}}} method.
haftmann@25533
   217
  This boils down an instance judgement to the relevant primitive
haftmann@22317
   218
  proof goals and should conveniently always be the first method applied
haftmann@22317
   219
  in an instantiation proof.
haftmann@22317
   220
haftmann@25533
   221
  From now on, the type-checker will consider \isa{int}
haftmann@25533
   222
  as a \isa{semigroup} automatically, i.e.\ any general results
haftmann@25533
   223
  are immediately available on concrete instances.
haftmann@28565
   224
haftmann@22550
   225
  \medskip Another instance of \isa{semigroup} are the natural numbers:%
haftmann@20967
   226
\end{isamarkuptext}%
haftmann@20967
   227
\isamarkuptrue%
haftmann@28565
   228
%
haftmann@28565
   229
\isadelimquote
haftmann@28565
   230
%
haftmann@28565
   231
\endisadelimquote
haftmann@28565
   232
%
haftmann@28565
   233
\isatagquote
haftmann@28565
   234
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   235
\ nat\ {\isacharcolon}{\isacharcolon}\ semigroup\isanewline
haftmann@28565
   236
\isakeyword{begin}\isanewline
haftmann@25533
   237
\isanewline
haftmann@28565
   238
\isacommand{primrec}\isamarkupfalse%
haftmann@25871
   239
\ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline
haftmann@28565
   240
\ \ {\isachardoublequoteopen}{\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann@28565
   241
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ Suc\ {\isacharparenleft}m\ {\isasymotimes}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   242
\isanewline
haftmann@28565
   243
\isacommand{instance}\isamarkupfalse%
haftmann@28565
   244
\ \isacommand{proof}\isamarkupfalse%
haftmann@28565
   245
\isanewline
haftmann@28565
   246
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@28565
   247
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\ \isanewline
haftmann@28565
   248
\ \ \isacommand{show}\isamarkupfalse%
haftmann@28565
   249
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28565
   250
\ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann@28565
   251
\ {\isacharparenleft}induct\ m{\isacharparenright}\ auto\isanewline
haftmann@28565
   252
\isacommand{qed}\isamarkupfalse%
haftmann@28565
   253
\isanewline
haftmann@28565
   254
\isanewline
haftmann@28565
   255
\isacommand{end}\isamarkupfalse%
haftmann@20967
   256
%
haftmann@28565
   257
\endisatagquote
haftmann@28565
   258
{\isafoldquote}%
haftmann@20967
   259
%
haftmann@28565
   260
\isadelimquote
haftmann@20967
   261
%
haftmann@28565
   262
\endisadelimquote
haftmann@20967
   263
%
haftmann@25871
   264
\begin{isamarkuptext}%
haftmann@25871
   265
\noindent Note the occurence of the name \isa{mult{\isacharunderscore}nat}
haftmann@25871
   266
  in the primrec declaration;  by default, the local name of
haftmann@25871
   267
  a class operation \isa{f} to instantiate on type constructor
haftmann@25871
   268
  \isa{{\isasymkappa}} are mangled as \isa{f{\isacharunderscore}{\isasymkappa}}.  In case of uncertainty,
haftmann@28565
   269
  these names may be inspected using the \hyperlink{command.print-context}{\mbox{\isa{\isacommand{print{\isacharunderscore}context}}}} command
haftmann@25871
   270
  or the corresponding ProofGeneral button.%
haftmann@25871
   271
\end{isamarkuptext}%
haftmann@25871
   272
\isamarkuptrue%
haftmann@25871
   273
%
haftmann@25247
   274
\isamarkupsubsection{Lifting and parametric types%
haftmann@25247
   275
}
haftmann@25247
   276
\isamarkuptrue%
haftmann@25247
   277
%
haftmann@20967
   278
\begin{isamarkuptext}%
haftmann@25247
   279
Overloaded definitions giving on class instantiation
haftmann@25247
   280
  may include recursion over the syntactic structure of types.
haftmann@25247
   281
  As a canonical example, we model product semigroups
haftmann@25247
   282
  using our simple algebra:%
haftmann@20967
   283
\end{isamarkuptext}%
haftmann@20967
   284
\isamarkuptrue%
haftmann@28565
   285
%
haftmann@28565
   286
\isadelimquote
haftmann@28565
   287
%
haftmann@28565
   288
\endisadelimquote
haftmann@28565
   289
%
haftmann@28565
   290
\isatagquote
haftmann@28565
   291
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   292
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}semigroup{\isacharcomma}\ semigroup{\isacharparenright}\ semigroup\isanewline
haftmann@28565
   293
\isakeyword{begin}\isanewline
haftmann@25533
   294
\isanewline
haftmann@28565
   295
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   296
\isanewline
haftmann@28565
   297
\ \ 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
haftmann@25533
   298
\isanewline
haftmann@28565
   299
\isacommand{instance}\isamarkupfalse%
haftmann@28565
   300
\ \isacommand{proof}\isamarkupfalse%
haftmann@20967
   301
\isanewline
haftmann@28565
   302
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@25533
   303
\ p\isactrlisub {\isadigit{1}}\ p\isactrlisub {\isadigit{2}}\ p\isactrlisub {\isadigit{3}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isasymColon}semigroup\ {\isasymtimes}\ {\isasymbeta}{\isasymColon}semigroup{\isachardoublequoteclose}\isanewline
haftmann@28565
   304
\ \ \isacommand{show}\isamarkupfalse%
haftmann@25247
   305
\ {\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
haftmann@28566
   306
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@25247
   307
\ mult{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@25247
   308
\ {\isacharparenleft}simp\ add{\isacharcolon}\ assoc{\isacharparenright}\isanewline
haftmann@28565
   309
\isacommand{qed}\isamarkupfalse%
haftmann@25533
   310
\ \ \ \ \ \ \isanewline
haftmann@25533
   311
\isanewline
haftmann@28565
   312
\isacommand{end}\isamarkupfalse%
haftmann@28565
   313
%
haftmann@28565
   314
\endisatagquote
haftmann@28565
   315
{\isafoldquote}%
haftmann@28565
   316
%
haftmann@28565
   317
\isadelimquote
haftmann@28565
   318
%
haftmann@28565
   319
\endisadelimquote
haftmann@20967
   320
%
haftmann@25247
   321
\begin{isamarkuptext}%
haftmann@25247
   322
\noindent Associativity from product semigroups is
haftmann@25247
   323
  established using
haftmann@28565
   324
  the definition of \isa{{\isacharparenleft}{\isasymotimes}{\isacharparenright}} on products and the hypothetical
wenzelm@27507
   325
  associativity of the type components;  these hypotheses
haftmann@25247
   326
  are facts due to the \isa{semigroup} constraints imposed
haftmann@28565
   327
  on the type components by the \hyperlink{command.instance}{\mbox{\isa{\isacommand{instance}}}} proposition.
haftmann@25247
   328
  Indeed, this pattern often occurs with parametric types
haftmann@25247
   329
  and type classes.%
haftmann@25247
   330
\end{isamarkuptext}%
haftmann@25247
   331
\isamarkuptrue%
haftmann@25247
   332
%
haftmann@25247
   333
\isamarkupsubsection{Subclassing%
haftmann@20967
   334
}
haftmann@20967
   335
\isamarkuptrue%
haftmann@20967
   336
%
haftmann@20967
   337
\begin{isamarkuptext}%
haftmann@22317
   338
We define a subclass \isa{monoidl} (a semigroup with a left-hand neutral)
haftmann@20967
   339
  by extending \isa{semigroup}
haftmann@24991
   340
  with one additional parameter \isa{neutral} together
haftmann@20967
   341
  with its property:%
haftmann@20967
   342
\end{isamarkuptext}%
haftmann@20967
   343
\isamarkuptrue%
haftmann@28565
   344
%
haftmann@28565
   345
\isadelimquote
haftmann@28565
   346
%
haftmann@28565
   347
\endisadelimquote
haftmann@28565
   348
%
haftmann@28565
   349
\isatagquote
haftmann@28565
   350
\isacommand{class}\isamarkupfalse%
haftmann@20967
   351
\ monoidl\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline
haftmann@28565
   352
\ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isachardoublequoteclose}\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline
haftmann@28565
   353
\ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}%
haftmann@28565
   354
\endisatagquote
haftmann@28565
   355
{\isafoldquote}%
haftmann@28565
   356
%
haftmann@28565
   357
\isadelimquote
haftmann@28565
   358
%
haftmann@28565
   359
\endisadelimquote
haftmann@28565
   360
%
haftmann@20967
   361
\begin{isamarkuptext}%
haftmann@25247
   362
\noindent Again, we prove some instances, by
haftmann@24991
   363
  providing suitable parameter definitions and proofs for the
wenzelm@27507
   364
  additional specifications.  Observe that instantiations
haftmann@25533
   365
  for types with the same arity may be simultaneous:%
haftmann@20967
   366
\end{isamarkuptext}%
haftmann@20967
   367
\isamarkuptrue%
haftmann@28566
   368
%
haftmann@28566
   369
\isadelimquote
haftmann@28566
   370
%
haftmann@28566
   371
\endisadelimquote
haftmann@28566
   372
%
haftmann@28566
   373
\isatagquote
haftmann@28566
   374
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   375
\ nat\ \isakeyword{and}\ int\ {\isacharcolon}{\isacharcolon}\ monoidl\isanewline
haftmann@28566
   376
\isakeyword{begin}\isanewline
haftmann@25533
   377
\isanewline
haftmann@28566
   378
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   379
\isanewline
haftmann@28566
   380
\ \ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   381
\isanewline
haftmann@28566
   382
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   383
\isanewline
haftmann@28566
   384
\ \ neutral{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}int{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   385
\isanewline
haftmann@28566
   386
\isacommand{instance}\isamarkupfalse%
haftmann@28566
   387
\ \isacommand{proof}\isamarkupfalse%
haftmann@20967
   388
\isanewline
haftmann@28566
   389
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@20967
   390
\ n\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
haftmann@28566
   391
\ \ \isacommand{show}\isamarkupfalse%
haftmann@25247
   392
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann@28566
   393
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
wenzelm@26991
   394
\ neutral{\isacharunderscore}nat{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   395
\ simp\isanewline
haftmann@28566
   396
\isacommand{next}\isamarkupfalse%
haftmann@20967
   397
\isanewline
haftmann@28566
   398
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@20967
   399
\ k\ {\isacharcolon}{\isacharcolon}\ int\isanewline
haftmann@28566
   400
\ \ \isacommand{show}\isamarkupfalse%
haftmann@25247
   401
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ k\ {\isacharequal}\ k{\isachardoublequoteclose}\isanewline
haftmann@28566
   402
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@20967
   403
\ neutral{\isacharunderscore}int{\isacharunderscore}def\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   404
\ simp\isanewline
haftmann@28566
   405
\isacommand{qed}\isamarkupfalse%
haftmann@20967
   406
\isanewline
haftmann@25533
   407
\isanewline
haftmann@28566
   408
\isacommand{end}\isamarkupfalse%
haftmann@25533
   409
\isanewline
haftmann@25533
   410
\isanewline
haftmann@28566
   411
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   412
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}monoidl{\isacharcomma}\ monoidl{\isacharparenright}\ monoidl\isanewline
haftmann@28566
   413
\isakeyword{begin}\isanewline
haftmann@25533
   414
\isanewline
haftmann@28566
   415
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   416
\isanewline
haftmann@28566
   417
\ \ neutral{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ {\isacharparenleft}{\isasymone}{\isacharcomma}\ {\isasymone}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   418
\isanewline
haftmann@28566
   419
\isacommand{instance}\isamarkupfalse%
haftmann@28566
   420
\ \isacommand{proof}\isamarkupfalse%
haftmann@20967
   421
\isanewline
haftmann@28566
   422
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@25533
   423
\ p\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isasymColon}monoidl\ {\isasymtimes}\ {\isasymbeta}{\isasymColon}monoidl{\isachardoublequoteclose}\isanewline
haftmann@28566
   424
\ \ \isacommand{show}\isamarkupfalse%
haftmann@25247
   425
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
haftmann@28566
   426
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@25247
   427
\ neutral{\isacharunderscore}prod{\isacharunderscore}def\ mult{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@25247
   428
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutl{\isacharparenright}\isanewline
haftmann@28566
   429
\isacommand{qed}\isamarkupfalse%
haftmann@28566
   430
\isanewline
haftmann@28566
   431
\isanewline
haftmann@28566
   432
\isacommand{end}\isamarkupfalse%
haftmann@20967
   433
%
haftmann@28566
   434
\endisatagquote
haftmann@28566
   435
{\isafoldquote}%
haftmann@20967
   436
%
haftmann@28566
   437
\isadelimquote
haftmann@20967
   438
%
haftmann@28566
   439
\endisadelimquote
haftmann@20967
   440
%
haftmann@20967
   441
\begin{isamarkuptext}%
haftmann@22317
   442
\noindent Fully-fledged monoids are modelled by another subclass
haftmann@24991
   443
  which does not add new parameters but tightens the specification:%
haftmann@20967
   444
\end{isamarkuptext}%
haftmann@20967
   445
\isamarkuptrue%
haftmann@28566
   446
%
haftmann@28566
   447
\isadelimquote
haftmann@28566
   448
%
haftmann@28566
   449
\endisadelimquote
haftmann@28566
   450
%
haftmann@28566
   451
\isatagquote
haftmann@28566
   452
\isacommand{class}\isamarkupfalse%
haftmann@20967
   453
\ monoid\ {\isacharequal}\ monoidl\ {\isacharplus}\isanewline
haftmann@28566
   454
\ \ \isakeyword{assumes}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
haftmann@25247
   455
\isanewline
haftmann@28566
   456
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   457
\ nat\ \isakeyword{and}\ int\ {\isacharcolon}{\isacharcolon}\ monoid\ \isanewline
haftmann@28566
   458
\isakeyword{begin}\isanewline
haftmann@25533
   459
\isanewline
haftmann@28566
   460
\isacommand{instance}\isamarkupfalse%
haftmann@28566
   461
\ \isacommand{proof}\isamarkupfalse%
haftmann@20967
   462
\isanewline
haftmann@28566
   463
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@20967
   464
\ n\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
haftmann@28566
   465
\ \ \isacommand{show}\isamarkupfalse%
haftmann@25247
   466
\ {\isachardoublequoteopen}n\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann@28566
   467
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@25871
   468
\ neutral{\isacharunderscore}nat{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@25871
   469
\ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
haftmann@28566
   470
\isacommand{next}\isamarkupfalse%
haftmann@20967
   471
\isanewline
haftmann@28566
   472
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@20967
   473
\ k\ {\isacharcolon}{\isacharcolon}\ int\isanewline
haftmann@28566
   474
\ \ \isacommand{show}\isamarkupfalse%
haftmann@25247
   475
\ {\isachardoublequoteopen}k\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ k{\isachardoublequoteclose}\isanewline
haftmann@28566
   476
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@20967
   477
\ neutral{\isacharunderscore}int{\isacharunderscore}def\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   478
\ simp\isanewline
haftmann@28566
   479
\isacommand{qed}\isamarkupfalse%
haftmann@25247
   480
\isanewline
haftmann@25533
   481
\isanewline
haftmann@28566
   482
\isacommand{end}\isamarkupfalse%
haftmann@25533
   483
\isanewline
haftmann@25533
   484
\isanewline
haftmann@28566
   485
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   486
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}monoid{\isacharcomma}\ monoid{\isacharparenright}\ monoid\isanewline
haftmann@28566
   487
\isakeyword{begin}\isanewline
haftmann@25533
   488
\isanewline
haftmann@28566
   489
\isacommand{instance}\isamarkupfalse%
haftmann@28566
   490
\ \isacommand{proof}\isamarkupfalse%
haftmann@25247
   491
\ \isanewline
haftmann@28566
   492
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@25533
   493
\ p\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isasymColon}monoid\ {\isasymtimes}\ {\isasymbeta}{\isasymColon}monoid{\isachardoublequoteclose}\isanewline
haftmann@28566
   494
\ \ \isacommand{show}\isamarkupfalse%
haftmann@25247
   495
\ {\isachardoublequoteopen}p\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
haftmann@28566
   496
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@25247
   497
\ neutral{\isacharunderscore}prod{\isacharunderscore}def\ mult{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@25247
   498
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutr{\isacharparenright}\isanewline
haftmann@28566
   499
\isacommand{qed}\isamarkupfalse%
haftmann@28566
   500
\isanewline
haftmann@28566
   501
\isanewline
haftmann@28566
   502
\isacommand{end}\isamarkupfalse%
haftmann@20967
   503
%
haftmann@28566
   504
\endisatagquote
haftmann@28566
   505
{\isafoldquote}%
haftmann@20967
   506
%
haftmann@28566
   507
\isadelimquote
haftmann@20967
   508
%
haftmann@28566
   509
\endisadelimquote
haftmann@22317
   510
%
haftmann@22317
   511
\begin{isamarkuptext}%
haftmann@22317
   512
\noindent To finish our small algebra example, we add a \isa{group} class
haftmann@22317
   513
  with a corresponding instance:%
haftmann@22317
   514
\end{isamarkuptext}%
haftmann@22317
   515
\isamarkuptrue%
haftmann@28566
   516
%
haftmann@28566
   517
\isadelimquote
haftmann@28566
   518
%
haftmann@28566
   519
\endisadelimquote
haftmann@28566
   520
%
haftmann@28566
   521
\isatagquote
haftmann@28566
   522
\isacommand{class}\isamarkupfalse%
haftmann@20967
   523
\ group\ {\isacharequal}\ monoidl\ {\isacharplus}\isanewline
haftmann@28566
   524
\ \ \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
haftmann@28566
   525
\ \ \isakeyword{assumes}\ invl{\isacharcolon}\ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
haftmann@20967
   526
\isanewline
haftmann@28566
   527
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   528
\ int\ {\isacharcolon}{\isacharcolon}\ group\isanewline
haftmann@28566
   529
\isakeyword{begin}\isanewline
haftmann@25533
   530
\isanewline
haftmann@28566
   531
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   532
\isanewline
haftmann@28566
   533
\ \ inverse{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}i{\isasymdiv}\ {\isacharequal}\ {\isacharminus}\ {\isacharparenleft}i{\isasymColon}int{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   534
\isanewline
haftmann@28566
   535
\isacommand{instance}\isamarkupfalse%
haftmann@28566
   536
\ \isacommand{proof}\isamarkupfalse%
haftmann@20967
   537
\isanewline
haftmann@28566
   538
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@20967
   539
\ i\ {\isacharcolon}{\isacharcolon}\ int\isanewline
haftmann@28566
   540
\ \ \isacommand{have}\isamarkupfalse%
haftmann@20967
   541
\ {\isachardoublequoteopen}{\isacharminus}i\ {\isacharplus}\ i\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   542
\ simp\isanewline
haftmann@28566
   543
\ \ \isacommand{then}\isamarkupfalse%
haftmann@20967
   544
\ \isacommand{show}\isamarkupfalse%
haftmann@22550
   545
\ {\isachardoublequoteopen}i{\isasymdiv}\ {\isasymotimes}\ i\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
haftmann@28566
   546
\ \ \ \ \isacommand{unfolding}\isamarkupfalse%
haftmann@25247
   547
\ mult{\isacharunderscore}int{\isacharunderscore}def\ neutral{\isacharunderscore}int{\isacharunderscore}def\ inverse{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{{\isachardot}}\isamarkupfalse%
haftmann@20967
   548
\isanewline
haftmann@28566
   549
\isacommand{qed}\isamarkupfalse%
haftmann@28566
   550
\isanewline
haftmann@28566
   551
\isanewline
haftmann@28566
   552
\isacommand{end}\isamarkupfalse%
haftmann@20967
   553
%
haftmann@28566
   554
\endisatagquote
haftmann@28566
   555
{\isafoldquote}%
haftmann@20967
   556
%
haftmann@28566
   557
\isadelimquote
haftmann@20967
   558
%
haftmann@28566
   559
\endisadelimquote
haftmann@20967
   560
%
haftmann@22317
   561
\isamarkupsection{Type classes as locales%
haftmann@22317
   562
}
haftmann@22317
   563
\isamarkuptrue%
haftmann@22317
   564
%
haftmann@22317
   565
\isamarkupsubsection{A look behind the scene%
haftmann@22317
   566
}
haftmann@22317
   567
\isamarkuptrue%
haftmann@22317
   568
%
haftmann@22317
   569
\begin{isamarkuptext}%
haftmann@22479
   570
The example above gives an impression how Isar type classes work
haftmann@22479
   571
  in practice.  As stated in the introduction, classes also provide
haftmann@22479
   572
  a link to Isar's locale system.  Indeed, the logical core of a class
haftmann@22479
   573
  is nothing else than a locale:%
haftmann@22479
   574
\end{isamarkuptext}%
haftmann@22479
   575
\isamarkuptrue%
haftmann@28566
   576
%
haftmann@28566
   577
\isadelimquote
haftmann@28566
   578
%
haftmann@28566
   579
\endisadelimquote
haftmann@28566
   580
%
haftmann@28566
   581
\isatagquote
haftmann@22479
   582
\isacommand{class}\isamarkupfalse%
haftmann@29705
   583
\ idem\ {\isacharequal}\isanewline
haftmann@22479
   584
\ \ \isakeyword{fixes}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\isanewline
haftmann@22479
   585
\ \ \isakeyword{assumes}\ idem{\isacharcolon}\ {\isachardoublequoteopen}f\ {\isacharparenleft}f\ x{\isacharparenright}\ {\isacharequal}\ f\ x{\isachardoublequoteclose}%
haftmann@28566
   586
\endisatagquote
haftmann@28566
   587
{\isafoldquote}%
haftmann@28566
   588
%
haftmann@28566
   589
\isadelimquote
haftmann@28566
   590
%
haftmann@28566
   591
\endisadelimquote
haftmann@28566
   592
%
haftmann@22479
   593
\begin{isamarkuptext}%
haftmann@22479
   594
\noindent essentially introduces the locale%
haftmann@22479
   595
\end{isamarkuptext}%
haftmann@22479
   596
\isamarkuptrue%
haftmann@30210
   597
\ %
haftmann@30210
   598
\isadeliminvisible
haftmann@22317
   599
%
haftmann@28565
   600
\endisadeliminvisible
haftmann@22479
   601
%
haftmann@28565
   602
\isataginvisible
haftmann@30210
   603
%
haftmann@28565
   604
\endisataginvisible
haftmann@28565
   605
{\isafoldinvisible}%
haftmann@22479
   606
%
haftmann@28565
   607
\isadeliminvisible
haftmann@22479
   608
%
haftmann@28565
   609
\endisadeliminvisible
haftmann@28566
   610
%
haftmann@28566
   611
\isadelimquote
haftmann@28566
   612
%
haftmann@28566
   613
\endisadelimquote
haftmann@28566
   614
%
haftmann@28566
   615
\isatagquote
haftmann@22479
   616
\isacommand{locale}\isamarkupfalse%
haftmann@22479
   617
\ idem\ {\isacharequal}\isanewline
haftmann@22479
   618
\ \ \isakeyword{fixes}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\isanewline
haftmann@22479
   619
\ \ \isakeyword{assumes}\ idem{\isacharcolon}\ {\isachardoublequoteopen}f\ {\isacharparenleft}f\ x{\isacharparenright}\ {\isacharequal}\ f\ x{\isachardoublequoteclose}%
haftmann@28566
   620
\endisatagquote
haftmann@28566
   621
{\isafoldquote}%
haftmann@28566
   622
%
haftmann@28566
   623
\isadelimquote
haftmann@28566
   624
%
haftmann@28566
   625
\endisadelimquote
haftmann@28566
   626
%
haftmann@22479
   627
\begin{isamarkuptext}%
haftmann@22550
   628
\noindent together with corresponding constant(s):%
haftmann@22479
   629
\end{isamarkuptext}%
haftmann@22479
   630
\isamarkuptrue%
haftmann@28566
   631
%
haftmann@28566
   632
\isadelimquote
haftmann@28566
   633
%
haftmann@28566
   634
\endisadelimquote
haftmann@28566
   635
%
haftmann@28566
   636
\isatagquote
haftmann@22479
   637
\isacommand{consts}\isamarkupfalse%
haftmann@22550
   638
\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}%
haftmann@28566
   639
\endisatagquote
haftmann@28566
   640
{\isafoldquote}%
haftmann@28566
   641
%
haftmann@28566
   642
\isadelimquote
haftmann@28566
   643
%
haftmann@28566
   644
\endisadelimquote
haftmann@28566
   645
%
haftmann@22550
   646
\begin{isamarkuptext}%
haftmann@22550
   647
\noindent The connection to the type system is done by means
haftmann@22550
   648
  of a primitive axclass%
haftmann@22550
   649
\end{isamarkuptext}%
haftmann@22550
   650
\isamarkuptrue%
haftmann@30210
   651
\ %
haftmann@30210
   652
\isadeliminvisible
haftmann@28566
   653
%
haftmann@29513
   654
\endisadeliminvisible
haftmann@29513
   655
%
haftmann@29513
   656
\isataginvisible
haftmann@30210
   657
%
haftmann@29513
   658
\endisataginvisible
haftmann@29513
   659
{\isafoldinvisible}%
haftmann@29513
   660
%
haftmann@29513
   661
\isadeliminvisible
haftmann@29513
   662
%
haftmann@29513
   663
\endisadeliminvisible
haftmann@29513
   664
%
haftmann@28566
   665
\isadelimquote
haftmann@28566
   666
%
haftmann@28566
   667
\endisadelimquote
haftmann@28566
   668
%
haftmann@28566
   669
\isatagquote
haftmann@22479
   670
\isacommand{axclass}\isamarkupfalse%
haftmann@22479
   671
\ idem\ {\isacharless}\ type\isanewline
haftmann@30210
   672
\ \ idem{\isacharcolon}\ {\isachardoublequoteopen}f\ {\isacharparenleft}f\ x{\isacharparenright}\ {\isacharequal}\ f\ x{\isachardoublequoteclose}\ %
haftmann@28566
   673
\endisatagquote
haftmann@28566
   674
{\isafoldquote}%
haftmann@28566
   675
%
haftmann@28566
   676
\isadelimquote
haftmann@28566
   677
%
haftmann@28566
   678
\endisadelimquote
haftmann@28566
   679
%
haftmann@29513
   680
\isadeliminvisible
haftmann@30210
   681
%
haftmann@29513
   682
\endisadeliminvisible
haftmann@29513
   683
%
haftmann@29513
   684
\isataginvisible
haftmann@30210
   685
%
haftmann@29513
   686
\endisataginvisible
haftmann@29513
   687
{\isafoldinvisible}%
haftmann@29513
   688
%
haftmann@29513
   689
\isadeliminvisible
haftmann@29513
   690
%
haftmann@29513
   691
\endisadeliminvisible
haftmann@29513
   692
%
haftmann@22479
   693
\begin{isamarkuptext}%
wenzelm@22649
   694
\noindent together with a corresponding interpretation:%
haftmann@22479
   695
\end{isamarkuptext}%
haftmann@22479
   696
\isamarkuptrue%
haftmann@28566
   697
%
haftmann@28566
   698
\isadelimquote
haftmann@28566
   699
%
haftmann@28566
   700
\endisadelimquote
haftmann@28566
   701
%
haftmann@28566
   702
\isatagquote
haftmann@22479
   703
\isacommand{interpretation}\isamarkupfalse%
haftmann@29513
   704
\ idem{\isacharunderscore}class{\isacharcolon}\isanewline
wenzelm@29297
   705
\ \ idem\ {\isachardoublequoteopen}f\ {\isasymColon}\ {\isacharparenleft}{\isasymalpha}{\isasymColon}idem{\isacharparenright}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\isanewline
haftmann@28947
   706
\isacommand{proof}\isamarkupfalse%
haftmann@28947
   707
\ \isacommand{qed}\isamarkupfalse%
haftmann@28947
   708
\ {\isacharparenleft}rule\ idem{\isacharparenright}%
haftmann@28566
   709
\endisatagquote
haftmann@28566
   710
{\isafoldquote}%
haftmann@22479
   711
%
haftmann@28566
   712
\isadelimquote
haftmann@22479
   713
%
haftmann@28566
   714
\endisadelimquote
haftmann@28566
   715
%
haftmann@28566
   716
\begin{isamarkuptext}%
haftmann@28566
   717
\noindent This gives you at hand the full power of the Isabelle module system;
haftmann@28566
   718
  conclusions in locale \isa{idem} are implicitly propagated
haftmann@28566
   719
  to class \isa{idem}.%
haftmann@28566
   720
\end{isamarkuptext}%
haftmann@28566
   721
\isamarkuptrue%
haftmann@30210
   722
\ %
haftmann@30210
   723
\isadeliminvisible
haftmann@22479
   724
%
haftmann@28565
   725
\endisadeliminvisible
haftmann@22479
   726
%
haftmann@28565
   727
\isataginvisible
haftmann@30210
   728
%
haftmann@28565
   729
\endisataginvisible
haftmann@28565
   730
{\isafoldinvisible}%
haftmann@22479
   731
%
haftmann@28565
   732
\isadeliminvisible
haftmann@22479
   733
%
haftmann@28565
   734
\endisadeliminvisible
haftmann@22479
   735
%
haftmann@20967
   736
\isamarkupsubsection{Abstract reasoning%
haftmann@20967
   737
}
haftmann@20967
   738
\isamarkuptrue%
haftmann@20967
   739
%
haftmann@20967
   740
\begin{isamarkuptext}%
haftmann@22479
   741
Isabelle locales enable reasoning at a general level, while results
haftmann@20967
   742
  are implicitly transferred to all instances.  For example, we can
haftmann@20967
   743
  now establish the \isa{left{\isacharunderscore}cancel} lemma for groups, which
haftmann@25247
   744
  states that the function \isa{{\isacharparenleft}x\ {\isasymotimes}{\isacharparenright}} is injective:%
haftmann@20967
   745
\end{isamarkuptext}%
haftmann@20967
   746
\isamarkuptrue%
haftmann@28566
   747
%
haftmann@28566
   748
\isadelimquote
haftmann@28566
   749
%
haftmann@28566
   750
\endisadelimquote
haftmann@28566
   751
%
haftmann@28566
   752
\isatagquote
haftmann@28566
   753
\isacommand{lemma}\isamarkupfalse%
haftmann@25200
   754
\ {\isacharparenleft}\isakeyword{in}\ group{\isacharparenright}\ left{\isacharunderscore}cancel{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequoteclose}\isanewline
haftmann@20967
   755
\isacommand{proof}\isamarkupfalse%
haftmann@20967
   756
\isanewline
haftmann@28566
   757
\ \ \isacommand{assume}\isamarkupfalse%
haftmann@25200
   758
\ {\isachardoublequoteopen}x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z{\isachardoublequoteclose}\isanewline
haftmann@28566
   759
\ \ \isacommand{then}\isamarkupfalse%
haftmann@20967
   760
\ \isacommand{have}\isamarkupfalse%
haftmann@25200
   761
\ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ {\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isacharequal}\ x{\isasymdiv}\ {\isasymotimes}\ {\isacharparenleft}x\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   762
\ simp\isanewline
haftmann@28566
   763
\ \ \isacommand{then}\isamarkupfalse%
haftmann@20967
   764
\ \isacommand{have}\isamarkupfalse%
haftmann@25200
   765
\ {\isachardoublequoteopen}{\isacharparenleft}x{\isasymdiv}\ {\isasymotimes}\ x{\isacharparenright}\ {\isasymotimes}\ y\ {\isacharequal}\ {\isacharparenleft}x{\isasymdiv}\ {\isasymotimes}\ x{\isacharparenright}\ {\isasymotimes}\ z{\isachardoublequoteclose}\ \isacommand{using}\isamarkupfalse%
haftmann@20967
   766
\ assoc\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   767
\ simp\isanewline
haftmann@28566
   768
\ \ \isacommand{then}\isamarkupfalse%
haftmann@20967
   769
\ \isacommand{show}\isamarkupfalse%
haftmann@20967
   770
\ {\isachardoublequoteopen}y\ {\isacharequal}\ z{\isachardoublequoteclose}\ \isacommand{using}\isamarkupfalse%
haftmann@20967
   771
\ neutl\ \isakeyword{and}\ invl\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   772
\ simp\isanewline
haftmann@28566
   773
\isacommand{next}\isamarkupfalse%
haftmann@20967
   774
\isanewline
haftmann@28566
   775
\ \ \isacommand{assume}\isamarkupfalse%
haftmann@20967
   776
\ {\isachardoublequoteopen}y\ {\isacharequal}\ z{\isachardoublequoteclose}\isanewline
haftmann@28566
   777
\ \ \isacommand{then}\isamarkupfalse%
haftmann@20967
   778
\ \isacommand{show}\isamarkupfalse%
haftmann@25200
   779
\ {\isachardoublequoteopen}x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@20967
   780
\ simp\isanewline
haftmann@28566
   781
\isacommand{qed}\isamarkupfalse%
haftmann@20967
   782
%
haftmann@28566
   783
\endisatagquote
haftmann@28566
   784
{\isafoldquote}%
haftmann@20967
   785
%
haftmann@28566
   786
\isadelimquote
haftmann@20967
   787
%
haftmann@28566
   788
\endisadelimquote
haftmann@20967
   789
%
haftmann@20967
   790
\begin{isamarkuptext}%
haftmann@28565
   791
\noindent Here the \qt{\hyperlink{keyword.in}{\mbox{\isa{\isakeyword{in}}}} \isa{group}} target specification
haftmann@20967
   792
  indicates that the result is recorded within that context for later
haftmann@28565
   793
  use.  This local theorem is also lifted to the global one \hyperlink{fact.group.left-cancel:}{\mbox{\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
haftmann@22479
   794
  \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}}.%
haftmann@20967
   795
\end{isamarkuptext}%
haftmann@20967
   796
\isamarkuptrue%
haftmann@20967
   797
%
haftmann@23956
   798
\isamarkupsubsection{Derived definitions%
haftmann@23956
   799
}
haftmann@23956
   800
\isamarkuptrue%
haftmann@23956
   801
%
haftmann@23956
   802
\begin{isamarkuptext}%
haftmann@23956
   803
Isabelle locales support a concept of local definitions
haftmann@23956
   804
  in locales:%
haftmann@23956
   805
\end{isamarkuptext}%
haftmann@23956
   806
\isamarkuptrue%
haftmann@28566
   807
%
haftmann@28566
   808
\isadelimquote
haftmann@28566
   809
%
haftmann@28566
   810
\endisadelimquote
haftmann@28566
   811
%
haftmann@28566
   812
\isatagquote
haftmann@28566
   813
\isacommand{primrec}\isamarkupfalse%
haftmann@28540
   814
\ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\ pow{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28566
   815
\ \ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
haftmann@28566
   816
\ \ {\isacharbar}\ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x{\isachardoublequoteclose}%
haftmann@28566
   817
\endisatagquote
haftmann@28566
   818
{\isafoldquote}%
haftmann@28566
   819
%
haftmann@28566
   820
\isadelimquote
haftmann@28566
   821
%
haftmann@28566
   822
\endisadelimquote
haftmann@28566
   823
%
haftmann@23956
   824
\begin{isamarkuptext}%
haftmann@23956
   825
\noindent If the locale \isa{group} is also a class, this local
haftmann@23956
   826
  definition is propagated onto a global definition of
haftmann@23956
   827
  \isa{{\isachardoublequote}pow{\isacharunderscore}nat\ {\isasymColon}\ nat\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoid\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoid{\isachardoublequote}}
haftmann@23956
   828
  with corresponding theorems
haftmann@23956
   829
haftmann@23956
   830
  \isa{pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}\isasep\isanewline%
haftmann@23956
   831
pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x}.
haftmann@23956
   832
haftmann@23956
   833
  \noindent As you can see from this example, for local
haftmann@23956
   834
  definitions you may use any specification tool
haftmann@23956
   835
  which works together with locales (e.g. \cite{krauss2006}).%
haftmann@23956
   836
\end{isamarkuptext}%
haftmann@23956
   837
\isamarkuptrue%
haftmann@23956
   838
%
haftmann@25247
   839
\isamarkupsubsection{A functor analogy%
haftmann@25247
   840
}
haftmann@25247
   841
\isamarkuptrue%
haftmann@25247
   842
%
haftmann@25247
   843
\begin{isamarkuptext}%
haftmann@25247
   844
We introduced Isar classes by analogy to type classes
haftmann@25247
   845
  functional programming;  if we reconsider this in the
haftmann@25247
   846
  context of what has been said about type classes and locales,
haftmann@25247
   847
  we can drive this analogy further by stating that type
haftmann@25247
   848
  classes essentially correspond to functors which have
haftmann@25247
   849
  a canonical interpretation as type classes.
haftmann@25247
   850
  Anyway, there is also the possibility of other interpretations.
haftmann@25247
   851
  For example, also \isa{list}s form a monoid with
haftmann@28565
   852
  \isa{append} and \isa{{\isacharbrackleft}{\isacharbrackright}} as operations, but it
haftmann@25247
   853
  seems inappropriate to apply to lists
wenzelm@27507
   854
  the same operations as for genuinely algebraic types.
haftmann@25247
   855
  In such a case, we simply can do a particular interpretation
haftmann@25247
   856
  of monoids for lists:%
haftmann@25247
   857
\end{isamarkuptext}%
haftmann@25247
   858
\isamarkuptrue%
haftmann@28566
   859
%
haftmann@28566
   860
\isadelimquote
haftmann@28566
   861
%
haftmann@28566
   862
\endisadelimquote
haftmann@28566
   863
%
haftmann@28566
   864
\isatagquote
haftmann@29513
   865
\isacommand{interpretation}\isamarkupfalse%
haftmann@29513
   866
\ list{\isacharunderscore}monoid{\isacharbang}{\isacharcolon}\ monoid\ append\ {\isachardoublequoteopen}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
haftmann@28947
   867
\ \ \isacommand{proof}\isamarkupfalse%
haftmann@28947
   868
\ \isacommand{qed}\isamarkupfalse%
haftmann@28947
   869
\ auto%
haftmann@28566
   870
\endisatagquote
haftmann@28566
   871
{\isafoldquote}%
haftmann@25247
   872
%
haftmann@28566
   873
\isadelimquote
haftmann@25247
   874
%
haftmann@28566
   875
\endisadelimquote
haftmann@25247
   876
%
haftmann@25247
   877
\begin{isamarkuptext}%
haftmann@25247
   878
\noindent This enables us to apply facts on monoids
haftmann@25247
   879
  to lists, e.g. \isa{{\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ x\ {\isacharequal}\ x}.
haftmann@25247
   880
haftmann@25247
   881
  When using this interpretation pattern, it may also
haftmann@25247
   882
  be appropriate to map derived definitions accordingly:%
haftmann@25247
   883
\end{isamarkuptext}%
haftmann@25247
   884
\isamarkuptrue%
haftmann@28566
   885
%
haftmann@28566
   886
\isadelimquote
haftmann@28566
   887
%
haftmann@28566
   888
\endisadelimquote
haftmann@28566
   889
%
haftmann@28566
   890
\isatagquote
haftmann@28566
   891
\isacommand{primrec}\isamarkupfalse%
haftmann@28540
   892
\ replicate\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isasymalpha}\ list\ {\isasymRightarrow}\ {\isasymalpha}\ list{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28566
   893
\ \ {\isachardoublequoteopen}replicate\ {\isadigit{0}}\ {\isacharunderscore}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
haftmann@28566
   894
\ \ {\isacharbar}\ {\isachardoublequoteopen}replicate\ {\isacharparenleft}Suc\ n{\isacharparenright}\ xs\ {\isacharequal}\ xs\ {\isacharat}\ replicate\ n\ xs{\isachardoublequoteclose}\isanewline
haftmann@25247
   895
\isanewline
haftmann@29513
   896
\isacommand{interpretation}\isamarkupfalse%
haftmann@29513
   897
\ list{\isacharunderscore}monoid{\isacharbang}{\isacharcolon}\ monoid\ append\ {\isachardoublequoteopen}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28566
   898
\ \ {\isachardoublequoteopen}monoid{\isachardot}pow{\isacharunderscore}nat\ append\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ replicate{\isachardoublequoteclose}\isanewline
haftmann@25247
   899
\isacommand{proof}\isamarkupfalse%
haftmann@28540
   900
\ {\isacharminus}\isanewline
haftmann@29513
   901
\ \ \isacommand{interpret}\isamarkupfalse%
haftmann@29513
   902
\ monoid\ append\ {\isachardoublequoteopen}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\ \isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
haftmann@25247
   903
\isanewline
haftmann@28566
   904
\ \ \isacommand{show}\isamarkupfalse%
haftmann@28565
   905
\ {\isachardoublequoteopen}monoid{\isachardot}pow{\isacharunderscore}nat\ append\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ replicate{\isachardoublequoteclose}\isanewline
haftmann@28566
   906
\ \ \isacommand{proof}\isamarkupfalse%
haftmann@28540
   907
\isanewline
haftmann@28566
   908
\ \ \ \ \isacommand{fix}\isamarkupfalse%
haftmann@28540
   909
\ n\isanewline
haftmann@28566
   910
\ \ \ \ \isacommand{show}\isamarkupfalse%
haftmann@28565
   911
\ {\isachardoublequoteopen}monoid{\isachardot}pow{\isacharunderscore}nat\ append\ {\isacharbrackleft}{\isacharbrackright}\ n\ {\isacharequal}\ replicate\ n{\isachardoublequoteclose}\isanewline
haftmann@28566
   912
\ \ \ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann@25247
   913
\ {\isacharparenleft}induct\ n{\isacharparenright}\ auto\isanewline
haftmann@28566
   914
\ \ \isacommand{qed}\isamarkupfalse%
haftmann@28540
   915
\isanewline
haftmann@28566
   916
\isacommand{qed}\isamarkupfalse%
haftmann@28540
   917
\ intro{\isacharunderscore}locales%
haftmann@28566
   918
\endisatagquote
haftmann@28566
   919
{\isafoldquote}%
haftmann@25247
   920
%
haftmann@28566
   921
\isadelimquote
haftmann@25247
   922
%
haftmann@28566
   923
\endisadelimquote
haftmann@25247
   924
%
haftmann@24991
   925
\isamarkupsubsection{Additional subclass relations%
haftmann@24991
   926
}
haftmann@24991
   927
\isamarkuptrue%
haftmann@24991
   928
%
haftmann@24991
   929
\begin{isamarkuptext}%
haftmann@24991
   930
Any \isa{group} is also a \isa{monoid};  this
haftmann@25247
   931
  can be made explicit by claiming an additional
haftmann@25247
   932
  subclass relation,
haftmann@24991
   933
  together with a proof of the logical difference:%
haftmann@24991
   934
\end{isamarkuptext}%
haftmann@24991
   935
\isamarkuptrue%
haftmann@28566
   936
%
haftmann@28566
   937
\isadelimquote
haftmann@28566
   938
%
haftmann@28566
   939
\endisadelimquote
haftmann@28566
   940
%
haftmann@28566
   941
\isatagquote
haftmann@28566
   942
\isacommand{subclass}\isamarkupfalse%
haftmann@24991
   943
\ {\isacharparenleft}\isakeyword{in}\ group{\isacharparenright}\ monoid\isanewline
haftmann@24991
   944
\isacommand{proof}\isamarkupfalse%
haftmann@28947
   945
\isanewline
haftmann@28566
   946
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@24991
   947
\ x\isanewline
haftmann@28566
   948
\ \ \isacommand{from}\isamarkupfalse%
haftmann@24991
   949
\ invl\ \isacommand{have}\isamarkupfalse%
haftmann@25200
   950
\ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@24991
   951
\ simp\isanewline
haftmann@28566
   952
\ \ \isacommand{with}\isamarkupfalse%
haftmann@24991
   953
\ assoc\ {\isacharbrackleft}symmetric{\isacharbrackright}\ neutl\ invl\ \isacommand{have}\isamarkupfalse%
haftmann@25200
   954
\ {\isachardoublequoteopen}x{\isasymdiv}\ {\isasymotimes}\ {\isacharparenleft}x\ {\isasymotimes}\ {\isasymone}{\isacharparenright}\ {\isacharequal}\ x{\isasymdiv}\ {\isasymotimes}\ x{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@24991
   955
\ simp\isanewline
haftmann@28566
   956
\ \ \isacommand{with}\isamarkupfalse%
haftmann@24991
   957
\ left{\isacharunderscore}cancel\ \isacommand{show}\isamarkupfalse%
haftmann@25200
   958
\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@24991
   959
\ simp\isanewline
haftmann@28566
   960
\isacommand{qed}\isamarkupfalse%
haftmann@24991
   961
%
haftmann@28566
   962
\endisatagquote
haftmann@28566
   963
{\isafoldquote}%
haftmann@24991
   964
%
haftmann@28566
   965
\isadelimquote
haftmann@24991
   966
%
haftmann@28566
   967
\endisadelimquote
haftmann@24991
   968
%
haftmann@24991
   969
\begin{isamarkuptext}%
haftmann@30210
   970
The logical proof is carried out on the locale level.
haftmann@28947
   971
  Afterwards it is propagated
haftmann@24991
   972
  to the type system, making \isa{group} an instance of
haftmann@25247
   973
  \isa{monoid} by adding an additional edge
haftmann@25247
   974
  to the graph of subclass relations
haftmann@25247
   975
  (cf.\ \figref{fig:subclass}).
haftmann@25247
   976
haftmann@25247
   977
  \begin{figure}[htbp]
haftmann@25247
   978
   \begin{center}
haftmann@25247
   979
     \small
haftmann@25247
   980
     \unitlength 0.6mm
haftmann@25247
   981
     \begin{picture}(40,60)(0,0)
haftmann@25247
   982
       \put(20,60){\makebox(0,0){\isa{semigroup}}}
haftmann@25247
   983
       \put(20,40){\makebox(0,0){\isa{monoidl}}}
haftmann@25247
   984
       \put(00,20){\makebox(0,0){\isa{monoid}}}
haftmann@25247
   985
       \put(40,00){\makebox(0,0){\isa{group}}}
haftmann@25247
   986
       \put(20,55){\vector(0,-1){10}}
haftmann@25247
   987
       \put(15,35){\vector(-1,-1){10}}
haftmann@25247
   988
       \put(25,35){\vector(1,-3){10}}
haftmann@25247
   989
     \end{picture}
haftmann@25247
   990
     \hspace{8em}
haftmann@25247
   991
     \begin{picture}(40,60)(0,0)
haftmann@25247
   992
       \put(20,60){\makebox(0,0){\isa{semigroup}}}
haftmann@25247
   993
       \put(20,40){\makebox(0,0){\isa{monoidl}}}
haftmann@25247
   994
       \put(00,20){\makebox(0,0){\isa{monoid}}}
haftmann@25247
   995
       \put(40,00){\makebox(0,0){\isa{group}}}
haftmann@25247
   996
       \put(20,55){\vector(0,-1){10}}
haftmann@25247
   997
       \put(15,35){\vector(-1,-1){10}}
haftmann@25247
   998
       \put(05,15){\vector(3,-1){30}}
haftmann@25247
   999
     \end{picture}
haftmann@25247
  1000
     \caption{Subclass relationship of monoids and groups:
haftmann@25247
  1001
        before and after establishing the relationship
haftmann@30210
  1002
        \isa{group\ {\isasymsubseteq}\ monoid};  transitive edges are left out.}
haftmann@25247
  1003
     \label{fig:subclass}
haftmann@25247
  1004
   \end{center}
haftmann@25247
  1005
  \end{figure}
haftmann@30210
  1006
haftmann@25247
  1007
  For illustration, a derived definition
haftmann@24991
  1008
  in \isa{group} which uses \isa{pow{\isacharunderscore}nat}:%
haftmann@24991
  1009
\end{isamarkuptext}%
haftmann@24991
  1010
\isamarkuptrue%
haftmann@28565
  1011
%
haftmann@28565
  1012
\isadelimquote
haftmann@28565
  1013
%
haftmann@28565
  1014
\endisadelimquote
haftmann@28565
  1015
%
haftmann@28565
  1016
\isatagquote
haftmann@28565
  1017
\isacommand{definition}\isamarkupfalse%
haftmann@28565
  1018
\ {\isacharparenleft}\isakeyword{in}\ group{\isacharparenright}\ pow{\isacharunderscore}int\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}int\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28565
  1019
\ \ {\isachardoublequoteopen}pow{\isacharunderscore}int\ k\ x\ {\isacharequal}\ {\isacharparenleft}if\ k\ {\isachargreater}{\isacharequal}\ {\isadigit{0}}\isanewline
haftmann@28565
  1020
\ \ \ \ then\ pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ k{\isacharparenright}\ x\isanewline
haftmann@28565
  1021
\ \ \ \ else\ {\isacharparenleft}pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ {\isacharparenleft}{\isacharminus}\ k{\isacharparenright}{\isacharparenright}\ x{\isacharparenright}{\isasymdiv}{\isacharparenright}{\isachardoublequoteclose}%
haftmann@28565
  1022
\endisatagquote
haftmann@28565
  1023
{\isafoldquote}%
haftmann@28565
  1024
%
haftmann@28565
  1025
\isadelimquote
haftmann@28565
  1026
%
haftmann@28565
  1027
\endisadelimquote
haftmann@28565
  1028
%
haftmann@24991
  1029
\begin{isamarkuptext}%
haftmann@25247
  1030
\noindent yields the global definition of
haftmann@24991
  1031
  \isa{{\isachardoublequote}pow{\isacharunderscore}int\ {\isasymColon}\ int\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}group\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}group{\isachardoublequote}}
haftmann@24991
  1032
  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}}.%
haftmann@24991
  1033
\end{isamarkuptext}%
haftmann@24991
  1034
\isamarkuptrue%
haftmann@24991
  1035
%
haftmann@25868
  1036
\isamarkupsubsection{A note on syntax%
haftmann@25868
  1037
}
haftmann@25868
  1038
\isamarkuptrue%
haftmann@25868
  1039
%
haftmann@25868
  1040
\begin{isamarkuptext}%
haftmann@25868
  1041
As a commodity, class context syntax allows to refer
wenzelm@27507
  1042
  to local class operations and their global counterparts
haftmann@25868
  1043
  uniformly;  type inference resolves ambiguities.  For example:%
haftmann@25868
  1044
\end{isamarkuptext}%
haftmann@25868
  1045
\isamarkuptrue%
haftmann@28565
  1046
%
haftmann@28565
  1047
\isadelimquote
haftmann@28565
  1048
%
haftmann@28565
  1049
\endisadelimquote
haftmann@28565
  1050
%
haftmann@28565
  1051
\isatagquote
haftmann@25868
  1052
\isacommand{context}\isamarkupfalse%
haftmann@25868
  1053
\ semigroup\isanewline
haftmann@25868
  1054
\isakeyword{begin}\isanewline
haftmann@25868
  1055
\isanewline
haftmann@25868
  1056
\isacommand{term}\isamarkupfalse%
haftmann@25868
  1057
\ {\isachardoublequoteopen}x\ {\isasymotimes}\ y{\isachardoublequoteclose}\ %
haftmann@25868
  1058
\isamarkupcmt{example 1%
haftmann@25868
  1059
}
haftmann@25868
  1060
\isanewline
haftmann@25868
  1061
\isacommand{term}\isamarkupfalse%
haftmann@25868
  1062
\ {\isachardoublequoteopen}{\isacharparenleft}x{\isasymColon}nat{\isacharparenright}\ {\isasymotimes}\ y{\isachardoublequoteclose}\ %
haftmann@25868
  1063
\isamarkupcmt{example 2%
haftmann@25868
  1064
}
haftmann@25868
  1065
\isanewline
haftmann@25868
  1066
\isanewline
haftmann@25868
  1067
\isacommand{end}\isamarkupfalse%
haftmann@25868
  1068
\isanewline
haftmann@25868
  1069
\isanewline
haftmann@25868
  1070
\isacommand{term}\isamarkupfalse%
haftmann@25868
  1071
\ {\isachardoublequoteopen}x\ {\isasymotimes}\ y{\isachardoublequoteclose}\ %
haftmann@25868
  1072
\isamarkupcmt{example 3%
haftmann@25868
  1073
}
haftmann@25868
  1074
%
haftmann@28565
  1075
\endisatagquote
haftmann@28565
  1076
{\isafoldquote}%
haftmann@28565
  1077
%
haftmann@28565
  1078
\isadelimquote
haftmann@28565
  1079
%
haftmann@28565
  1080
\endisadelimquote
haftmann@28565
  1081
%
haftmann@25868
  1082
\begin{isamarkuptext}%
haftmann@25868
  1083
\noindent Here in example 1, the term refers to the local class operation
haftmann@25868
  1084
  \isa{mult\ {\isacharbrackleft}{\isasymalpha}{\isacharbrackright}}, whereas in example 2 the type constraint
haftmann@25868
  1085
  enforces the global class operation \isa{mult\ {\isacharbrackleft}nat{\isacharbrackright}}.
haftmann@25868
  1086
  In the global context in example 3, the reference is
haftmann@25868
  1087
  to the polymorphic global class operation \isa{mult\ {\isacharbrackleft}{\isacharquery}{\isasymalpha}\ {\isasymColon}\ semigroup{\isacharbrackright}}.%
haftmann@25868
  1088
\end{isamarkuptext}%
haftmann@25868
  1089
\isamarkuptrue%
haftmann@25868
  1090
%
haftmann@29705
  1091
\isamarkupsection{Further issues%
haftmann@29705
  1092
}
haftmann@29705
  1093
\isamarkuptrue%
haftmann@29705
  1094
%
haftmann@29705
  1095
\isamarkupsubsection{Type classes and code generation%
haftmann@20967
  1096
}
haftmann@20967
  1097
\isamarkuptrue%
haftmann@20967
  1098
%
haftmann@20967
  1099
\begin{isamarkuptext}%
haftmann@22317
  1100
Turning back to the first motivation for type classes,
haftmann@22317
  1101
  namely overloading, it is obvious that overloading
haftmann@28565
  1102
  stemming from \hyperlink{command.class}{\mbox{\isa{\isacommand{class}}}} statements and
haftmann@28565
  1103
  \hyperlink{command.instantiation}{\mbox{\isa{\isacommand{instantiation}}}}
haftmann@25533
  1104
  targets naturally maps to Haskell type classes.
haftmann@22317
  1105
  The code generator framework \cite{isabelle-codegen} 
haftmann@22317
  1106
  takes this into account.  Concerning target languages
haftmann@22317
  1107
  lacking type classes (e.g.~SML), type classes
haftmann@22317
  1108
  are implemented by explicit dictionary construction.
haftmann@28565
  1109
  As example, let's go back to the power function:%
haftmann@20967
  1110
\end{isamarkuptext}%
haftmann@20967
  1111
\isamarkuptrue%
haftmann@28565
  1112
%
haftmann@28565
  1113
\isadelimquote
haftmann@28565
  1114
%
haftmann@28565
  1115
\endisadelimquote
haftmann@28565
  1116
%
haftmann@28565
  1117
\isatagquote
haftmann@28565
  1118
\isacommand{definition}\isamarkupfalse%
haftmann@28565
  1119
\ example\ {\isacharcolon}{\isacharcolon}\ int\ \isakeyword{where}\isanewline
haftmann@28565
  1120
\ \ {\isachardoublequoteopen}example\ {\isacharequal}\ pow{\isacharunderscore}int\ {\isadigit{1}}{\isadigit{0}}\ {\isacharparenleft}{\isacharminus}{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}%
haftmann@28565
  1121
\endisatagquote
haftmann@28565
  1122
{\isafoldquote}%
haftmann@28565
  1123
%
haftmann@28565
  1124
\isadelimquote
haftmann@28565
  1125
%
haftmann@28565
  1126
\endisadelimquote
haftmann@28565
  1127
%
haftmann@20967
  1128
\begin{isamarkuptext}%
haftmann@22317
  1129
\noindent This maps to Haskell as:%
haftmann@20967
  1130
\end{isamarkuptext}%
haftmann@20967
  1131
\isamarkuptrue%
haftmann@28540
  1132
%
haftmann@28565
  1133
\isadelimquote
haftmann@28540
  1134
%
haftmann@28565
  1135
\endisadelimquote
haftmann@28540
  1136
%
haftmann@28565
  1137
\isatagquote
haftmann@28540
  1138
%
haftmann@20967
  1139
\begin{isamarkuptext}%
haftmann@28727
  1140
\isatypewriter%
haftmann@28540
  1141
\noindent%
haftmann@28714
  1142
\hspace*{0pt}module Example where {\char123}\\
haftmann@28714
  1143
\hspace*{0pt}\\
haftmann@28714
  1144
\hspace*{0pt}\\
wenzelm@30121
  1145
\hspace*{0pt}data Nat = Zero{\char95}nat | Suc Nat;\\
haftmann@28714
  1146
\hspace*{0pt}\\
haftmann@28714
  1147
\hspace*{0pt}nat{\char95}aux ::~Integer -> Nat -> Nat;\\
haftmann@28714
  1148
\hspace*{0pt}nat{\char95}aux i n = (if i <= 0 then n else nat{\char95}aux (i - 1) (Suc n));\\
haftmann@28714
  1149
\hspace*{0pt}\\
haftmann@28714
  1150
\hspace*{0pt}nat ::~Integer -> Nat;\\
haftmann@28714
  1151
\hspace*{0pt}nat i = nat{\char95}aux i Zero{\char95}nat;\\
haftmann@28714
  1152
\hspace*{0pt}\\
haftmann@28714
  1153
\hspace*{0pt}class Semigroup a where {\char123}\\
haftmann@28714
  1154
\hspace*{0pt} ~mult ::~a -> a -> a;\\
haftmann@28714
  1155
\hspace*{0pt}{\char125};\\
haftmann@28714
  1156
\hspace*{0pt}\\
haftmann@28714
  1157
\hspace*{0pt}class (Semigroup a) => Monoidl a where {\char123}\\
haftmann@28714
  1158
\hspace*{0pt} ~neutral ::~a;\\
haftmann@28714
  1159
\hspace*{0pt}{\char125};\\
haftmann@28714
  1160
\hspace*{0pt}\\
haftmann@28714
  1161
\hspace*{0pt}class (Monoidl a) => Monoid a where {\char123}\\
haftmann@28714
  1162
\hspace*{0pt}{\char125};\\
haftmann@28714
  1163
\hspace*{0pt}\\
haftmann@28714
  1164
\hspace*{0pt}class (Monoid a) => Group a where {\char123}\\
haftmann@28714
  1165
\hspace*{0pt} ~inverse ::~a -> a;\\
haftmann@28714
  1166
\hspace*{0pt}{\char125};\\
haftmann@28714
  1167
\hspace*{0pt}\\
haftmann@28714
  1168
\hspace*{0pt}inverse{\char95}int ::~Integer -> Integer;\\
haftmann@28714
  1169
\hspace*{0pt}inverse{\char95}int i = negate i;\\
haftmann@28714
  1170
\hspace*{0pt}\\
haftmann@28714
  1171
\hspace*{0pt}neutral{\char95}int ::~Integer;\\
haftmann@28714
  1172
\hspace*{0pt}neutral{\char95}int = 0;\\
haftmann@28714
  1173
\hspace*{0pt}\\
haftmann@28714
  1174
\hspace*{0pt}mult{\char95}int ::~Integer -> Integer -> Integer;\\
haftmann@28714
  1175
\hspace*{0pt}mult{\char95}int i j = i + j;\\
haftmann@28714
  1176
\hspace*{0pt}\\
haftmann@28714
  1177
\hspace*{0pt}instance Semigroup Integer where {\char123}\\
haftmann@28714
  1178
\hspace*{0pt} ~mult = mult{\char95}int;\\
haftmann@28714
  1179
\hspace*{0pt}{\char125};\\
haftmann@28714
  1180
\hspace*{0pt}\\
haftmann@28714
  1181
\hspace*{0pt}instance Monoidl Integer where {\char123}\\
haftmann@28714
  1182
\hspace*{0pt} ~neutral = neutral{\char95}int;\\
haftmann@28714
  1183
\hspace*{0pt}{\char125};\\
haftmann@28714
  1184
\hspace*{0pt}\\
haftmann@28714
  1185
\hspace*{0pt}instance Monoid Integer where {\char123}\\
haftmann@28714
  1186
\hspace*{0pt}{\char125};\\
haftmann@28714
  1187
\hspace*{0pt}\\
haftmann@28714
  1188
\hspace*{0pt}instance Group Integer where {\char123}\\
haftmann@28714
  1189
\hspace*{0pt} ~inverse = inverse{\char95}int;\\
haftmann@28714
  1190
\hspace*{0pt}{\char125};\\
haftmann@28714
  1191
\hspace*{0pt}\\
haftmann@28947
  1192
\hspace*{0pt}pow{\char95}nat ::~forall a.~(Monoid a) => Nat -> a -> a;\\
haftmann@28714
  1193
\hspace*{0pt}pow{\char95}nat Zero{\char95}nat x = neutral;\\
haftmann@28714
  1194
\hspace*{0pt}pow{\char95}nat (Suc n) x = mult x (pow{\char95}nat n x);\\
haftmann@28714
  1195
\hspace*{0pt}\\
haftmann@28947
  1196
\hspace*{0pt}pow{\char95}int ::~forall a.~(Group a) => Integer -> a -> a;\\
haftmann@28714
  1197
\hspace*{0pt}pow{\char95}int k x =\\
haftmann@28714
  1198
\hspace*{0pt} ~(if 0 <= k then pow{\char95}nat (nat k) x\\
haftmann@28714
  1199
\hspace*{0pt} ~~~else inverse (pow{\char95}nat (nat (negate k)) x));\\
haftmann@28714
  1200
\hspace*{0pt}\\
haftmann@28714
  1201
\hspace*{0pt}example ::~Integer;\\
haftmann@28714
  1202
\hspace*{0pt}example = pow{\char95}int 10 (-2);\\
haftmann@28714
  1203
\hspace*{0pt}\\
haftmann@28714
  1204
\hspace*{0pt}{\char125}%
haftmann@22317
  1205
\end{isamarkuptext}%
haftmann@22317
  1206
\isamarkuptrue%
haftmann@28540
  1207
%
haftmann@28565
  1208
\endisatagquote
haftmann@28565
  1209
{\isafoldquote}%
haftmann@28540
  1210
%
haftmann@28565
  1211
\isadelimquote
haftmann@28540
  1212
%
haftmann@28565
  1213
\endisadelimquote
haftmann@28540
  1214
%
haftmann@22317
  1215
\begin{isamarkuptext}%
haftmann@28540
  1216
\noindent The whole code in SML with explicit dictionary passing:%
haftmann@20967
  1217
\end{isamarkuptext}%
haftmann@20967
  1218
\isamarkuptrue%
haftmann@20967
  1219
%
haftmann@28565
  1220
\isadelimquote
haftmann@28540
  1221
%
haftmann@28565
  1222
\endisadelimquote
haftmann@28540
  1223
%
haftmann@28565
  1224
\isatagquote
haftmann@28540
  1225
%
haftmann@28540
  1226
\begin{isamarkuptext}%
haftmann@28727
  1227
\isatypewriter%
haftmann@28540
  1228
\noindent%
haftmann@28714
  1229
\hspace*{0pt}structure Example = \\
haftmann@28714
  1230
\hspace*{0pt}struct\\
haftmann@28714
  1231
\hspace*{0pt}\\
wenzelm@30121
  1232
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\
haftmann@28714
  1233
\hspace*{0pt}\\
haftmann@28714
  1234
\hspace*{0pt}fun nat{\char95}aux i n =\\
haftmann@28947
  1235
\hspace*{0pt} ~(if IntInf.<= (i,~(0 :~IntInf.int)) then n\\
haftmann@28947
  1236
\hspace*{0pt} ~~~else nat{\char95}aux (IntInf.- (i,~(1 :~IntInf.int))) (Suc n));\\
haftmann@28714
  1237
\hspace*{0pt}\\
haftmann@28714
  1238
\hspace*{0pt}fun nat i = nat{\char95}aux i Zero{\char95}nat;\\
haftmann@28714
  1239
\hspace*{0pt}\\
haftmann@28714
  1240
\hspace*{0pt}type 'a semigroup = {\char123}mult :~'a -> 'a -> 'a{\char125};\\
haftmann@28714
  1241
\hspace*{0pt}fun mult (A{\char95}:'a semigroup) = {\char35}mult A{\char95};\\
haftmann@28714
  1242
\hspace*{0pt}\\
haftmann@28714
  1243
\hspace*{0pt}type 'a monoidl =\\
haftmann@28947
  1244
\hspace*{0pt} ~{\char123}Classes{\char95}{\char95}semigroup{\char95}monoidl :~'a semigroup,~neutral :~'a{\char125};\\
haftmann@28714
  1245
\hspace*{0pt}fun semigroup{\char95}monoidl (A{\char95}:'a monoidl) = {\char35}Classes{\char95}{\char95}semigroup{\char95}monoidl A{\char95};\\
haftmann@28714
  1246
\hspace*{0pt}fun neutral (A{\char95}:'a monoidl) = {\char35}neutral A{\char95};\\
haftmann@28714
  1247
\hspace*{0pt}\\
haftmann@28714
  1248
\hspace*{0pt}type 'a monoid = {\char123}Classes{\char95}{\char95}monoidl{\char95}monoid :~'a monoidl{\char125};\\
haftmann@28714
  1249
\hspace*{0pt}fun monoidl{\char95}monoid (A{\char95}:'a monoid) = {\char35}Classes{\char95}{\char95}monoidl{\char95}monoid A{\char95};\\
haftmann@28714
  1250
\hspace*{0pt}\\
haftmann@28947
  1251
\hspace*{0pt}type 'a group = {\char123}Classes{\char95}{\char95}monoid{\char95}group :~'a monoid,~inverse :~'a -> 'a{\char125};\\
haftmann@28714
  1252
\hspace*{0pt}fun monoid{\char95}group (A{\char95}:'a group) = {\char35}Classes{\char95}{\char95}monoid{\char95}group A{\char95};\\
haftmann@28714
  1253
\hspace*{0pt}fun inverse (A{\char95}:'a group) = {\char35}inverse A{\char95};\\
haftmann@28714
  1254
\hspace*{0pt}\\
haftmann@28714
  1255
\hspace*{0pt}fun inverse{\char95}int i = IntInf.{\char126}~i;\\
haftmann@28714
  1256
\hspace*{0pt}\\
wenzelm@29297
  1257
\hspace*{0pt}val neutral{\char95}int :~IntInf.int = (0 :~IntInf.int)\\
haftmann@28714
  1258
\hspace*{0pt}\\
haftmann@28947
  1259
\hspace*{0pt}fun mult{\char95}int i j = IntInf.+ (i,~j);\\
haftmann@28714
  1260
\hspace*{0pt}\\
haftmann@28714
  1261
\hspace*{0pt}val semigroup{\char95}int = {\char123}mult = mult{\char95}int{\char125}~:~IntInf.int semigroup;\\
haftmann@28714
  1262
\hspace*{0pt}\\
haftmann@28714
  1263
\hspace*{0pt}val monoidl{\char95}int =\\
haftmann@28947
  1264
\hspace*{0pt} ~{\char123}Classes{\char95}{\char95}semigroup{\char95}monoidl = semigroup{\char95}int,~neutral = neutral{\char95}int{\char125}~:\\
haftmann@28714
  1265
\hspace*{0pt} ~IntInf.int monoidl;\\
haftmann@28714
  1266
\hspace*{0pt}\\
haftmann@28714
  1267
\hspace*{0pt}val monoid{\char95}int = {\char123}Classes{\char95}{\char95}monoidl{\char95}monoid = monoidl{\char95}int{\char125}~:\\
haftmann@28714
  1268
\hspace*{0pt} ~IntInf.int monoid;\\
haftmann@28714
  1269
\hspace*{0pt}\\
haftmann@28714
  1270
\hspace*{0pt}val group{\char95}int =\\
haftmann@28947
  1271
\hspace*{0pt} ~{\char123}Classes{\char95}{\char95}monoid{\char95}group = monoid{\char95}int,~inverse = inverse{\char95}int{\char125}~:\\
haftmann@28714
  1272
\hspace*{0pt} ~IntInf.int group;\\
haftmann@28714
  1273
\hspace*{0pt}\\
haftmann@28714
  1274
\hspace*{0pt}fun pow{\char95}nat A{\char95}~Zero{\char95}nat x = neutral (monoidl{\char95}monoid A{\char95})\\
haftmann@28714
  1275
\hspace*{0pt} ~| pow{\char95}nat A{\char95}~(Suc n) x =\\
haftmann@28714
  1276
\hspace*{0pt} ~~~mult ((semigroup{\char95}monoidl o monoidl{\char95}monoid) A{\char95}) x (pow{\char95}nat A{\char95}~n x);\\
haftmann@28714
  1277
\hspace*{0pt}\\
haftmann@28714
  1278
\hspace*{0pt}fun pow{\char95}int A{\char95}~k x =\\
haftmann@28947
  1279
\hspace*{0pt} ~(if IntInf.<= ((0 :~IntInf.int),~k)\\
haftmann@28714
  1280
\hspace*{0pt} ~~~then pow{\char95}nat (monoid{\char95}group A{\char95}) (nat k) x\\
haftmann@28714
  1281
\hspace*{0pt} ~~~else inverse A{\char95}~(pow{\char95}nat (monoid{\char95}group A{\char95}) (nat (IntInf.{\char126}~k)) x));\\
haftmann@28714
  1282
\hspace*{0pt}\\
haftmann@28714
  1283
\hspace*{0pt}val example :~IntInf.int =\\
wenzelm@29297
  1284
\hspace*{0pt} ~pow{\char95}int group{\char95}int (10 :~IntInf.int) ({\char126}2 :~IntInf.int)\\
haftmann@28714
  1285
\hspace*{0pt}\\
haftmann@28947
  1286
\hspace*{0pt}end;~(*struct Example*)%
haftmann@28540
  1287
\end{isamarkuptext}%
haftmann@28540
  1288
\isamarkuptrue%
haftmann@28540
  1289
%
haftmann@28565
  1290
\endisatagquote
haftmann@28565
  1291
{\isafoldquote}%
haftmann@28540
  1292
%
haftmann@28565
  1293
\isadelimquote
haftmann@28540
  1294
%
haftmann@28565
  1295
\endisadelimquote
haftmann@28540
  1296
%
haftmann@29705
  1297
\isamarkupsubsection{Inspecting the type class universe%
haftmann@29705
  1298
}
haftmann@29705
  1299
\isamarkuptrue%
haftmann@29705
  1300
%
haftmann@29705
  1301
\begin{isamarkuptext}%
haftmann@29705
  1302
To facilitate orientation in complex subclass structures,
haftmann@29705
  1303
  two diagnostics commands are provided:
haftmann@29705
  1304
haftmann@29705
  1305
  \begin{description}
haftmann@29705
  1306
haftmann@29705
  1307
    \item[\hyperlink{command.print-classes}{\mbox{\isa{\isacommand{print{\isacharunderscore}classes}}}}] print a list of all classes
haftmann@29705
  1308
      together with associated operations etc.
haftmann@29705
  1309
haftmann@29705
  1310
    \item[\hyperlink{command.class-deps}{\mbox{\isa{\isacommand{class{\isacharunderscore}deps}}}}] visualizes the subclass relation
haftmann@29705
  1311
      between all classes as a Hasse diagram.
haftmann@29705
  1312
haftmann@29705
  1313
  \end{description}%
haftmann@29705
  1314
\end{isamarkuptext}%
haftmann@29705
  1315
\isamarkuptrue%
haftmann@29705
  1316
%
haftmann@20967
  1317
\isadelimtheory
haftmann@20967
  1318
%
haftmann@20967
  1319
\endisadelimtheory
haftmann@20967
  1320
%
haftmann@20967
  1321
\isatagtheory
haftmann@20967
  1322
\isacommand{end}\isamarkupfalse%
haftmann@20967
  1323
%
haftmann@20967
  1324
\endisatagtheory
haftmann@20967
  1325
{\isafoldtheory}%
haftmann@20967
  1326
%
haftmann@20967
  1327
\isadelimtheory
haftmann@20967
  1328
%
haftmann@20967
  1329
\endisadelimtheory
haftmann@20967
  1330
\isanewline
haftmann@20967
  1331
\end{isabellebody}%
haftmann@20967
  1332
%%% Local Variables:
haftmann@20967
  1333
%%% mode: latex
haftmann@20967
  1334
%%% TeX-master: "root"
haftmann@20967
  1335
%%% End: