20946
|
1 |
|
|
2 |
(* $Id$ *)
|
|
3 |
|
22317
|
4 |
(*<*)
|
20946
|
5 |
theory Classes
|
23956
|
6 |
imports Main Pretty_Int
|
20946
|
7 |
begin
|
|
8 |
|
22317
|
9 |
ML {*
|
|
10 |
CodegenSerializer.code_width := 74;
|
|
11 |
*}
|
|
12 |
|
20946
|
13 |
syntax
|
|
14 |
"_alpha" :: "type" ("\<alpha>")
|
22479
|
15 |
"_alpha_ofsort" :: "sort \<Rightarrow> type" ("\<alpha>()\<Colon>_" [0] 1000)
|
20946
|
16 |
"_beta" :: "type" ("\<beta>")
|
22479
|
17 |
"_beta_ofsort" :: "sort \<Rightarrow> type" ("\<beta>()\<Colon>_" [0] 1000)
|
20946
|
18 |
"_gamma" :: "type" ("\<gamma>")
|
22479
|
19 |
"_gamma_ofsort" :: "sort \<Rightarrow> type" ("\<gamma>()\<Colon>_" [0] 1000)
|
20946
|
20 |
"_alpha_f" :: "type" ("\<alpha>\<^sub>f")
|
22479
|
21 |
"_alpha_f_ofsort" :: "sort \<Rightarrow> type" ("\<alpha>\<^sub>f()\<Colon>_" [0] 1000)
|
20946
|
22 |
"_beta_f" :: "type" ("\<beta>\<^sub>f")
|
22479
|
23 |
"_beta_f_ofsort" :: "sort \<Rightarrow> type" ("\<beta>\<^sub>f()\<Colon>_" [0] 1000)
|
20946
|
24 |
"_gamma_f" :: "type" ("\<gamma>\<^sub>f")
|
22479
|
25 |
"_gamma_ofsort_f" :: "sort \<Rightarrow> type" ("\<gamma>\<^sub>f()\<Colon>_" [0] 1000)
|
20946
|
26 |
|
|
27 |
parse_ast_translation {*
|
|
28 |
let
|
|
29 |
fun alpha_ast_tr [] = Syntax.Variable "'a"
|
|
30 |
| alpha_ast_tr asts = raise Syntax.AST ("alpha_ast_tr", asts);
|
|
31 |
fun alpha_ofsort_ast_tr [ast] =
|
|
32 |
Syntax.Appl [Syntax.Constant "_ofsort", Syntax.Variable "'a", ast]
|
|
33 |
| alpha_ofsort_ast_tr asts = raise Syntax.AST ("alpha_ast_tr", asts);
|
|
34 |
fun beta_ast_tr [] = Syntax.Variable "'b"
|
|
35 |
| beta_ast_tr asts = raise Syntax.AST ("beta_ast_tr", asts);
|
|
36 |
fun beta_ofsort_ast_tr [ast] =
|
|
37 |
Syntax.Appl [Syntax.Constant "_ofsort", Syntax.Variable "'b", ast]
|
|
38 |
| beta_ofsort_ast_tr asts = raise Syntax.AST ("beta_ast_tr", asts);
|
|
39 |
fun gamma_ast_tr [] = Syntax.Variable "'c"
|
|
40 |
| gamma_ast_tr asts = raise Syntax.AST ("gamma_ast_tr", asts);
|
|
41 |
fun gamma_ofsort_ast_tr [ast] =
|
|
42 |
Syntax.Appl [Syntax.Constant "_ofsort", Syntax.Variable "'c", ast]
|
|
43 |
| gamma_ofsort_ast_tr asts = raise Syntax.AST ("gamma_ast_tr", asts);
|
|
44 |
fun alpha_f_ast_tr [] = Syntax.Variable "'a_f"
|
|
45 |
| alpha_f_ast_tr asts = raise Syntax.AST ("alpha_f_ast_tr", asts);
|
|
46 |
fun alpha_f_ofsort_ast_tr [ast] =
|
|
47 |
Syntax.Appl [Syntax.Constant "_ofsort", Syntax.Variable "'a_f", ast]
|
|
48 |
| alpha_f_ofsort_ast_tr asts = raise Syntax.AST ("alpha_f_ast_tr", asts);
|
|
49 |
fun beta_f_ast_tr [] = Syntax.Variable "'b_f"
|
|
50 |
| beta_f_ast_tr asts = raise Syntax.AST ("beta_f_ast_tr", asts);
|
|
51 |
fun beta_f_ofsort_ast_tr [ast] =
|
|
52 |
Syntax.Appl [Syntax.Constant "_ofsort", Syntax.Variable "'b_f", ast]
|
|
53 |
| beta_f_ofsort_ast_tr asts = raise Syntax.AST ("beta_f_ast_tr", asts);
|
|
54 |
fun gamma_f_ast_tr [] = Syntax.Variable "'c_f"
|
|
55 |
| gamma_f_ast_tr asts = raise Syntax.AST ("gamma_f_ast_tr", asts);
|
|
56 |
fun gamma_f_ofsort_ast_tr [ast] =
|
|
57 |
Syntax.Appl [Syntax.Constant "_ofsort", Syntax.Variable "'c_f", ast]
|
|
58 |
| gamma_f_ofsort_ast_tr asts = raise Syntax.AST ("gamma_f_ast_tr", asts);
|
|
59 |
in [
|
|
60 |
("_alpha", alpha_ast_tr), ("_alpha_ofsort", alpha_ofsort_ast_tr),
|
|
61 |
("_beta", beta_ast_tr), ("_beta_ofsort", beta_ofsort_ast_tr),
|
|
62 |
("_gamma", gamma_ast_tr), ("_gamma_ofsort", gamma_ofsort_ast_tr),
|
|
63 |
("_alpha_f", alpha_f_ast_tr), ("_alpha_f_ofsort", alpha_f_ofsort_ast_tr),
|
|
64 |
("_beta_f", beta_f_ast_tr), ("_beta_f_ofsort", beta_f_ofsort_ast_tr),
|
|
65 |
("_gamma_f", gamma_f_ast_tr), ("_gamma_f_ofsort", gamma_f_ofsort_ast_tr)
|
|
66 |
] end
|
|
67 |
*}
|
|
68 |
(*>*)
|
|
69 |
|
|
70 |
|
|
71 |
chapter {* Haskell-style classes with Isabelle/Isar *}
|
|
72 |
|
|
73 |
section {* Introduction *}
|
|
74 |
|
|
75 |
text {*
|
22317
|
76 |
Type classes were introduces by Wadler and Blott \cite{wadler89how}
|
|
77 |
into the Haskell language, to allow for a reasonable implementation
|
|
78 |
of overloading\footnote{throughout this tutorial, we are referring
|
|
79 |
to classical Haskell 1.0 type classes, not considering
|
|
80 |
later additions in expressiveness}.
|
|
81 |
As a canonical example, a polymorphic equality function
|
|
82 |
@{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} which is overloaded on different
|
22550
|
83 |
types for @{text "\<alpha>"}, which is achieved by splitting introduction
|
22317
|
84 |
of the @{text eq} function from its overloaded definitions by means
|
|
85 |
of @{text class} and @{text instance} declarations:
|
|
86 |
|
|
87 |
\medskip\noindent\hspace*{2ex}@{text "class eq where"}\footnote{syntax here is a kind of isabellized Haskell} \\
|
|
88 |
\hspace*{4ex}@{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"}
|
20946
|
89 |
|
22317
|
90 |
\medskip\noindent\hspace*{2ex}@{text "instance nat \<Colon> eq where"} \\
|
|
91 |
\hspace*{4ex}@{text "eq 0 0 = True"} \\
|
|
92 |
\hspace*{4ex}@{text "eq 0 _ = False"} \\
|
|
93 |
\hspace*{4ex}@{text "eq _ 0 = False"} \\
|
|
94 |
\hspace*{4ex}@{text "eq (Suc n) (Suc m) = eq n m"}
|
|
95 |
|
|
96 |
\medskip\noindent\hspace*{2ex}@{text "instance (\<alpha>\<Colon>eq, \<beta>\<Colon>eq) pair \<Colon> eq where"} \\
|
22550
|
97 |
\hspace*{4ex}@{text "eq (x1, y1) (x2, y2) = eq x1 x2 \<and> eq y1 y2"}
|
20946
|
98 |
|
22317
|
99 |
\medskip\noindent\hspace*{2ex}@{text "class ord extends eq where"} \\
|
|
100 |
\hspace*{4ex}@{text "less_eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} \\
|
|
101 |
\hspace*{4ex}@{text "less \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"}
|
|
102 |
|
22550
|
103 |
\medskip\noindent Type variables are annotated with (finitly many) classes;
|
22317
|
104 |
these annotations are assertions that a particular polymorphic type
|
|
105 |
provides definitions for overloaded functions.
|
|
106 |
|
|
107 |
Indeed, type classes not only allow for simple overloading
|
|
108 |
but form a generic calculus, an instance of order-sorted
|
23956
|
109 |
algebra \cite{Nipkow-Prehofer:1993,nipkow-sorts93,Wenzel:1997:TPHOL}.
|
20946
|
110 |
|
22317
|
111 |
From a software enigineering point of view, type classes
|
|
112 |
correspond to interfaces in object-oriented languages like Java;
|
|
113 |
so, it is naturally desirable that type classes do not only
|
|
114 |
provide functions (class operations) but also state specifications
|
|
115 |
implementations must obey. For example, the @{text "class eq"}
|
22550
|
116 |
above could be given the following specification, demanding that
|
|
117 |
@{text "class eq"} is an equivalence relation obeying reflexivity,
|
|
118 |
symmetry and transitivity:
|
22317
|
119 |
|
|
120 |
\medskip\noindent\hspace*{2ex}@{text "class eq where"} \\
|
|
121 |
\hspace*{4ex}@{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} \\
|
|
122 |
\hspace*{2ex}@{text "satisfying"} \\
|
|
123 |
\hspace*{4ex}@{text "refl: eq x x"} \\
|
|
124 |
\hspace*{4ex}@{text "sym: eq x y \<longleftrightarrow> eq x y"} \\
|
|
125 |
\hspace*{4ex}@{text "trans: eq x y \<and> eq y z \<longrightarrow> eq x z"}
|
|
126 |
|
22550
|
127 |
\medskip\noindent From a theoretic point of view, type classes are leightweight
|
22347
|
128 |
modules; Haskell type classes may be emulated by
|
22317
|
129 |
SML functors \cite{classes_modules}.
|
|
130 |
Isabelle/Isar offers a discipline of type classes which brings
|
|
131 |
all those aspects together:
|
|
132 |
|
|
133 |
\begin{enumerate}
|
|
134 |
\item specifying abstract operations togehter with
|
|
135 |
corresponding specifications,
|
|
136 |
\item instantating those abstract operations by a particular
|
|
137 |
type
|
|
138 |
\item in connection with a ``less ad-hoc'' approach to overloading,
|
23956
|
139 |
\item with a direct link to the Isabelle module system
|
|
140 |
(aka locales \cite{kammueller-locales}).
|
22317
|
141 |
\end{enumerate}
|
|
142 |
|
22550
|
143 |
\noindent Isar type classes also directly support code generation
|
|
144 |
in a Haskell like fashion.
|
22317
|
145 |
|
|
146 |
This tutorial demonstrates common elements of structured specifications
|
|
147 |
and abstract reasoning with type classes by the algebraic hierarchy of
|
20946
|
148 |
semigroups, monoids and groups. Our background theory is that of
|
23956
|
149 |
Isabelle/HOL \cite{isa-tutorial}, for which some
|
22317
|
150 |
familiarity is assumed.
|
20946
|
151 |
|
22317
|
152 |
Here we merely present the look-and-feel for end users.
|
20946
|
153 |
Internally, those are mapped to more primitive Isabelle concepts.
|
23956
|
154 |
See \cite{Haftmann-Wenzel:2006:classes} for more detail.
|
20946
|
155 |
*}
|
|
156 |
|
22317
|
157 |
section {* A simple algebra example \label{sec:example} *}
|
20946
|
158 |
|
|
159 |
subsection {* Class definition *}
|
|
160 |
|
|
161 |
text {*
|
|
162 |
Depending on an arbitrary type @{text "\<alpha>"}, class @{text
|
|
163 |
"semigroup"} introduces a binary operation @{text "\<circ>"} that is
|
|
164 |
assumed to be associative:
|
|
165 |
*}
|
|
166 |
|
22473
|
167 |
class semigroup = type +
|
20946
|
168 |
fixes mult :: "\<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" (infixl "\<^loc>\<otimes>" 70)
|
|
169 |
assumes assoc: "(x \<^loc>\<otimes> y) \<^loc>\<otimes> z = x \<^loc>\<otimes> (y \<^loc>\<otimes> z)"
|
|
170 |
|
|
171 |
text {*
|
|
172 |
\noindent This @{text "\<CLASS>"} specification consists of two
|
|
173 |
parts: the \qn{operational} part names the class operation (@{text
|
|
174 |
"\<FIXES>"}), the \qn{logical} part specifies properties on them
|
|
175 |
(@{text "\<ASSUMES>"}). The local @{text "\<FIXES>"} and @{text
|
|
176 |
"\<ASSUMES>"} are lifted to the theory toplevel, yielding the global
|
22479
|
177 |
operation @{term [source] "mult \<Colon> \<alpha>\<Colon>semigroup \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"} and the
|
20946
|
178 |
global theorem @{text "semigroup.assoc:"}~@{prop [source] "\<And>x y
|
22479
|
179 |
z \<Colon> \<alpha>\<Colon>semigroup. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"}.
|
20946
|
180 |
*}
|
|
181 |
|
|
182 |
|
|
183 |
subsection {* Class instantiation \label{sec:class_inst} *}
|
|
184 |
|
|
185 |
text {*
|
|
186 |
The concrete type @{text "int"} is made a @{text "semigroup"}
|
|
187 |
instance by providing a suitable definition for the class operation
|
|
188 |
@{text "mult"} and a proof for the specification of @{text "assoc"}.
|
|
189 |
*}
|
|
190 |
|
|
191 |
instance int :: semigroup
|
22479
|
192 |
mult_int_def: "\<And>i j \<Colon> int. i \<otimes> j \<equiv> i + j"
|
20946
|
193 |
proof
|
22317
|
194 |
fix i j k :: int have "(i + j) + k = i + (j + k)" by simp
|
|
195 |
then show "(i \<otimes> j) \<otimes> k = i \<otimes> (j \<otimes> k)" unfolding mult_int_def .
|
20946
|
196 |
qed
|
|
197 |
|
|
198 |
text {*
|
|
199 |
\noindent From now on, the type-checker will consider @{text "int"}
|
|
200 |
as a @{text "semigroup"} automatically, i.e.\ any general results
|
|
201 |
are immediately available on concrete instances.
|
|
202 |
|
22317
|
203 |
Note that the first proof step is the @{text default} method,
|
|
204 |
which for instantiation proofs maps to the @{text intro_classes} method.
|
|
205 |
This boils down an instantiation judgement to the relevant primitive
|
|
206 |
proof goals and should conveniently always be the first method applied
|
|
207 |
in an instantiation proof.
|
|
208 |
|
22550
|
209 |
\medskip Another instance of @{text "semigroup"} are the natural numbers:
|
20946
|
210 |
*}
|
|
211 |
|
|
212 |
instance nat :: semigroup
|
22317
|
213 |
mult_nat_def: "m \<otimes> n \<equiv> m + n"
|
20946
|
214 |
proof
|
|
215 |
fix m n q :: nat
|
22317
|
216 |
show "m \<otimes> n \<otimes> q = m \<otimes> (n \<otimes> q)" unfolding mult_nat_def by simp
|
20946
|
217 |
qed
|
|
218 |
|
|
219 |
text {*
|
22550
|
220 |
\noindent Also @{text "list"}s form a semigroup with @{const "op @"} as
|
20946
|
221 |
operation:
|
|
222 |
*}
|
|
223 |
|
|
224 |
instance list :: (type) semigroup
|
22317
|
225 |
mult_list_def: "xs \<otimes> ys \<equiv> xs @ ys"
|
20946
|
226 |
proof
|
|
227 |
fix xs ys zs :: "\<alpha> list"
|
|
228 |
show "xs \<otimes> ys \<otimes> zs = xs \<otimes> (ys \<otimes> zs)"
|
|
229 |
proof -
|
22317
|
230 |
from mult_list_def have "\<And>xs ys\<Colon>\<alpha> list. xs \<otimes> ys \<equiv> xs @ ys" .
|
20946
|
231 |
thus ?thesis by simp
|
|
232 |
qed
|
|
233 |
qed
|
|
234 |
|
|
235 |
|
|
236 |
subsection {* Subclasses *}
|
|
237 |
|
|
238 |
text {*
|
22317
|
239 |
We define a subclass @{text "monoidl"} (a semigroup with a left-hand neutral)
|
20946
|
240 |
by extending @{text "semigroup"}
|
|
241 |
with one additional operation @{text "neutral"} together
|
|
242 |
with its property:
|
|
243 |
*}
|
|
244 |
|
|
245 |
class monoidl = semigroup +
|
|
246 |
fixes neutral :: "\<alpha>" ("\<^loc>\<one>")
|
|
247 |
assumes neutl: "\<^loc>\<one> \<^loc>\<otimes> x = x"
|
|
248 |
|
|
249 |
text {*
|
|
250 |
\noindent Again, we make some instances, by
|
|
251 |
providing suitable operation definitions and proofs for the
|
|
252 |
additional specifications.
|
|
253 |
*}
|
|
254 |
|
|
255 |
instance nat :: monoidl
|
22317
|
256 |
neutral_nat_def: "\<one> \<equiv> 0"
|
20946
|
257 |
proof
|
|
258 |
fix n :: nat
|
|
259 |
show "\<one> \<otimes> n = n" unfolding neutral_nat_def mult_nat_def by simp
|
|
260 |
qed
|
|
261 |
|
|
262 |
instance int :: monoidl
|
22317
|
263 |
neutral_int_def: "\<one> \<equiv> 0"
|
20946
|
264 |
proof
|
|
265 |
fix k :: int
|
|
266 |
show "\<one> \<otimes> k = k" unfolding neutral_int_def mult_int_def by simp
|
|
267 |
qed
|
|
268 |
|
|
269 |
instance list :: (type) monoidl
|
22317
|
270 |
neutral_list_def: "\<one> \<equiv> []"
|
20946
|
271 |
proof
|
|
272 |
fix xs :: "\<alpha> list"
|
|
273 |
show "\<one> \<otimes> xs = xs"
|
|
274 |
proof -
|
22347
|
275 |
from mult_list_def have "\<And>xs ys\<Colon>\<alpha> list. xs \<otimes> ys \<equiv> xs @ ys" .
|
20946
|
276 |
moreover from mult_list_def neutral_list_def have "\<one> \<equiv> []\<Colon>\<alpha> list" by simp
|
|
277 |
ultimately show ?thesis by simp
|
|
278 |
qed
|
|
279 |
qed
|
|
280 |
|
|
281 |
text {*
|
22317
|
282 |
\noindent Fully-fledged monoids are modelled by another subclass
|
|
283 |
which does not add new operations but tightens the specification:
|
20946
|
284 |
*}
|
|
285 |
|
|
286 |
class monoid = monoidl +
|
|
287 |
assumes neutr: "x \<^loc>\<otimes> \<^loc>\<one> = x"
|
|
288 |
|
22317
|
289 |
text {*
|
|
290 |
\noindent Instantiations may also be given simultaneously for different
|
|
291 |
type constructors:
|
|
292 |
*}
|
|
293 |
|
|
294 |
instance nat :: monoid and int :: monoid and list :: (type) monoid
|
20946
|
295 |
proof
|
|
296 |
fix n :: nat
|
|
297 |
show "n \<otimes> \<one> = n" unfolding neutral_nat_def mult_nat_def by simp
|
22317
|
298 |
next
|
20946
|
299 |
fix k :: int
|
|
300 |
show "k \<otimes> \<one> = k" unfolding neutral_int_def mult_int_def by simp
|
22317
|
301 |
next
|
20946
|
302 |
fix xs :: "\<alpha> list"
|
|
303 |
show "xs \<otimes> \<one> = xs"
|
|
304 |
proof -
|
|
305 |
from mult_list_def have "\<And>xs ys\<Colon>\<alpha> list. xs \<otimes> ys \<equiv> xs @ ys" .
|
22347
|
306 |
moreover from mult_list_def neutral_list_def have "\<one> \<equiv> []\<Colon>\<alpha> list" by simp
|
20946
|
307 |
ultimately show ?thesis by simp
|
|
308 |
qed
|
22317
|
309 |
qed
|
|
310 |
|
|
311 |
text {*
|
|
312 |
\noindent To finish our small algebra example, we add a @{text "group"} class
|
|
313 |
with a corresponding instance:
|
|
314 |
*}
|
20946
|
315 |
|
|
316 |
class group = monoidl +
|
|
317 |
fixes inverse :: "\<alpha> \<Rightarrow> \<alpha>" ("(_\<^loc>\<div>)" [1000] 999)
|
|
318 |
assumes invl: "x\<^loc>\<div> \<^loc>\<otimes> x = \<^loc>\<one>"
|
|
319 |
|
|
320 |
instance int :: group
|
22317
|
321 |
inverse_int_def: "i\<div> \<equiv> - i"
|
20946
|
322 |
proof
|
|
323 |
fix i :: int
|
|
324 |
have "-i + i = 0" by simp
|
22550
|
325 |
then show "i\<div> \<otimes> i = \<one>"
|
|
326 |
unfolding mult_int_def and neutral_int_def and inverse_int_def .
|
20946
|
327 |
qed
|
|
328 |
|
22317
|
329 |
section {* Type classes as locales *}
|
|
330 |
|
|
331 |
subsection {* A look behind the scene *}
|
|
332 |
|
22347
|
333 |
text {*
|
|
334 |
The example above gives an impression how Isar type classes work
|
|
335 |
in practice. As stated in the introduction, classes also provide
|
|
336 |
a link to Isar's locale system. Indeed, the logical core of a class
|
|
337 |
is nothing else than a locale:
|
|
338 |
*}
|
|
339 |
|
22473
|
340 |
class idem = type +
|
22347
|
341 |
fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
|
|
342 |
assumes idem: "f (f x) = f x"
|
22317
|
343 |
|
|
344 |
text {*
|
22347
|
345 |
\noindent essentially introduces the locale
|
|
346 |
*}
|
|
347 |
(*<*) setup {* Sign.add_path "foo" *} (*>*)
|
|
348 |
locale idem =
|
|
349 |
fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
|
|
350 |
assumes idem: "f (f x) = f x"
|
|
351 |
|
22550
|
352 |
text {* \noindent together with corresponding constant(s): *}
|
22347
|
353 |
|
|
354 |
consts f :: "\<alpha> \<Rightarrow> \<alpha>"
|
|
355 |
|
22550
|
356 |
text {*
|
|
357 |
\noindent The connection to the type system is done by means
|
|
358 |
of a primitive axclass
|
|
359 |
*}
|
|
360 |
|
22347
|
361 |
axclass idem < type
|
|
362 |
idem: "f (f x) = f x"
|
|
363 |
|
22550
|
364 |
text {* \noindent together with a corresponding interpretation: *}
|
22347
|
365 |
|
|
366 |
interpretation idem_class:
|
|
367 |
idem ["f \<Colon> ('a\<Colon>idem) \<Rightarrow> \<alpha>"]
|
|
368 |
by unfold_locales (rule idem)
|
|
369 |
(*<*) setup {* Sign.parent_path *} (*>*)
|
|
370 |
text {*
|
|
371 |
This give you at hand the full power of the Isabelle module system;
|
|
372 |
conclusions in locale @{text idem} are implicitly propagated
|
22479
|
373 |
to class @{text idem}.
|
22317
|
374 |
*}
|
20946
|
375 |
|
|
376 |
subsection {* Abstract reasoning *}
|
|
377 |
|
|
378 |
text {*
|
22347
|
379 |
Isabelle locales enable reasoning at a general level, while results
|
20946
|
380 |
are implicitly transferred to all instances. For example, we can
|
|
381 |
now establish the @{text "left_cancel"} lemma for groups, which
|
|
382 |
states that the function @{text "(x \<circ>)"} is injective:
|
|
383 |
*}
|
|
384 |
|
|
385 |
lemma (in group) left_cancel: "x \<^loc>\<otimes> y = x \<^loc>\<otimes> z \<longleftrightarrow> y = z"
|
|
386 |
proof
|
|
387 |
assume "x \<^loc>\<otimes> y = x \<^loc>\<otimes> z"
|
22347
|
388 |
then have "x\<^loc>\<div> \<^loc>\<otimes> (x \<^loc>\<otimes> y) = x\<^loc>\<div> \<^loc>\<otimes> (x \<^loc>\<otimes> z)" by simp
|
|
389 |
then have "(x\<^loc>\<div> \<^loc>\<otimes> x) \<^loc>\<otimes> y = (x\<^loc>\<div> \<^loc>\<otimes> x) \<^loc>\<otimes> z" using assoc by simp
|
|
390 |
then show "y = z" using neutl and invl by simp
|
20946
|
391 |
next
|
|
392 |
assume "y = z"
|
22347
|
393 |
then show "x \<^loc>\<otimes> y = x \<^loc>\<otimes> z" by simp
|
20946
|
394 |
qed
|
|
395 |
|
|
396 |
text {*
|
|
397 |
\noindent Here the \qt{@{text "\<IN> group"}} target specification
|
|
398 |
indicates that the result is recorded within that context for later
|
|
399 |
use. This local theorem is also lifted to the global one @{text
|
22479
|
400 |
"group.left_cancel:"} @{prop [source] "\<And>x y z \<Colon> \<alpha>\<Colon>group. x \<otimes> y = x \<otimes>
|
20946
|
401 |
z \<longleftrightarrow> y = z"}. Since type @{text "int"} has been made an instance of
|
|
402 |
@{text "group"} before, we may refer to that fact as well: @{prop
|
22479
|
403 |
[source] "\<And>x y z \<Colon> int. x \<otimes> y = x \<otimes> z \<longleftrightarrow> y = z"}.
|
20946
|
404 |
*}
|
|
405 |
|
|
406 |
|
23956
|
407 |
subsection {* Derived definitions *}
|
|
408 |
|
|
409 |
text {*
|
|
410 |
Isabelle locales support a concept of local definitions
|
|
411 |
in locales:
|
|
412 |
*}
|
|
413 |
|
|
414 |
fun (in monoid)
|
|
415 |
pow_nat :: "nat \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
|
|
416 |
"pow_nat 0 x = \<^loc>\<one>"
|
|
417 |
| "pow_nat (Suc n) x = x \<^loc>\<otimes> pow_nat n x"
|
20946
|
418 |
|
|
419 |
text {*
|
23956
|
420 |
\noindent If the locale @{text group} is also a class, this local
|
|
421 |
definition is propagated onto a global definition of
|
|
422 |
@{term [source] "pow_nat \<Colon> nat \<Rightarrow> \<alpha>\<Colon>monoid \<Rightarrow> \<alpha>\<Colon>monoid"}
|
|
423 |
with corresponding theorems
|
|
424 |
|
|
425 |
@{thm pow_nat.simps [no_vars]}.
|
20946
|
426 |
|
23956
|
427 |
\noindent As you can see from this example, for local
|
|
428 |
definitions you may use any specification tool
|
|
429 |
which works together with locales (e.g. \cite{krauss2006}).
|
|
430 |
*}
|
|
431 |
|
|
432 |
|
|
433 |
(*subsection {* Additional subclass relations *}
|
22347
|
434 |
|
|
435 |
text {*
|
|
436 |
Any @{text "group"} is also a @{text "monoid"}; this
|
|
437 |
can be made explicit by claiming an additional subclass relation,
|
|
438 |
together with a proof of the logical difference:
|
|
439 |
*}
|
|
440 |
|
23956
|
441 |
instance advanced group < monoid
|
|
442 |
proof unfold_locales
|
22347
|
443 |
fix x
|
|
444 |
from invl have "x\<^loc>\<div> \<^loc>\<otimes> x = \<^loc>\<one>" by simp
|
|
445 |
with assoc [symmetric] neutl invl have "x\<^loc>\<div> \<^loc>\<otimes> (x \<^loc>\<otimes> \<^loc>\<one>) = x\<^loc>\<div> \<^loc>\<otimes> x" by simp
|
|
446 |
with left_cancel show "x \<^loc>\<otimes> \<^loc>\<one> = x" by simp
|
23956
|
447 |
qed
|
|
448 |
|
|
449 |
text {*
|
|
450 |
The logical proof is carried out on the locale level
|
|
451 |
and thus conveniently is opened using the @{text unfold_locales}
|
|
452 |
method which only leaves the logical differences still
|
|
453 |
open to proof to the user. After the proof it is propagated
|
|
454 |
to the type system, making @{text group} an instance of
|
|
455 |
@{text monoid}. For illustration, a derived definition
|
|
456 |
in @{text group} which uses @{text of_nat}:
|
|
457 |
*}
|
|
458 |
|
|
459 |
definition (in group)
|
|
460 |
pow_int :: "int \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
|
|
461 |
"pow_int k x = (if k >= 0
|
|
462 |
then pow_nat (nat k) x
|
|
463 |
else (pow_nat (nat (- k)) x)\<^loc>\<div>)"
|
|
464 |
|
|
465 |
text {*
|
|
466 |
yields the global definition of
|
|
467 |
@{term [source] "pow_int \<Colon> int \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group"}
|
|
468 |
with the corresponding theorem @{thm pow_int_def [no_vars]}.
|
|
469 |
*} *)
|
|
470 |
|
22347
|
471 |
|
22317
|
472 |
section {* Further issues *}
|
20946
|
473 |
|
22317
|
474 |
subsection {* Code generation *}
|
|
475 |
|
|
476 |
text {*
|
|
477 |
Turning back to the first motivation for type classes,
|
|
478 |
namely overloading, it is obvious that overloading
|
|
479 |
stemming from @{text "\<CLASS>"} and @{text "\<INSTANCE>"}
|
|
480 |
statements naturally maps to Haskell type classes.
|
|
481 |
The code generator framework \cite{isabelle-codegen}
|
|
482 |
takes this into account. Concerning target languages
|
|
483 |
lacking type classes (e.g.~SML), type classes
|
|
484 |
are implemented by explicit dictionary construction.
|
23956
|
485 |
For example, lets go back to the power function:
|
22317
|
486 |
*}
|
|
487 |
|
|
488 |
fun
|
23956
|
489 |
pow_nat :: "nat \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group" where
|
22317
|
490 |
"pow_nat 0 x = \<one>"
|
22479
|
491 |
| "pow_nat (Suc n) x = x \<otimes> pow_nat n x"
|
22317
|
492 |
|
|
493 |
definition
|
22347
|
494 |
pow_int :: "int \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group" where
|
22317
|
495 |
"pow_int k x = (if k >= 0
|
|
496 |
then pow_nat (nat k) x
|
|
497 |
else (pow_nat (nat (- k)) x)\<div>)"
|
|
498 |
|
|
499 |
definition
|
|
500 |
example :: int where
|
|
501 |
"example = pow_int 10 (-2)"
|
|
502 |
|
|
503 |
text {*
|
|
504 |
\noindent This maps to Haskell as:
|
|
505 |
*}
|
|
506 |
|
23956
|
507 |
code_gen example in Haskell to Classes file "code_examples/"
|
22317
|
508 |
(* NOTE: you may use Haskell only once in this document, otherwise
|
|
509 |
you have to work in distinct subdirectories *)
|
|
510 |
|
|
511 |
text {*
|
|
512 |
\lsthaskell{Thy/code_examples/Classes.hs}
|
|
513 |
|
|
514 |
\noindent The whole code in SML with explicit dictionary passing:
|
|
515 |
*}
|
|
516 |
|
23956
|
517 |
code_gen example (*<*)in SML to Classes(*>*)in SML to Classes file "code_examples/classes.ML"
|
22317
|
518 |
|
|
519 |
text {*
|
|
520 |
\lstsml{Thy/code_examples/classes.ML}
|
|
521 |
*}
|
|
522 |
|
|
523 |
|
|
524 |
(* subsection {* Different syntax for same specifications *}
|
20946
|
525 |
|
|
526 |
text {*
|
|
527 |
|
22479
|
528 |
subsection {* Syntactic classes *}
|
22317
|
529 |
|
20946
|
530 |
*} *)
|
|
531 |
|
|
532 |
end
|