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