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