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