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