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