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