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