20967
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{Classes}%
|
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
\isanewline
|
|
7 |
\isanewline
|
|
8 |
\isanewline
|
|
9 |
%
|
|
10 |
\endisadelimtheory
|
|
11 |
%
|
|
12 |
\isatagtheory
|
|
13 |
\isacommand{theory}\isamarkupfalse%
|
|
14 |
\ Classes\isanewline
|
|
15 |
\isakeyword{imports}\ Main\isanewline
|
22317
|
16 |
\isakeyword{begin}%
|
20967
|
17 |
\endisatagtheory
|
|
18 |
{\isafoldtheory}%
|
|
19 |
%
|
|
20 |
\isadelimtheory
|
22317
|
21 |
\isanewline
|
20967
|
22 |
%
|
|
23 |
\endisadelimtheory
|
|
24 |
%
|
|
25 |
\isadelimML
|
22317
|
26 |
\isanewline
|
|
27 |
%
|
|
28 |
\endisadelimML
|
|
29 |
%
|
|
30 |
\isatagML
|
|
31 |
\isacommand{ML}\isamarkupfalse%
|
|
32 |
\ {\isacharverbatimopen}\isanewline
|
|
33 |
CodegenSerializer{\isachardot}code{\isacharunderscore}width\ {\isacharcolon}{\isacharequal}\ {\isadigit{7}}{\isadigit{4}}{\isacharsemicolon}\isanewline
|
|
34 |
{\isacharverbatimclose}\isanewline
|
|
35 |
%
|
|
36 |
\endisatagML
|
|
37 |
{\isafoldML}%
|
|
38 |
%
|
|
39 |
\isadelimML
|
|
40 |
%
|
|
41 |
\endisadelimML
|
|
42 |
%
|
|
43 |
\isadelimML
|
20967
|
44 |
%
|
|
45 |
\endisadelimML
|
|
46 |
%
|
|
47 |
\isatagML
|
|
48 |
%
|
|
49 |
\endisatagML
|
|
50 |
{\isafoldML}%
|
|
51 |
%
|
|
52 |
\isadelimML
|
|
53 |
%
|
|
54 |
\endisadelimML
|
|
55 |
%
|
|
56 |
\isamarkupchapter{Haskell-style classes with Isabelle/Isar%
|
|
57 |
}
|
|
58 |
\isamarkuptrue%
|
|
59 |
%
|
|
60 |
\isamarkupsection{Introduction%
|
|
61 |
}
|
|
62 |
\isamarkuptrue%
|
|
63 |
%
|
|
64 |
\begin{isamarkuptext}%
|
22317
|
65 |
Type classes were introduces by Wadler and Blott \cite{wadler89how}
|
|
66 |
into the Haskell language, to allow for a reasonable implementation
|
|
67 |
of overloading\footnote{throughout this tutorial, we are referring
|
|
68 |
to classical Haskell 1.0 type classes, not considering
|
|
69 |
later additions in expressiveness}.
|
|
70 |
As a canonical example, a polymorphic equality function
|
|
71 |
\isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} which is overloaded on different
|
|
72 |
types for \isa{{\isasymalpha}}, which is achieves by splitting introduction
|
|
73 |
of the \isa{eq} function from its overloaded definitions by means
|
|
74 |
of \isa{class} and \isa{instance} declarations:
|
|
75 |
|
|
76 |
\medskip\noindent\hspace*{2ex}\isa{class\ eq\ where}\footnote{syntax here is a kind of isabellized Haskell} \\
|
|
77 |
\hspace*{4ex}\isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool}
|
|
78 |
|
|
79 |
\medskip\noindent\hspace*{2ex}\isa{instance\ nat\ {\isasymColon}\ eq\ where} \\
|
|
80 |
\hspace*{4ex}\isa{eq\ {\isadigit{0}}\ {\isadigit{0}}\ {\isacharequal}\ True} \\
|
|
81 |
\hspace*{4ex}\isa{eq\ {\isadigit{0}}\ {\isacharunderscore}\ {\isacharequal}\ False} \\
|
|
82 |
\hspace*{4ex}\isa{eq\ {\isacharunderscore}\ {\isadigit{0}}\ {\isacharequal}\ False} \\
|
|
83 |
\hspace*{4ex}\isa{eq\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharparenleft}Suc\ m{\isacharparenright}\ {\isacharequal}\ eq\ n\ m}
|
|
84 |
|
|
85 |
\medskip\noindent\hspace*{2ex}\isa{instance\ {\isacharparenleft}{\isasymalpha}{\isasymColon}eq{\isacharcomma}\ {\isasymbeta}{\isasymColon}eq{\isacharparenright}\ pair\ {\isasymColon}\ eq\ where} \\
|
|
86 |
\hspace*{4ex}\isa{eq\ {\isacharparenleft}x{\isadigit{1}}{\isacharcomma}\ y{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ eq\ x{\isadigit{1}}\ x{\isadigit{2}}\ {\isacharampersand}{\isacharampersand}\ eq\ y{\isadigit{1}}\ y{\isadigit{2}}}
|
|
87 |
|
|
88 |
\medskip\noindent\hspace*{2ex}\isa{class\ ord\ extends\ eq\ where} \\
|
|
89 |
\hspace*{4ex}\isa{less{\isacharunderscore}eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} \\
|
|
90 |
\hspace*{4ex}\isa{less\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool}
|
|
91 |
|
|
92 |
\medskip Type variables are annotated with (finitly many) classes;
|
|
93 |
these annotations are assertions that a particular polymorphic type
|
|
94 |
provides definitions for overloaded functions.
|
|
95 |
|
|
96 |
Indeed, type classes not only allow for simple overloading
|
|
97 |
but form a generic calculus, an instance of order-sorted
|
|
98 |
algebra \cite{Nipkow-Prehofer:1993,Nipkow:1993,Wenzel:1997}.
|
20967
|
99 |
|
22317
|
100 |
From a software enigineering point of view, type classes
|
|
101 |
correspond to interfaces in object-oriented languages like Java;
|
|
102 |
so, it is naturally desirable that type classes do not only
|
|
103 |
provide functions (class operations) but also state specifications
|
|
104 |
implementations must obey. For example, the \isa{class\ eq}
|
|
105 |
above could be given the following specification:
|
|
106 |
|
|
107 |
\medskip\noindent\hspace*{2ex}\isa{class\ eq\ where} \\
|
|
108 |
\hspace*{4ex}\isa{eq\ {\isasymColon}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ bool} \\
|
|
109 |
\hspace*{2ex}\isa{satisfying} \\
|
|
110 |
\hspace*{4ex}\isa{refl{\isacharcolon}\ eq\ x\ x} \\
|
|
111 |
\hspace*{4ex}\isa{sym{\isacharcolon}\ eq\ x\ y\ {\isasymlongleftrightarrow}\ eq\ x\ y} \\
|
|
112 |
\hspace*{4ex}\isa{trans{\isacharcolon}\ eq\ x\ y\ {\isasymand}\ eq\ y\ z\ {\isasymlongrightarrow}\ eq\ x\ z}
|
|
113 |
|
|
114 |
\medskip From a theoretic point of view, type classes are leightweight
|
|
115 |
modules; indeed, Haskell type classes may be emulated by
|
|
116 |
SML functors \cite{classes_modules}.
|
|
117 |
Isabelle/Isar offers a discipline of type classes which brings
|
|
118 |
all those aspects together:
|
|
119 |
|
|
120 |
\begin{enumerate}
|
|
121 |
\item specifying abstract operations togehter with
|
|
122 |
corresponding specifications,
|
|
123 |
\item instantating those abstract operations by a particular
|
|
124 |
type
|
|
125 |
\item in connection with a ``less ad-hoc'' approach to overloading,
|
|
126 |
\item with a direct link to the Isabelle module system (aka locales).
|
|
127 |
\end{enumerate}
|
|
128 |
|
|
129 |
Isar type classes also directly support code generation
|
|
130 |
in as Haskell like fashion.
|
|
131 |
|
|
132 |
This tutorial demonstrates common elements of structured specifications
|
|
133 |
and abstract reasoning with type classes by the algebraic hierarchy of
|
|
134 |
semigroups, monoids and groups. Our background theory is that of
|
|
135 |
Isabelle/HOL \cite{Nipkow-et-al:2002:tutorial}, for which some
|
|
136 |
familiarity is assumed.
|
|
137 |
|
|
138 |
Here we merely present the look-and-feel for end users.
|
|
139 |
Internally, those are mapped to more primitive Isabelle concepts.
|
|
140 |
See \cite{haftmann_wenzel2006classes} for more detail.%
|
20967
|
141 |
\end{isamarkuptext}%
|
|
142 |
\isamarkuptrue%
|
|
143 |
%
|
|
144 |
\isamarkupsection{A simple algebra example \label{sec:example}%
|
|
145 |
}
|
|
146 |
\isamarkuptrue%
|
|
147 |
%
|
|
148 |
\isamarkupsubsection{Class definition%
|
|
149 |
}
|
|
150 |
\isamarkuptrue%
|
|
151 |
%
|
|
152 |
\begin{isamarkuptext}%
|
|
153 |
Depending on an arbitrary type \isa{{\isasymalpha}}, class \isa{semigroup} introduces a binary operation \isa{{\isasymcirc}} that is
|
|
154 |
assumed to be associative:%
|
|
155 |
\end{isamarkuptext}%
|
|
156 |
\isamarkuptrue%
|
|
157 |
\ \ \ \ \isacommand{class}\isamarkupfalse%
|
|
158 |
\ semigroup\ {\isacharequal}\isanewline
|
|
159 |
\ \ \ \ \ \ \isakeyword{fixes}\ mult\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \ \ \ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}\isactrlloc {\isasymotimes}{\isachardoublequoteclose}\ {\isadigit{7}}{\isadigit{0}}{\isacharparenright}\isanewline
|
|
160 |
\ \ \ \ \ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ \isactrlloc {\isasymotimes}\ y{\isacharparenright}\ \isactrlloc {\isasymotimes}\ z\ {\isacharequal}\ x\ \isactrlloc {\isasymotimes}\ {\isacharparenleft}y\ \isactrlloc {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}%
|
|
161 |
\begin{isamarkuptext}%
|
|
162 |
\noindent This \isa{{\isasymCLASS}} specification consists of two
|
|
163 |
parts: the \qn{operational} part names the class operation (\isa{{\isasymFIXES}}), the \qn{logical} part specifies properties on them
|
|
164 |
(\isa{{\isasymASSUMES}}). The local \isa{{\isasymFIXES}} and \isa{{\isasymASSUMES}} are lifted to the theory toplevel, yielding the global
|
|
165 |
operation \isa{{\isachardoublequote}mult\ {\isacharcolon}{\isacharcolon}\ {\isasymalpha}{\isacharcolon}{\isacharcolon}semigroup\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequote}} and the
|
|
166 |
global theorem \isa{semigroup{\isachardot}assoc{\isacharcolon}}~\isa{{\isachardoublequote}{\isasymAnd}x\ y\ z{\isacharcolon}{\isacharcolon}{\isasymalpha}{\isacharcolon}{\isacharcolon}semigroup{\isachardot}\ {\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequote}}.%
|
|
167 |
\end{isamarkuptext}%
|
|
168 |
\isamarkuptrue%
|
|
169 |
%
|
|
170 |
\isamarkupsubsection{Class instantiation \label{sec:class_inst}%
|
|
171 |
}
|
|
172 |
\isamarkuptrue%
|
|
173 |
%
|
|
174 |
\begin{isamarkuptext}%
|
|
175 |
The concrete type \isa{int} is made a \isa{semigroup}
|
|
176 |
instance by providing a suitable definition for the class operation
|
|
177 |
\isa{mult} and a proof for the specification of \isa{assoc}.%
|
|
178 |
\end{isamarkuptext}%
|
|
179 |
\isamarkuptrue%
|
|
180 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
|
181 |
\ int\ {\isacharcolon}{\isacharcolon}\ semigroup\isanewline
|
22317
|
182 |
\ \ \ \ \ \ mult{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymAnd}i\ j\ {\isacharcolon}{\isacharcolon}\ int{\isachardot}\ i\ {\isasymotimes}\ j\ {\isasymequiv}\ i\ {\isacharplus}\ j{\isachardoublequoteclose}\isanewline
|
20967
|
183 |
%
|
|
184 |
\isadelimproof
|
|
185 |
\ \ \ \ %
|
|
186 |
\endisadelimproof
|
|
187 |
%
|
|
188 |
\isatagproof
|
|
189 |
\isacommand{proof}\isamarkupfalse%
|
|
190 |
\isanewline
|
22317
|
191 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
20967
|
192 |
\ i\ j\ k\ {\isacharcolon}{\isacharcolon}\ int\ \isacommand{have}\isamarkupfalse%
|
|
193 |
\ {\isachardoublequoteopen}{\isacharparenleft}i\ {\isacharplus}\ j{\isacharparenright}\ {\isacharplus}\ k\ {\isacharequal}\ i\ {\isacharplus}\ {\isacharparenleft}j\ {\isacharplus}\ k{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
194 |
\ simp\isanewline
|
22317
|
195 |
\ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
|
20967
|
196 |
\ \isacommand{show}\isamarkupfalse%
|
|
197 |
\ {\isachardoublequoteopen}{\isacharparenleft}i\ {\isasymotimes}\ j{\isacharparenright}\ {\isasymotimes}\ k\ {\isacharequal}\ i\ {\isasymotimes}\ {\isacharparenleft}j\ {\isasymotimes}\ k{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{unfolding}\isamarkupfalse%
|
|
198 |
\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{{\isachardot}}\isamarkupfalse%
|
|
199 |
\isanewline
|
|
200 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
201 |
%
|
|
202 |
\endisatagproof
|
|
203 |
{\isafoldproof}%
|
|
204 |
%
|
|
205 |
\isadelimproof
|
|
206 |
%
|
|
207 |
\endisadelimproof
|
|
208 |
%
|
|
209 |
\begin{isamarkuptext}%
|
|
210 |
\noindent From now on, the type-checker will consider \isa{int}
|
|
211 |
as a \isa{semigroup} automatically, i.e.\ any general results
|
|
212 |
are immediately available on concrete instances.
|
|
213 |
|
22317
|
214 |
Note that the first proof step is the \isa{default} method,
|
|
215 |
which for instantiation proofs maps to the \isa{intro{\isacharunderscore}classes} method.
|
|
216 |
This boils down an instantiation judgement to the relevant primitive
|
|
217 |
proof goals and should conveniently always be the first method applied
|
|
218 |
in an instantiation proof.
|
|
219 |
|
20967
|
220 |
Another instance of \isa{semigroup} are the natural numbers:%
|
|
221 |
\end{isamarkuptext}%
|
|
222 |
\isamarkuptrue%
|
|
223 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
|
224 |
\ nat\ {\isacharcolon}{\isacharcolon}\ semigroup\isanewline
|
22317
|
225 |
\ \ \ \ \ \ mult{\isacharunderscore}nat{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymequiv}\ m\ {\isacharplus}\ n{\isachardoublequoteclose}\isanewline
|
20967
|
226 |
%
|
|
227 |
\isadelimproof
|
|
228 |
\ \ \ \ %
|
|
229 |
\endisadelimproof
|
|
230 |
%
|
|
231 |
\isatagproof
|
|
232 |
\isacommand{proof}\isamarkupfalse%
|
|
233 |
\isanewline
|
|
234 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
235 |
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\ \isanewline
|
|
236 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
237 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{unfolding}\isamarkupfalse%
|
22317
|
238 |
\ mult{\isacharunderscore}nat{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
20967
|
239 |
\ simp\isanewline
|
|
240 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
241 |
%
|
|
242 |
\endisatagproof
|
|
243 |
{\isafoldproof}%
|
|
244 |
%
|
|
245 |
\isadelimproof
|
|
246 |
%
|
|
247 |
\endisadelimproof
|
|
248 |
%
|
|
249 |
\begin{isamarkuptext}%
|
|
250 |
Also \isa{list}s form a semigroup with \isa{op\ {\isacharat}} as
|
|
251 |
operation:%
|
|
252 |
\end{isamarkuptext}%
|
|
253 |
\isamarkuptrue%
|
|
254 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
|
255 |
\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ semigroup\isanewline
|
22317
|
256 |
\ \ \ \ \ \ mult{\isacharunderscore}list{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}xs\ {\isasymotimes}\ ys\ {\isasymequiv}\ xs\ {\isacharat}\ ys{\isachardoublequoteclose}\isanewline
|
20967
|
257 |
%
|
|
258 |
\isadelimproof
|
|
259 |
\ \ \ \ %
|
|
260 |
\endisadelimproof
|
|
261 |
%
|
|
262 |
\isatagproof
|
|
263 |
\isacommand{proof}\isamarkupfalse%
|
|
264 |
\isanewline
|
|
265 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
266 |
\ xs\ ys\ zs\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ list{\isachardoublequoteclose}\isanewline
|
|
267 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
268 |
\ {\isachardoublequoteopen}xs\ {\isasymotimes}\ ys\ {\isasymotimes}\ zs\ {\isacharequal}\ xs\ {\isasymotimes}\ {\isacharparenleft}ys\ {\isasymotimes}\ zs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
269 |
\ \ \ \ \ \ \isacommand{proof}\isamarkupfalse%
|
|
270 |
\ {\isacharminus}\isanewline
|
|
271 |
\ \ \ \ \ \ \ \ \isacommand{from}\isamarkupfalse%
|
22317
|
272 |
\ mult{\isacharunderscore}list{\isacharunderscore}def\ \isacommand{have}\isamarkupfalse%
|
20967
|
273 |
\ {\isachardoublequoteopen}{\isasymAnd}xs\ ys{\isasymColon}{\isasymalpha}\ list{\isachardot}\ xs\ {\isasymotimes}\ ys\ {\isasymequiv}\ xs\ {\isacharat}\ ys{\isachardoublequoteclose}\ \isacommand{{\isachardot}}\isamarkupfalse%
|
|
274 |
\isanewline
|
|
275 |
\ \ \ \ \ \ \ \ \isacommand{thus}\isamarkupfalse%
|
|
276 |
\ {\isacharquery}thesis\ \isacommand{by}\isamarkupfalse%
|
|
277 |
\ simp\isanewline
|
|
278 |
\ \ \ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
279 |
\isanewline
|
|
280 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
281 |
%
|
|
282 |
\endisatagproof
|
|
283 |
{\isafoldproof}%
|
|
284 |
%
|
|
285 |
\isadelimproof
|
|
286 |
%
|
|
287 |
\endisadelimproof
|
|
288 |
%
|
|
289 |
\isamarkupsubsection{Subclasses%
|
|
290 |
}
|
|
291 |
\isamarkuptrue%
|
|
292 |
%
|
|
293 |
\begin{isamarkuptext}%
|
22317
|
294 |
We define a subclass \isa{monoidl} (a semigroup with a left-hand neutral)
|
20967
|
295 |
by extending \isa{semigroup}
|
|
296 |
with one additional operation \isa{neutral} together
|
|
297 |
with its property:%
|
|
298 |
\end{isamarkuptext}%
|
|
299 |
\isamarkuptrue%
|
|
300 |
\ \ \ \ \isacommand{class}\isamarkupfalse%
|
|
301 |
\ monoidl\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline
|
|
302 |
\ \ \ \ \ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}{\isachardoublequoteclose}\ {\isacharparenleft}{\isachardoublequoteopen}\isactrlloc {\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
303 |
\ \ \ \ \ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}\isactrlloc {\isasymone}\ \isactrlloc {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}%
|
|
304 |
\begin{isamarkuptext}%
|
|
305 |
\noindent Again, we make some instances, by
|
|
306 |
providing suitable operation definitions and proofs for the
|
|
307 |
additional specifications.%
|
|
308 |
\end{isamarkuptext}%
|
|
309 |
\isamarkuptrue%
|
|
310 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
|
311 |
\ nat\ {\isacharcolon}{\isacharcolon}\ monoidl\isanewline
|
22317
|
312 |
\ \ \ \ \ \ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymequiv}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
|
20967
|
313 |
%
|
|
314 |
\isadelimproof
|
|
315 |
\ \ \ \ %
|
|
316 |
\endisadelimproof
|
|
317 |
%
|
|
318 |
\isatagproof
|
|
319 |
\isacommand{proof}\isamarkupfalse%
|
|
320 |
\isanewline
|
|
321 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
322 |
\ n\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
|
|
323 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
324 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\ \isacommand{unfolding}\isamarkupfalse%
|
|
325 |
\ neutral{\isacharunderscore}nat{\isacharunderscore}def\ mult{\isacharunderscore}nat{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
326 |
\ simp\isanewline
|
|
327 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
328 |
%
|
|
329 |
\endisatagproof
|
|
330 |
{\isafoldproof}%
|
|
331 |
%
|
|
332 |
\isadelimproof
|
|
333 |
\isanewline
|
|
334 |
%
|
|
335 |
\endisadelimproof
|
|
336 |
\isanewline
|
|
337 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
|
338 |
\ int\ {\isacharcolon}{\isacharcolon}\ monoidl\isanewline
|
22317
|
339 |
\ \ \ \ \ \ neutral{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymequiv}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
|
20967
|
340 |
%
|
|
341 |
\isadelimproof
|
|
342 |
\ \ \ \ %
|
|
343 |
\endisadelimproof
|
|
344 |
%
|
|
345 |
\isatagproof
|
|
346 |
\isacommand{proof}\isamarkupfalse%
|
|
347 |
\isanewline
|
|
348 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
349 |
\ k\ {\isacharcolon}{\isacharcolon}\ int\isanewline
|
|
350 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
351 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ k\ {\isacharequal}\ k{\isachardoublequoteclose}\ \isacommand{unfolding}\isamarkupfalse%
|
|
352 |
\ neutral{\isacharunderscore}int{\isacharunderscore}def\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
353 |
\ simp\isanewline
|
|
354 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
355 |
%
|
|
356 |
\endisatagproof
|
|
357 |
{\isafoldproof}%
|
|
358 |
%
|
|
359 |
\isadelimproof
|
|
360 |
\isanewline
|
|
361 |
%
|
|
362 |
\endisadelimproof
|
|
363 |
\isanewline
|
|
364 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
|
365 |
\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ monoidl\isanewline
|
22317
|
366 |
\ \ \ \ \ \ neutral{\isacharunderscore}list{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
20967
|
367 |
%
|
|
368 |
\isadelimproof
|
|
369 |
\ \ \ \ %
|
|
370 |
\endisadelimproof
|
|
371 |
%
|
|
372 |
\isatagproof
|
|
373 |
\isacommand{proof}\isamarkupfalse%
|
|
374 |
\isanewline
|
|
375 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
376 |
\ xs\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ list{\isachardoublequoteclose}\isanewline
|
|
377 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
378 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ xs\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
|
|
379 |
\ \ \ \ \ \ \isacommand{proof}\isamarkupfalse%
|
|
380 |
\ {\isacharminus}\isanewline
|
|
381 |
\ \ \ \ \ \ \ \ \isacommand{from}\isamarkupfalse%
|
|
382 |
\ mult{\isacharunderscore}list{\isacharunderscore}def\ \isacommand{have}\isamarkupfalse%
|
|
383 |
\ {\isachardoublequoteopen}{\isasymAnd}xs\ ys{\isasymColon}{\isacharprime}a\ list{\isachardot}\ xs\ {\isasymotimes}\ ys\ {\isasymequiv}\ xs\ {\isacharat}\ ys{\isachardoublequoteclose}\ \isacommand{{\isachardot}}\isamarkupfalse%
|
|
384 |
\isanewline
|
|
385 |
\ \ \ \ \ \ \ \ \isacommand{moreover}\isamarkupfalse%
|
|
386 |
\ \isacommand{from}\isamarkupfalse%
|
|
387 |
\ mult{\isacharunderscore}list{\isacharunderscore}def\ neutral{\isacharunderscore}list{\isacharunderscore}def\ \isacommand{have}\isamarkupfalse%
|
|
388 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isasymColon}{\isasymalpha}\ list{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
389 |
\ simp\isanewline
|
|
390 |
\ \ \ \ \ \ \ \ \isacommand{ultimately}\isamarkupfalse%
|
|
391 |
\ \isacommand{show}\isamarkupfalse%
|
|
392 |
\ {\isacharquery}thesis\ \isacommand{by}\isamarkupfalse%
|
|
393 |
\ simp\isanewline
|
|
394 |
\ \ \ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
395 |
\isanewline
|
|
396 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
397 |
%
|
|
398 |
\endisatagproof
|
|
399 |
{\isafoldproof}%
|
|
400 |
%
|
|
401 |
\isadelimproof
|
|
402 |
%
|
|
403 |
\endisadelimproof
|
|
404 |
%
|
|
405 |
\begin{isamarkuptext}%
|
22317
|
406 |
\noindent Fully-fledged monoids are modelled by another subclass
|
|
407 |
which does not add new operations but tightens the specification:%
|
20967
|
408 |
\end{isamarkuptext}%
|
|
409 |
\isamarkuptrue%
|
|
410 |
\ \ \ \ \isacommand{class}\isamarkupfalse%
|
|
411 |
\ monoid\ {\isacharequal}\ monoidl\ {\isacharplus}\isanewline
|
22317
|
412 |
\ \ \ \ \ \ \isakeyword{assumes}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ \isactrlloc {\isasymotimes}\ \isactrlloc {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}%
|
|
413 |
\begin{isamarkuptext}%
|
|
414 |
\noindent Instantiations may also be given simultaneously for different
|
|
415 |
type constructors:%
|
|
416 |
\end{isamarkuptext}%
|
|
417 |
\isamarkuptrue%
|
20967
|
418 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
22317
|
419 |
\ nat\ {\isacharcolon}{\isacharcolon}\ monoid\ \isakeyword{and}\ int\ {\isacharcolon}{\isacharcolon}\ monoid\ \isakeyword{and}\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ monoid\isanewline
|
20967
|
420 |
%
|
|
421 |
\isadelimproof
|
|
422 |
\ \ \ \ %
|
|
423 |
\endisadelimproof
|
|
424 |
%
|
|
425 |
\isatagproof
|
|
426 |
\isacommand{proof}\isamarkupfalse%
|
|
427 |
\isanewline
|
|
428 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
429 |
\ n\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
|
|
430 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
431 |
\ {\isachardoublequoteopen}n\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ n{\isachardoublequoteclose}\ \isacommand{unfolding}\isamarkupfalse%
|
|
432 |
\ neutral{\isacharunderscore}nat{\isacharunderscore}def\ mult{\isacharunderscore}nat{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
433 |
\ simp\isanewline
|
22317
|
434 |
\ \ \ \ \isacommand{next}\isamarkupfalse%
|
20967
|
435 |
\isanewline
|
|
436 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
437 |
\ k\ {\isacharcolon}{\isacharcolon}\ int\isanewline
|
|
438 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
439 |
\ {\isachardoublequoteopen}k\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ k{\isachardoublequoteclose}\ \isacommand{unfolding}\isamarkupfalse%
|
|
440 |
\ neutral{\isacharunderscore}int{\isacharunderscore}def\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
441 |
\ simp\isanewline
|
22317
|
442 |
\ \ \ \ \isacommand{next}\isamarkupfalse%
|
20967
|
443 |
\isanewline
|
|
444 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
445 |
\ xs\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ list{\isachardoublequoteclose}\isanewline
|
|
446 |
\ \ \ \ \ \ \isacommand{show}\isamarkupfalse%
|
|
447 |
\ {\isachardoublequoteopen}xs\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
|
|
448 |
\ \ \ \ \ \ \isacommand{proof}\isamarkupfalse%
|
|
449 |
\ {\isacharminus}\isanewline
|
|
450 |
\ \ \ \ \ \ \ \ \isacommand{from}\isamarkupfalse%
|
|
451 |
\ mult{\isacharunderscore}list{\isacharunderscore}def\ \isacommand{have}\isamarkupfalse%
|
|
452 |
\ {\isachardoublequoteopen}{\isasymAnd}xs\ ys{\isasymColon}{\isasymalpha}\ list{\isachardot}\ xs\ {\isasymotimes}\ ys\ {\isasymequiv}\ xs\ {\isacharat}\ ys{\isachardoublequoteclose}\ \isacommand{{\isachardot}}\isamarkupfalse%
|
|
453 |
\isanewline
|
|
454 |
\ \ \ \ \ \ \ \ \isacommand{moreover}\isamarkupfalse%
|
|
455 |
\ \isacommand{from}\isamarkupfalse%
|
|
456 |
\ mult{\isacharunderscore}list{\isacharunderscore}def\ neutral{\isacharunderscore}list{\isacharunderscore}def\ \isacommand{have}\isamarkupfalse%
|
|
457 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isasymColon}{\isacharprime}a\ list{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
458 |
\ simp\isanewline
|
|
459 |
\ \ \ \ \ \ \ \ \isacommand{ultimately}\isamarkupfalse%
|
|
460 |
\ \isacommand{show}\isamarkupfalse%
|
|
461 |
\ {\isacharquery}thesis\ \isacommand{by}\isamarkupfalse%
|
|
462 |
\ simp\isanewline
|
|
463 |
\ \ \ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
464 |
\isanewline
|
|
465 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
466 |
%
|
|
467 |
\endisatagproof
|
|
468 |
{\isafoldproof}%
|
|
469 |
%
|
|
470 |
\isadelimproof
|
|
471 |
%
|
|
472 |
\endisadelimproof
|
22317
|
473 |
%
|
|
474 |
\begin{isamarkuptext}%
|
|
475 |
\noindent To finish our small algebra example, we add a \isa{group} class
|
|
476 |
with a corresponding instance:%
|
|
477 |
\end{isamarkuptext}%
|
|
478 |
\isamarkuptrue%
|
20967
|
479 |
\ \ \ \ \isacommand{class}\isamarkupfalse%
|
|
480 |
\ group\ {\isacharequal}\ monoidl\ {\isacharplus}\isanewline
|
|
481 |
\ \ \ \ \ \ \isakeyword{fixes}\ inverse\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \ \ \ {\isacharparenleft}{\isachardoublequoteopen}{\isacharparenleft}{\isacharunderscore}\isactrlloc {\isasymdiv}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbrackleft}{\isadigit{1}}{\isadigit{0}}{\isadigit{0}}{\isadigit{0}}{\isacharbrackright}\ {\isadigit{9}}{\isadigit{9}}{\isadigit{9}}{\isacharparenright}\isanewline
|
|
482 |
\ \ \ \ \ \ \isakeyword{assumes}\ invl{\isacharcolon}\ {\isachardoublequoteopen}x\isactrlloc {\isasymdiv}\ \isactrlloc {\isasymotimes}\ x\ {\isacharequal}\ \isactrlloc {\isasymone}{\isachardoublequoteclose}\isanewline
|
|
483 |
\isanewline
|
|
484 |
\ \ \ \ \isacommand{instance}\isamarkupfalse%
|
|
485 |
\ int\ {\isacharcolon}{\isacharcolon}\ group\isanewline
|
22317
|
486 |
\ \ \ \ \ \ inverse{\isacharunderscore}int{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequoteopen}i{\isasymdiv}\ {\isasymequiv}\ {\isacharminus}\ i{\isachardoublequoteclose}\isanewline
|
20967
|
487 |
%
|
|
488 |
\isadelimproof
|
|
489 |
\ \ \ \ %
|
|
490 |
\endisadelimproof
|
|
491 |
%
|
|
492 |
\isatagproof
|
|
493 |
\isacommand{proof}\isamarkupfalse%
|
|
494 |
\isanewline
|
|
495 |
\ \ \ \ \ \ \isacommand{fix}\isamarkupfalse%
|
|
496 |
\ i\ {\isacharcolon}{\isacharcolon}\ int\isanewline
|
|
497 |
\ \ \ \ \ \ \isacommand{have}\isamarkupfalse%
|
|
498 |
\ {\isachardoublequoteopen}{\isacharminus}i\ {\isacharplus}\ i\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
499 |
\ simp\isanewline
|
|
500 |
\ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
|
|
501 |
\ \isacommand{show}\isamarkupfalse%
|
|
502 |
\ {\isachardoublequoteopen}i{\isasymdiv}\ {\isasymotimes}\ i\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\ \isacommand{unfolding}\isamarkupfalse%
|
|
503 |
\ mult{\isacharunderscore}int{\isacharunderscore}def\ \isakeyword{and}\ neutral{\isacharunderscore}int{\isacharunderscore}def\ \isakeyword{and}\ inverse{\isacharunderscore}int{\isacharunderscore}def\ \isacommand{{\isachardot}}\isamarkupfalse%
|
|
504 |
\isanewline
|
|
505 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
506 |
%
|
|
507 |
\endisatagproof
|
|
508 |
{\isafoldproof}%
|
|
509 |
%
|
|
510 |
\isadelimproof
|
|
511 |
%
|
|
512 |
\endisadelimproof
|
|
513 |
%
|
22317
|
514 |
\isamarkupsection{Type classes as locales%
|
|
515 |
}
|
|
516 |
\isamarkuptrue%
|
|
517 |
%
|
|
518 |
\isamarkupsubsection{A look behind the scene%
|
|
519 |
}
|
|
520 |
\isamarkuptrue%
|
|
521 |
%
|
|
522 |
\begin{isamarkuptext}%
|
|
523 |
%
|
|
524 |
\end{isamarkuptext}%
|
|
525 |
\isamarkuptrue%
|
|
526 |
%
|
20967
|
527 |
\isamarkupsubsection{Abstract reasoning%
|
|
528 |
}
|
|
529 |
\isamarkuptrue%
|
|
530 |
%
|
|
531 |
\begin{isamarkuptext}%
|
|
532 |
Abstract theories enable reasoning at a general level, while results
|
|
533 |
are implicitly transferred to all instances. For example, we can
|
|
534 |
now establish the \isa{left{\isacharunderscore}cancel} lemma for groups, which
|
|
535 |
states that the function \isa{{\isacharparenleft}x\ {\isasymcirc}{\isacharparenright}} is injective:%
|
|
536 |
\end{isamarkuptext}%
|
|
537 |
\isamarkuptrue%
|
|
538 |
\ \ \ \ \isacommand{lemma}\isamarkupfalse%
|
|
539 |
\ {\isacharparenleft}\isakeyword{in}\ group{\isacharparenright}\ left{\isacharunderscore}cancel{\isacharcolon}\ {\isachardoublequoteopen}x\ \isactrlloc {\isasymotimes}\ y\ {\isacharequal}\ x\ \isactrlloc {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequoteclose}\isanewline
|
|
540 |
%
|
|
541 |
\isadelimproof
|
|
542 |
\ \ \ \ %
|
|
543 |
\endisadelimproof
|
|
544 |
%
|
|
545 |
\isatagproof
|
|
546 |
\isacommand{proof}\isamarkupfalse%
|
|
547 |
\isanewline
|
|
548 |
\ \ \ \ \isacommand{assume}\isamarkupfalse%
|
|
549 |
\ {\isachardoublequoteopen}x\ \isactrlloc {\isasymotimes}\ y\ {\isacharequal}\ x\ \isactrlloc {\isasymotimes}\ z{\isachardoublequoteclose}\isanewline
|
|
550 |
\ \ \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
|
|
551 |
\ \isacommand{have}\isamarkupfalse%
|
|
552 |
\ {\isachardoublequoteopen}x\isactrlloc {\isasymdiv}\ \isactrlloc {\isasymotimes}\ {\isacharparenleft}x\ \isactrlloc {\isasymotimes}\ y{\isacharparenright}\ {\isacharequal}\ x\isactrlloc {\isasymdiv}\ \isactrlloc {\isasymotimes}\ {\isacharparenleft}x\ \isactrlloc {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
553 |
\ simp\isanewline
|
|
554 |
\ \ \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
|
|
555 |
\ \isacommand{have}\isamarkupfalse%
|
|
556 |
\ {\isachardoublequoteopen}{\isacharparenleft}x\isactrlloc {\isasymdiv}\ \isactrlloc {\isasymotimes}\ x{\isacharparenright}\ \isactrlloc {\isasymotimes}\ y\ {\isacharequal}\ {\isacharparenleft}x\isactrlloc {\isasymdiv}\ \isactrlloc {\isasymotimes}\ x{\isacharparenright}\ \isactrlloc {\isasymotimes}\ z{\isachardoublequoteclose}\ \isacommand{using}\isamarkupfalse%
|
|
557 |
\ assoc\ \isacommand{by}\isamarkupfalse%
|
|
558 |
\ simp\isanewline
|
|
559 |
\ \ \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
|
|
560 |
\ \isacommand{show}\isamarkupfalse%
|
|
561 |
\ {\isachardoublequoteopen}y\ {\isacharequal}\ z{\isachardoublequoteclose}\ \isacommand{using}\isamarkupfalse%
|
|
562 |
\ neutl\ \isakeyword{and}\ invl\ \isacommand{by}\isamarkupfalse%
|
|
563 |
\ simp\isanewline
|
|
564 |
\ \ \ \ \isacommand{next}\isamarkupfalse%
|
|
565 |
\isanewline
|
|
566 |
\ \ \ \ \isacommand{assume}\isamarkupfalse%
|
|
567 |
\ {\isachardoublequoteopen}y\ {\isacharequal}\ z{\isachardoublequoteclose}\isanewline
|
|
568 |
\ \ \ \ \ \ \ \ \isacommand{then}\isamarkupfalse%
|
|
569 |
\ \isacommand{show}\isamarkupfalse%
|
|
570 |
\ {\isachardoublequoteopen}x\ \isactrlloc {\isasymotimes}\ y\ {\isacharequal}\ x\ \isactrlloc {\isasymotimes}\ z{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
|
|
571 |
\ simp\isanewline
|
|
572 |
\ \ \ \ \isacommand{qed}\isamarkupfalse%
|
|
573 |
%
|
|
574 |
\endisatagproof
|
|
575 |
{\isafoldproof}%
|
|
576 |
%
|
|
577 |
\isadelimproof
|
|
578 |
%
|
|
579 |
\endisadelimproof
|
|
580 |
%
|
|
581 |
\begin{isamarkuptext}%
|
|
582 |
\noindent Here the \qt{\isa{{\isasymIN}\ group}} target specification
|
|
583 |
indicates that the result is recorded within that context for later
|
|
584 |
use. This local theorem is also lifted to the global one \isa{group{\isachardot}left{\isacharunderscore}cancel{\isacharcolon}} \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z{\isacharcolon}{\isacharcolon}{\isasymalpha}{\isacharcolon}{\isacharcolon}group{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}. Since type \isa{int} has been made an instance of
|
|
585 |
\isa{group} before, we may refer to that fact as well: \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z{\isacharcolon}{\isacharcolon}int{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}.%
|
|
586 |
\end{isamarkuptext}%
|
|
587 |
\isamarkuptrue%
|
|
588 |
%
|
22317
|
589 |
\isamarkupsection{Further issues%
|
20967
|
590 |
}
|
|
591 |
\isamarkuptrue%
|
|
592 |
%
|
22317
|
593 |
\isamarkupsubsection{Code generation%
|
20967
|
594 |
}
|
|
595 |
\isamarkuptrue%
|
|
596 |
%
|
|
597 |
\begin{isamarkuptext}%
|
22317
|
598 |
Turning back to the first motivation for type classes,
|
|
599 |
namely overloading, it is obvious that overloading
|
|
600 |
stemming from \isa{{\isasymCLASS}} and \isa{{\isasymINSTANCE}}
|
|
601 |
statements naturally maps to Haskell type classes.
|
|
602 |
The code generator framework \cite{isabelle-codegen}
|
|
603 |
takes this into account. Concerning target languages
|
|
604 |
lacking type classes (e.g.~SML), type classes
|
|
605 |
are implemented by explicit dictionary construction.
|
|
606 |
As example, the natural power function on groups:%
|
20967
|
607 |
\end{isamarkuptext}%
|
|
608 |
\isamarkuptrue%
|
22317
|
609 |
\ \ \ \ \isacommand{fun}\isamarkupfalse%
|
20967
|
610 |
\isanewline
|
|
611 |
\ \ \ \ \ \ pow{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharprime}a{\isasymColon}monoidl\ {\isasymRightarrow}\ {\isacharprime}a{\isasymColon}monoidl{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
612 |
\ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
|
|
613 |
\ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x{\isachardoublequoteclose}\isanewline
|
|
614 |
\isanewline
|
|
615 |
\ \ \ \ \isacommand{definition}\isamarkupfalse%
|
|
616 |
\isanewline
|
22317
|
617 |
\ \ \ \ \ \ pow{\isacharunderscore}int\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}int\ {\isasymRightarrow}\ {\isacharprime}a{\isasymColon}group\ {\isasymRightarrow}\ {\isacharprime}a{\isasymColon}group{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
20967
|
618 |
\ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}int\ k\ x\ {\isacharequal}\ {\isacharparenleft}if\ k\ {\isachargreater}{\isacharequal}\ {\isadigit{0}}\isanewline
|
|
619 |
\ \ \ \ \ \ \ \ then\ pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ k{\isacharparenright}\ x\isanewline
|
|
620 |
\ \ \ \ \ \ \ \ else\ {\isacharparenleft}pow{\isacharunderscore}nat\ {\isacharparenleft}nat\ {\isacharparenleft}{\isacharminus}\ k{\isacharparenright}{\isacharparenright}\ x{\isacharparenright}{\isasymdiv}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
621 |
\isanewline
|
|
622 |
\ \ \ \ \isacommand{definition}\isamarkupfalse%
|
|
623 |
\isanewline
|
22317
|
624 |
\ \ \ \ \ \ example\ {\isacharcolon}{\isacharcolon}\ int\ \isakeyword{where}\isanewline
|
20967
|
625 |
\ \ \ \ \ \ {\isachardoublequoteopen}example\ {\isacharequal}\ pow{\isacharunderscore}int\ {\isadigit{1}}{\isadigit{0}}\ {\isacharparenleft}{\isacharminus}{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}%
|
|
626 |
\begin{isamarkuptext}%
|
22317
|
627 |
\noindent This maps to Haskell as:%
|
20967
|
628 |
\end{isamarkuptext}%
|
|
629 |
\isamarkuptrue%
|
22317
|
630 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
631 |
\ example\ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}{\isachardoublequoteclose}{\isacharparenright}%
|
20967
|
632 |
\begin{isamarkuptext}%
|
22317
|
633 |
\lsthaskell{Thy/code_examples/Classes.hs}
|
|
634 |
|
|
635 |
\noindent (we have left out all other modules).
|
|
636 |
|
|
637 |
\noindent The whole code in SML with explicit dictionary passing:%
|
|
638 |
\end{isamarkuptext}%
|
|
639 |
\isamarkuptrue%
|
|
640 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
641 |
\ example\ {\isacharparenleft}SML\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}classes{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
642 |
\begin{isamarkuptext}%
|
|
643 |
\lstsml{Thy/code_examples/classes.ML}%
|
20967
|
644 |
\end{isamarkuptext}%
|
|
645 |
\isamarkuptrue%
|
|
646 |
%
|
|
647 |
\isadelimtheory
|
|
648 |
%
|
|
649 |
\endisadelimtheory
|
|
650 |
%
|
|
651 |
\isatagtheory
|
|
652 |
\isacommand{end}\isamarkupfalse%
|
|
653 |
%
|
|
654 |
\endisatagtheory
|
|
655 |
{\isafoldtheory}%
|
|
656 |
%
|
|
657 |
\isadelimtheory
|
|
658 |
%
|
|
659 |
\endisadelimtheory
|
|
660 |
\isanewline
|
|
661 |
\end{isabellebody}%
|
|
662 |
%%% Local Variables:
|
|
663 |
%%% mode: latex
|
|
664 |
%%% TeX-master: "root"
|
|
665 |
%%% End:
|