8890
|
1 |
|
|
2 |
\chapter{Introduction}
|
|
3 |
|
|
4 |
A Haskell-style type-system \cite{haskell-report} with ordered type-classes
|
|
5 |
has been present in Isabelle since 1991 \cite{nipkow-sorts93}. Initially,
|
|
6 |
classes have mainly served as a \emph{purely syntactic} tool to formulate
|
|
7 |
polymorphic object-logics in a clean way, such as the standard Isabelle
|
|
8 |
formulation of many-sorted FOL \cite{paulson-isa-book}.
|
|
9 |
|
|
10 |
Applying classes at the \emph{logical level} to provide a simple notion of
|
|
11 |
abstract theories and instantiations to concrete ones, has been long proposed
|
|
12 |
as well \cite{nipkow-types93,nipkow-sorts93}). At that time, Isabelle still
|
|
13 |
lacked built-in support for these \emph{axiomatic type classes}. More
|
|
14 |
importantly, their semantics was not yet fully fleshed out (and unnecessarily
|
|
15 |
complicated, too).
|
|
16 |
|
|
17 |
Since the Isabelle94 releases, actual axiomatic type classes have been an
|
|
18 |
integral part of Isabelle's meta-logic. This very simple implementation is
|
|
19 |
based on a straight-forward extension of traditional simple-typed Higher-Order
|
|
20 |
Logic, including types qualified by logical predicates and overloaded constant
|
|
21 |
definitions; see \cite{Wenzel:1997:TPHOL} for further details.
|
|
22 |
|
|
23 |
Yet until Isabelle99, there used to be still a fundamental methodological
|
|
24 |
problem in using axiomatic type classes conveniently, due to the traditional
|
|
25 |
distinction of Isabelle theory files vs.\ ML proof scripts. This has been
|
|
26 |
finally overcome with the advent of Isabelle/Isar theories
|
|
27 |
\cite{isabelle-isar-ref}: now definitions and proofs may be freely intermixed.
|
|
28 |
This nicely accommodates the usual procedure of defining axiomatic type
|
|
29 |
classes, proving abstract properties, defining operations on concrete types,
|
|
30 |
proving concrete properties for instantiation of classes etc.
|
|
31 |
|
|
32 |
\medskip
|
|
33 |
|
|
34 |
So to cut a long story short, the present version of axiomatic type classes
|
|
35 |
(Isabelle99 or later) now provides an even more useful and convenient
|
|
36 |
mechanism for light-weight abstract theories, without any special provisions
|
|
37 |
to be observed by the user.
|
|
38 |
|
|
39 |
|
|
40 |
\chapter{Examples}\label{sec:ex}
|
|
41 |
|
|
42 |
Axiomatic type classes are a concept of Isabelle's meta-logic
|
|
43 |
\cite{paulson-isa-book,Wenzel:1997:TPHOL}. They may be applied to any
|
|
44 |
object-logic that directly uses the meta type system, such as Isabelle/HOL
|
|
45 |
\cite{isabelle-HOL}. Subsequently, we present various examples that are all
|
|
46 |
formulated within HOL, except the one of \secref{sec:ex-natclass} which is in
|
|
47 |
FOL.
|
|
48 |
|
|
49 |
\section{Semigroups}
|
|
50 |
|
|
51 |
An axiomatic type class is simply a class of types that all meet certain
|
|
52 |
\emph{axioms}. Thus, type classes may be also understood as type predicates
|
|
53 |
--- i.e.\ abstractions over a single type argument $\alpha$. Class axioms
|
|
54 |
typically contain polymorphic constants that depend on this type $\alpha$.
|
|
55 |
These \emph{characteristic constants} behave like operations associated with
|
|
56 |
the ``carrier'' type $\alpha$.
|
|
57 |
|
|
58 |
We illustrate these basic concepts by the following theory of semi-groups.
|
|
59 |
|
|
60 |
\bigskip
|
|
61 |
\input{generated/Semigroup}
|
|
62 |
\bigskip
|
|
63 |
|
|
64 |
\noindent
|
|
65 |
Above we have first declared a polymorphic constant $\TIMES :: \alpha \To
|
|
66 |
\alpha \To \alpha$ and then defined the class $semigroup$ of all types $\tau$
|
|
67 |
such that $\TIMES :: \tau \To \tau \To \tau$ is indeed an associative
|
|
68 |
operator. The $assoc$ axiom contains exactly one type variable, which is
|
|
69 |
invisible in the above presentation, though. Also note that free term
|
|
70 |
variables (like $x$, $y$, $z$) are allowed for user convenience ---
|
|
71 |
conceptually all of these are bound by outermost universal quantifiers.
|
|
72 |
|
|
73 |
\medskip
|
|
74 |
|
|
75 |
In general, type classes may be used to describe \emph{structures} with
|
|
76 |
exactly one carrier $\alpha$ and a fixed \emph{signature}. Different
|
|
77 |
signatures require different classes. In the following theory, class
|
|
78 |
$plus_semigroup$ represents semigroups of the form $(\tau, \PLUS^\tau)$ while
|
|
79 |
$times_semigroup$ represents semigroups $(\tau, \TIMES^\tau)$.
|
|
80 |
|
|
81 |
\bigskip
|
|
82 |
\input{generated/Semigroups}
|
|
83 |
\bigskip
|
|
84 |
|
|
85 |
\noindent Even if classes $plus_semigroup$ and $times_semigroup$ both represent
|
|
86 |
semigroups in a sense, they are not the same!
|
|
87 |
|
|
88 |
|
|
89 |
\section{Basic group theory}
|
|
90 |
|
|
91 |
\input{generated/Group}
|
|
92 |
|
|
93 |
|
|
94 |
The meta type system of Isabelle supports \emph{intersections} and
|
|
95 |
\emph{inclusions} of type classes. These directly correspond to intersections
|
|
96 |
and inclusions of type predicates in a purely set theoretic sense. This is
|
|
97 |
sufficient as a means to describe simple hierarchies of structures. As an
|
|
98 |
illustration, we use the well-known example of semigroups, monoids, general
|
|
99 |
groups and abelian groups.
|
|
100 |
|
|
101 |
|
|
102 |
\subsection{Monoids and Groups}
|
|
103 |
|
|
104 |
First we declare some polymorphic constants required later for the signature
|
|
105 |
parts of our structures.
|
|
106 |
|
|
107 |
|
|
108 |
Next we define class $monoid$ of monoids of the form $(\alpha,
|
|
109 |
{\TIMES}^\alpha)$. Note that multiple class axioms are allowed for user
|
|
110 |
convenience --- they simply represent the conjunction of their respective
|
|
111 |
universal closures.
|
|
112 |
|
|
113 |
|
|
114 |
So class $monoid$ contains exactly those types $\tau$ where $\TIMES :: \tau
|
|
115 |
\To \tau \To \tau$ and $1 :: \tau$ are specified appropriately, such that
|
|
116 |
$\TIMES$ is associative and $1$ is a left and right unit for $\TIMES$.
|
|
117 |
|
|
118 |
|
|
119 |
Independently of $monoid$, we now define a linear hierarchy of semigroups,
|
|
120 |
general groups and abelian groups. Note that the names of class axioms are
|
|
121 |
automatically qualified with the class name; thus we may re-use common names
|
|
122 |
such as $assoc$.
|
|
123 |
|
|
124 |
|
|
125 |
Class $group$ inherits associativity of $\TIMES$ from $semigroup$ and adds the
|
|
126 |
other two group axioms. Similarly, $agroup$ is defined as the subset of
|
|
127 |
$group$ such that for all of its elements $\tau$, the operation $\TIMES ::
|
|
128 |
\tau \To \tau \To \tau$ is even commutative.
|
|
129 |
|
|
130 |
|
|
131 |
\subsection{Abstract reasoning}
|
|
132 |
|
|
133 |
In a sense, axiomatic type classes may be viewed as \emph{abstract theories}.
|
|
134 |
Above class definitions internally generate the following abstract axioms:
|
|
135 |
|
|
136 |
%FIXME
|
|
137 |
% \begin{ascbox}
|
|
138 |
% assoc: (?x::?'a::semigroup) <*> (?y::?'a::semigroup)
|
|
139 |
% <*> (?z::?'a::semigroup) = ?x <*> (?y <*> ?z)\medskip
|
|
140 |
% left@unit: 1 <*> (?x::?'a::group) = ?x
|
|
141 |
% left@inverse: inverse (?x::?'a::group) <*> ?x = 1\medskip
|
|
142 |
% commut: (?x::?'a::agroup) <*> (?y::?'a::agroup) = ?y <*> ?x
|
|
143 |
% \emphnd{ascbox}
|
|
144 |
|
|
145 |
All of these contain type variables $\alpha :: c$ that are restricted to types
|
|
146 |
of some class $c$. These \emph{sort constraints} express a logical
|
|
147 |
precondition for the whole formula. For example, $assoc$ states that for all
|
|
148 |
$\tau$, provided that $\tau :: semigroup$, the operation $\TIMES :: \tau \To
|
|
149 |
\tau \To \tau$ is associative.
|
|
150 |
|
|
151 |
\medskip
|
|
152 |
|
|
153 |
From a purely technical point of view, abstract axioms are just ordinary
|
|
154 |
Isabelle theorems; they may be used for proofs without special treatment.
|
|
155 |
Such ``abstract proofs'' usually yield new abstract theorems. For example, we
|
|
156 |
may now derive the following laws of general groups.
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
Abstract theorems may be instantiated to only those types $\tau$ where the
|
|
161 |
appropriate class membership $\tau :: c$ is known at Isabelle's type signature
|
|
162 |
level. Since we have $agroup \subseteq group \subseteq semigroup$ by
|
|
163 |
definition, all theorems of $semigroup$ and $group$ are automatically
|
|
164 |
inherited by $group$ and $agroup$.
|
|
165 |
|
|
166 |
|
|
167 |
\subsection{Abstract instantiation}
|
|
168 |
|
|
169 |
From the definition, the $monoid$ and $group$ classes have been independent.
|
|
170 |
Note that for monoids, $right_unit$ had to be included as an axiom, but for
|
|
171 |
groups both $right_unit$ and $right_inverse$ are derivable from the other
|
|
172 |
axioms. With $group_right_unit$ derived as a theorem of group theory (see
|
|
173 |
page~\pageref{thm:group-right-unit}), we may now instantiate $group \subset
|
|
174 |
monoid$ properly as follows (cf.\ \figref{fig:monoid-group}).
|
|
175 |
|
|
176 |
\begin{figure}[htbp]
|
|
177 |
\begin{center}
|
|
178 |
\small
|
|
179 |
% \unitlength 0.75mm
|
|
180 |
\unitlength 0.6mm
|
|
181 |
\begin{picture}(65,90)(0,-10)
|
|
182 |
\put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}}
|
|
183 |
\put(15,50){\line(1,1){10}} \put(35,60){\line(1,-1){10}}
|
|
184 |
\put(15,5){\makebox(0,0){$agroup$}}
|
|
185 |
\put(15,25){\makebox(0,0){$group$}}
|
|
186 |
\put(15,45){\makebox(0,0){$semigroup$}}
|
|
187 |
\put(30,65){\makebox(0,0){$term$}} \put(50,45){\makebox(0,0){$monoid$}}
|
|
188 |
\end{picture}
|
|
189 |
\hspace{4em}
|
|
190 |
\begin{picture}(30,90)(0,0)
|
|
191 |
\put(15,10){\line(0,1){10}} \put(15,30){\line(0,1){10}}
|
|
192 |
\put(15,50){\line(0,1){10}} \put(15,70){\line(0,1){10}}
|
|
193 |
\put(15,5){\makebox(0,0){$agroup$}}
|
|
194 |
\put(15,25){\makebox(0,0){$group$}}
|
|
195 |
\put(15,45){\makebox(0,0){$monoid$}}
|
|
196 |
\put(15,65){\makebox(0,0){$semigroup$}}
|
|
197 |
\put(15,85){\makebox(0,0){$term$}}
|
|
198 |
\end{picture}
|
|
199 |
\caption{Monoids and groups: according to definition, and by proof}
|
|
200 |
\label{fig:monoid-group}
|
|
201 |
\end{center}
|
|
202 |
\end{figure}
|
|
203 |
|
|
204 |
|
|
205 |
\endinput
|
|
206 |
|
|
207 |
We know by definition that $\TIMES$ is associative for monoids, i.e.\ $monoid
|
|
208 |
\subseteq semigroup$. Furthermore we have $assoc$, $left_unit$ and
|
|
209 |
$right_unit$ for groups (where $right_unit$ is derivable from the group
|
|
210 |
axioms), that is $group \subseteq monoid$. Thus we may establish the
|
|
211 |
following class instantiations.
|
|
212 |
|
|
213 |
\endinput
|
|
214 |
|
|
215 |
The \texttt{instance} section does really check that the class inclusions
|
|
216 |
(or type arities) to be added are derivable. To this end, it sets up a
|
|
217 |
suitable goal and attempts a proof with the help of some internal
|
|
218 |
axioms and user supplied theorems. These latter \emph{witnesses} usually
|
|
219 |
should be appropriate type instances of the class axioms (as are
|
|
220 |
\texttt{Monoid.assoc} and \texttt{assoc}, \texttt{left_unit}, \texttt{right_unit}
|
|
221 |
above).
|
|
222 |
|
|
223 |
The most important internal tool for instantiation proofs is the class
|
|
224 |
introduction rule that is automatically generated by \texttt{axclass}. For
|
|
225 |
class \texttt{group} this is axiom \texttt{groupI}:
|
|
226 |
|
|
227 |
\begin{ascbox}
|
|
228 |
groupI: [| OFCLASS(?'a::term, semigroup@class);
|
|
229 |
!!x::?'a::term. 1 <*> x = x;
|
|
230 |
!!x::?'a::term. inverse x <*> x = 1 |]
|
|
231 |
==> OFCLASS(?'a::term, group@class)
|
|
232 |
\emphnd{ascbox}
|
|
233 |
|
|
234 |
There are also axioms \texttt{monoidI}, \texttt{semigroupI} and \texttt{agroupI}
|
|
235 |
of a similar form. Note that $\texttt{OFCLASS}(\tau, c \texttt{_class})$
|
|
236 |
expresses class membership $\tau :: c$ as a proposition of the
|
|
237 |
meta-logic.
|
|
238 |
|
|
239 |
|
|
240 |
\subsection{Concrete instantiation}
|
|
241 |
|
|
242 |
So far we have covered the case of \texttt{instance $c@1$ < $c@2$} that
|
|
243 |
could be described as \emph{abstract instantiation} --- $c@1$ is more
|
|
244 |
special than $c@2$ and thus an instance of $c@2$. Even more
|
|
245 |
interesting for practical applications are \emph{concrete instantiations}
|
|
246 |
of axiomatic type classes. That is, certain simple schemes $(\alpha@1,
|
|
247 |
\ldots, \alpha@n)t :: c$ of class membership may be transferred to
|
|
248 |
Isabelle's type signature level. This form of \texttt{instance} is a ``safe''
|
|
249 |
variant of the old-style \texttt{arities} theory section.
|
|
250 |
|
|
251 |
As an example, we show that type \texttt{bool} with exclusive-or as
|
|
252 |
operation $\TIMES$, identity as \texttt{inverse}, and \texttt{False} as \texttt{1}
|
|
253 |
forms an abelian group. We first define theory \texttt{Xor}:
|
|
254 |
|
|
255 |
\begin{ascbox}
|
|
256 |
Xor = Group +\medskip
|
|
257 |
defs
|
|
258 |
prod@bool@def "x <*> y == x ~= (y::bool)"
|
|
259 |
inverse@bool@def "inverse x == x::bool"
|
|
260 |
unit@bool@def "1 == False"\medskip
|
|
261 |
end
|
|
262 |
\emphnd{ascbox}
|
|
263 |
|
|
264 |
It is important to note that above \texttt{defs} are just overloaded
|
|
265 |
meta-level constant definitions. In particular, type classes are not
|
|
266 |
yet involved at all! This form of constant definition with overloading
|
|
267 |
(and optional primitive recursion over types) would be also possible
|
|
268 |
in traditional versions of \HOL\ that lack type classes.
|
|
269 |
% (see FIXME <foundation> for more details)
|
|
270 |
Nonetheless, such definitions are best applied in the context of
|
|
271 |
classes.
|
|
272 |
|
|
273 |
\medskip
|
|
274 |
|
|
275 |
Since we chose the \texttt{defs} of theory \texttt{Xor} suitably, the class
|
|
276 |
membership $\texttt{bool} :: \texttt{agroup}$ is now derivable as follows:
|
|
277 |
|
|
278 |
\begin{ascbox}
|
|
279 |
open AxClass;
|
|
280 |
goal@arity Xor.thy ("bool", [], "agroup");
|
|
281 |
\out{Level 0}
|
|
282 |
\out{OFCLASS(bool, agroup@class)}
|
|
283 |
\out{ 1. OFCLASS(bool, agroup@class)}\brk
|
|
284 |
by (axclass@tac []);
|
|
285 |
\out{Level 1}
|
|
286 |
\out{OFCLASS(bool, agroup@class)}
|
|
287 |
\out{ 1. !!(x::bool) (y::bool) z::bool. x <*> y <*> z = x <*> (y <*> z)}
|
|
288 |
\out{ 2. !!x::bool. 1 <*> x = x}
|
|
289 |
\out{ 3. !!x::bool. inverse x <*> x = 1}
|
|
290 |
\out{ 4. !!(x::bool) y::bool. x <*> y = y <*> x}
|
|
291 |
\emphnd{ascbox}
|
|
292 |
|
|
293 |
The tactic \texttt{axclass_tac} has applied \texttt{agroupI} internally to
|
|
294 |
expand the class definition. It now remains to be shown that the
|
|
295 |
\texttt{agroup} axioms hold for bool. To this end, we expand the
|
|
296 |
definitions of \texttt{Xor} and solve the new subgoals by \texttt{fast_tac}
|
|
297 |
of \HOL:
|
|
298 |
|
|
299 |
\begin{ascbox}
|
|
300 |
by (rewrite@goals@tac [Xor.prod@bool@def, Xor.inverse@bool@def,
|
|
301 |
Xor.unit@bool@def]);
|
|
302 |
\out{Level 2}
|
|
303 |
\out{OFCLASS(bool, agroup@class)}
|
|
304 |
\out{ 1. !!(x::bool) (y::bool) z::bool. x ~= y ~= z = (x ~= (y ~= z))}
|
|
305 |
\out{ 2. !!x::bool. False ~= x = x}
|
|
306 |
\out{ 3. !!x::bool. x ~= x = False}
|
|
307 |
\out{ 4. !!(x::bool) y::bool. x ~= y = (y ~= x)}
|
|
308 |
by (ALLGOALS (fast@tac HOL@cs));
|
|
309 |
\out{Level 3}
|
|
310 |
\out{OFCLASS(bool, agroup@class)}
|
|
311 |
\out{No subgoals!}
|
|
312 |
qed "bool@in@agroup";
|
|
313 |
\out{val bool@in@agroup = "OFCLASS(bool, agroup@class)"}
|
|
314 |
\emphnd{ascbox}
|
|
315 |
|
|
316 |
The result is theorem $\texttt{OFCLASS}(\texttt{bool}, \texttt{agroup_class})$
|
|
317 |
which expresses $\texttt{bool} :: \texttt{agroup}$ as a meta-proposition. This
|
|
318 |
is not yet known at the type signature level, though.
|
|
319 |
|
|
320 |
\medskip
|
|
321 |
|
|
322 |
What we have done here by hand, can be also performed via
|
|
323 |
\texttt{instance} in a similar way behind the scenes. This may look as
|
|
324 |
follows\footnote{Subsequently, theory \texttt{Xor} is no longer
|
|
325 |
required.}:
|
|
326 |
|
|
327 |
\begin{ascbox}
|
|
328 |
BoolGroupInsts = Group +\medskip
|
|
329 |
defs
|
|
330 |
prod@bool@def "x <*> y == x ~= (y::bool)"
|
|
331 |
inverse@bool@def "inverse x == x::bool"
|
|
332 |
unit@bool@def "1 == False"\medskip
|
|
333 |
instance
|
|
334 |
bool :: agroup \{| ALLGOALS (fast@tac HOL@cs) |\}\medskip
|
|
335 |
end
|
|
336 |
\emphnd{ascbox}
|
|
337 |
|
|
338 |
This way, we have $\texttt{bool} :: \texttt{agroup}$ in the type signature of
|
|
339 |
\texttt{BoolGroupInsts}, and all abstract group theorems are transferred
|
|
340 |
to \texttt{bool} for free.
|
|
341 |
|
|
342 |
Similarly, we could now also instantiate our group theory classes to
|
|
343 |
many other concrete types. For example, $\texttt{int} :: \texttt{agroup}$ (by
|
|
344 |
defining $\TIMES$ as addition, \texttt{inverse} as negation and \texttt{1} as
|
|
345 |
zero, say) or $\texttt{list} :: (\texttt{term})\texttt{semigroup}$ (e.g.\ if
|
|
346 |
$\TIMES$ is list append). Thus, the characteristic constants $\TIMES$,
|
|
347 |
\texttt{inverse}, \texttt{1} really become overloaded, i.e.\ have different
|
|
348 |
meanings on different types.
|
|
349 |
|
|
350 |
|
|
351 |
\subsection{Lifting and Functors}
|
|
352 |
|
|
353 |
As already mentioned above, \HOL-like systems not only support
|
|
354 |
overloaded definitions of polymorphic constants (without requiring
|
|
355 |
type classes), but even primitive recursion over types. That is,
|
|
356 |
definitional equations $c^\tau \emphq t$ may also contain constants of
|
|
357 |
name $c$ on the RHS --- provided that these have types that are
|
|
358 |
strictly simpler (structurally) than $\tau$.
|
|
359 |
|
|
360 |
This feature enables us to \emph{lift operations}, e.g.\ to Cartesian
|
|
361 |
products, direct sums or function spaces. Below, theory
|
|
362 |
\texttt{ProdGroupInsts} lifts $\TIMES$ componentwise to two-place
|
|
363 |
Cartesian products $\alpha \times \beta$:
|
|
364 |
|
|
365 |
\begin{ascbox}
|
|
366 |
ProdGroupInsts = Prod + Group +\medskip
|
|
367 |
defs
|
|
368 |
prod@prod@def "p <*> q == (fst p <*> fst q, snd p <*> snd q)"\medskip
|
|
369 |
instance
|
|
370 |
"*" :: (semigroup, semigroup) semigroup
|
|
371 |
\{| simp@tac (prod@ss addsimps [assoc]) 1 |\}
|
|
372 |
end
|
|
373 |
\emphnd{ascbox}
|
|
374 |
|
|
375 |
Note that \texttt{prod_prod_def} basically has the form $\emphdrv
|
|
376 |
{\TIMES}^{\alpha \times \beta} \emphq \ldots {\TIMES}^\alpha \ldots
|
|
377 |
{\TIMES}^\beta \ldots$, i.e.\ the recursive occurrences
|
|
378 |
$\TIMES^\alpha$ and $\TIMES^\beta$ are really at ``simpler'' types.
|
|
379 |
|
|
380 |
It is very easy to see that associativity of $\TIMES^\alpha$,
|
|
381 |
$\TIMES^\beta$ transfers to ${\TIMES}^{\alpha \times \beta}$. Hence
|
|
382 |
the two-place type constructor $\times$ maps semigroups into
|
|
383 |
semigroups. This fact is proven and put into Isabelle's type signature by
|
|
384 |
above \texttt{instance} section.
|
|
385 |
|
|
386 |
\medskip
|
|
387 |
|
|
388 |
Thus, if we view class instances as ``structures'', overloaded
|
|
389 |
constant definitions with primitive recursion over types indirectly
|
|
390 |
provide some kind of ``functors'' (mappings between abstract theories,
|
|
391 |
that is).
|
|
392 |
|
|
393 |
|
|
394 |
|
|
395 |
|
|
396 |
%%% Local Variables:
|
|
397 |
%%% mode: latex
|
|
398 |
%%% TeX-master: "axclass"
|
|
399 |
%%% End:
|
|
400 |
|
|
401 |
|
|
402 |
\section{Syntactic classes}
|
|
403 |
|
|
404 |
There is still a feature of Isabelle's type system left that we have not yet
|
|
405 |
used: when declaring polymorphic constants $c :: \sigma$, the type variables
|
|
406 |
occurring in $\sigma$ may be constrained by type classes (or even general
|
|
407 |
sorts) in an arbitrary way. Note that by default, in Isabelle/HOL the
|
|
408 |
declaration: FIXME
|
|
409 |
|
|
410 |
\input{generated/Product}
|
|
411 |
\input{generated/NatClass}
|
|
412 |
|
|
413 |
|
|
414 |
|
|
415 |
\endinput
|
|
416 |
|
|
417 |
|
|
418 |
%\section
|
|
419 |
|
|
420 |
\begin{ascbox}
|
|
421 |
<*> :: ['a, 'a] => 'a
|
|
422 |
\emphnd{ascbox}
|
|
423 |
|
|
424 |
is actually an abbreviation for:
|
|
425 |
|
|
426 |
\begin{ascbox}
|
|
427 |
<*> :: ['a::term, 'a::term] => 'a::term
|
|
428 |
\emphnd{ascbox}
|
|
429 |
|
|
430 |
Since class \texttt{term} is the universal class of \HOL, this is not
|
|
431 |
really a restriction at all. A less trivial example is the following
|
|
432 |
theory \texttt{Product}:
|
|
433 |
|
|
434 |
\begin{ascbox}
|
|
435 |
Product = HOL +\medskip
|
|
436 |
axclass
|
|
437 |
product < term\medskip
|
|
438 |
consts
|
|
439 |
"<*>" :: "['a::product, 'a] => 'a" (infixl 70)\medskip
|
|
440 |
end
|
|
441 |
\emphnd{ascbox}
|
|
442 |
|
|
443 |
Here class \texttt{product} is defined as subclass of \texttt{term}, but
|
|
444 |
without any additional axioms. This effects in logical equivalence of
|
|
445 |
\texttt{product} and \texttt{term}, since \texttt{productI} is as follows:
|
|
446 |
|
|
447 |
\begin{ascbox}
|
|
448 |
productI: OFCLASS(?'a::logic, term@class) ==>
|
|
449 |
OFCLASS(?'a::logic, product@class)
|
|
450 |
\emphnd{ascbox}
|
|
451 |
|
|
452 |
I.e.\ $\texttt{term} \subseteq \texttt{product}$. The converse inclusion is
|
|
453 |
already contained in the type signature of theory \texttt{Product}.
|
|
454 |
|
|
455 |
Now, what is the difference of declaring $\TIMES :: [\alpha ::
|
|
456 |
\texttt{product}, \alpha] \To \alpha$ vs.\ declaring $\TIMES :: [\alpha ::
|
|
457 |
\texttt{term}, \alpha] \To \alpha$? In this special case (where
|
|
458 |
$\texttt{product} \emphq \texttt{term}$), it should be obvious that both
|
|
459 |
declarations are the same from the logic's point of view. It is even
|
|
460 |
most sensible in the general case not to attach any \emph{logical}
|
|
461 |
meaning to sort constraints occurring in constant \emph{declarations}
|
|
462 |
(see \cite[page 79]{Wenzel94} for more details).
|
|
463 |
|
|
464 |
On the other hand there are syntactic differences, of course.
|
|
465 |
Constants $\TIMES^\tau$ are rejected by the type-checker, unless $\tau
|
|
466 |
:: \texttt{product}$ is part of the type signature. In our example, this
|
|
467 |
arity may be always added when required by means of a trivial
|
|
468 |
\texttt{instance}.
|
|
469 |
|
|
470 |
Thus, we can follow this discipline: Overloaded polymorphic constants
|
|
471 |
have their type arguments restricted to an associated (logically
|
|
472 |
trivial) class $c$. Only immediately before \emph{specifying} these
|
|
473 |
constants on a certain type $\tau$ do we instantiate $\tau :: c$.
|
|
474 |
|
|
475 |
This is done for class \texttt{product} and type \texttt{bool} in theory
|
|
476 |
\texttt{ProductInsts} below:
|
|
477 |
|
|
478 |
\begin{ascbox}
|
|
479 |
ProductInsts = Product +\medskip
|
|
480 |
instance
|
|
481 |
bool :: product\medskip
|
|
482 |
defs
|
|
483 |
prod@bool@def "x <*> y == x & y::bool"\medskip
|
|
484 |
end
|
|
485 |
\emphnd{ascbox}
|
|
486 |
|
|
487 |
Note that \texttt{instance bool ::\ product} does not require any
|
|
488 |
witnesses, since this statement is logically trivial. Nonetheless,
|
|
489 |
\texttt{instance} really performs a proof.
|
|
490 |
|
|
491 |
Only after $\texttt{bool} :: \texttt{product}$ is made known to the type
|
|
492 |
checker, does \texttt{prod_bool_def} become syntactically well-formed.
|
|
493 |
|
|
494 |
\medskip
|
|
495 |
|
|
496 |
It is very important to see that above \texttt{defs} are not directly
|
|
497 |
connected with \texttt{instance} at all! We were just following our
|
|
498 |
convention to specify $\TIMES$ on \texttt{bool} after having instantiated
|
|
499 |
$\texttt{bool} :: \texttt{product}$. Isabelle does not require these definitions,
|
|
500 |
which is in contrast to programming languages like Haskell.
|
|
501 |
|
|
502 |
\medskip
|
|
503 |
|
|
504 |
While Isabelle type classes and those of Haskell are almost the same as
|
|
505 |
far as type-checking and type inference are concerned, there are major
|
|
506 |
semantic differences. Haskell classes require their instances to
|
|
507 |
\emph{provide operations} of certain \emph{names}. Therefore, its
|
|
508 |
\texttt{instance} has a \texttt{where} part that tells the system what these
|
|
509 |
``member functions'' should be.
|
|
510 |
|
|
511 |
This style of \texttt{instance} won't make much sense in Isabelle, because its
|
|
512 |
meta-logic has no corresponding notion of ``providing operations'' or
|
|
513 |
``names''.
|
|
514 |
|
|
515 |
|
|
516 |
\section{Defining natural numbers in FOL}
|
|
517 |
\label{sec:ex-natclass}
|
|
518 |
|
|
519 |
Axiomatic type classes abstract over exactly one type argument. Thus,
|
|
520 |
any \emph{axiomatic} theory extension where each axiom refers to at most
|
|
521 |
one type variable, may be trivially turned into a \emph{definitional}
|
|
522 |
one.
|
|
523 |
|
|
524 |
We illustrate this with the natural numbers in Isabelle/FOL:
|
|
525 |
|
|
526 |
\begin{ascbox}
|
|
527 |
NatClass = FOL +\medskip
|
|
528 |
consts
|
|
529 |
"0" :: "'a" ("0")
|
|
530 |
Suc :: "'a => 'a"
|
|
531 |
rec :: "['a, 'a, ['a, 'a] => 'a] => 'a"\medskip
|
|
532 |
axclass
|
|
533 |
nat < term
|
|
534 |
induct "[| P(0); !!x. P(x) ==> P(Suc(x)) |] ==> P(n)"
|
|
535 |
Suc@inject "Suc(m) = Suc(n) ==> m = n"
|
|
536 |
Suc@neq@0 "Suc(m) = 0 ==> R"
|
|
537 |
rec@0 "rec(0, a, f) = a"
|
|
538 |
rec@Suc "rec(Suc(m), a, f) = f(m, rec(m, a, f))"\medskip
|
|
539 |
consts
|
|
540 |
"+" :: "['a::nat, 'a] => 'a" (infixl 60)\medskip
|
|
541 |
defs
|
|
542 |
add@def "m + n == rec(m, n, %x y. Suc(y))"\medskip
|
|
543 |
end
|
|
544 |
\emphnd{ascbox}
|
|
545 |
|
|
546 |
\texttt{NatClass} is an abstract version of \texttt{Nat}\footnote{See
|
|
547 |
directory \texttt{FOL/ex}.}. Basically, we have just replaced all
|
|
548 |
occurrences of \emph{type} \texttt{nat} by $\alpha$ and used the natural
|
|
549 |
number axioms to define \emph{class} \texttt{nat}\footnote{There's a snag:
|
|
550 |
The original recursion operator \texttt{rec} had to be made monomorphic,
|
|
551 |
in a sense.}. Thus class \texttt{nat} contains exactly those types
|
|
552 |
$\tau$ that are isomorphic to ``the'' natural numbers (with signature
|
|
553 |
\texttt{0}, \texttt{Suc}, \texttt{rec}).
|
|
554 |
|
|
555 |
Furthermore, theory \texttt{NatClass} defines an ``abstract constant'' $+$
|
|
556 |
based on class \texttt{nat}.
|
|
557 |
|
|
558 |
\medskip
|
|
559 |
|
|
560 |
What we have done here can be also viewed as \emph{type specification}.
|
|
561 |
Of course, it still remains open if there is some type at all that
|
|
562 |
meets the class axioms. Now a very nice property of axiomatic type
|
|
563 |
classes is, that abstract reasoning is always possible --- independent
|
|
564 |
of satisfiability. The meta-logic won't break, even if some class (or
|
|
565 |
general sort) turns out to be empty (``inconsistent'')
|
|
566 |
later\footnote{As a consequence of an old bug, this is \emph{not} true
|
|
567 |
for pre-Isabelle94-2 versions.}.
|
|
568 |
|
|
569 |
For example, we may derive the following abstract natural numbers
|
|
570 |
theorems:
|
|
571 |
|
|
572 |
\begin{ascbox}
|
|
573 |
add@0: 0 + (?n::?'a::nat) = ?n
|
|
574 |
add@Suc: Suc(?m::?'a::nat) + (?n::?'a::nat) = Suc(?m + ?n)
|
|
575 |
\emphnd{ascbox}
|
|
576 |
|
|
577 |
See also file \texttt{FOL/ex/NatClass.ML}. Note that this required only
|
|
578 |
some trivial modifications of the original \texttt{Nat.ML}.
|
|
579 |
|
|
580 |
|
|
581 |
%% FIXME move some parts to ref or isar-ref manual (!?);
|
|
582 |
|
|
583 |
% \chapter{The user interface of Isabelle's axclass package}
|
|
584 |
|
|
585 |
% The actual axiomatic type class package of Isabelle/Pure mainly consists
|
|
586 |
% of two new theory sections: \texttt{axclass} and \texttt{instance}. Some
|
|
587 |
% typical applications of these have already been demonstrated in
|
|
588 |
% \secref{sec:ex}, below their syntax and semantics are presented more
|
|
589 |
% completely.
|
|
590 |
|
|
591 |
|
|
592 |
% \section{The axclass section}
|
|
593 |
|
|
594 |
% Within theory files, \texttt{axclass} introduces an axiomatic type class
|
|
595 |
% definition. Its concrete syntax is:
|
|
596 |
|
|
597 |
% \begin{matharray}{l}
|
|
598 |
% \texttt{axclass} \\
|
|
599 |
% \ \ c \texttt{ < } c@1\texttt, \ldots\texttt, c@n \\
|
|
600 |
% \ \ id@1\ axm@1 \\
|
|
601 |
% \ \ \vdots \\
|
|
602 |
% \ \ id@m\ axm@m
|
|
603 |
% \emphnd{matharray}
|
|
604 |
|
|
605 |
% Where $c, c@1, \ldots, c@n$ are classes (category $id$ or
|
|
606 |
% $string$) and $axm@1, \ldots, axm@m$ (with $m \ge
|
|
607 |
% 0$) are formulas (category $string$).
|
|
608 |
|
|
609 |
% Class $c$ has to be new, and sort $\{c@1, \ldots, c@n\}$ a subsort of
|
|
610 |
% \texttt{logic}. Each class axiom $axm@j$ may contain any term
|
|
611 |
% variables, but at most one type variable (which need not be the same
|
|
612 |
% for all axioms). The sort of this type variable has to be a supersort
|
|
613 |
% of $\{c@1, \ldots, c@n\}$.
|
|
614 |
|
|
615 |
% \medskip
|
|
616 |
|
|
617 |
% The \texttt{axclass} section declares $c$ as subclass of $c@1, \ldots,
|
|
618 |
% c@n$ to the type signature.
|
|
619 |
|
|
620 |
% Furthermore, $axm@1, \ldots, axm@m$ are turned into the
|
|
621 |
% ``abstract axioms'' of $c$ with names $id@1, \ldots,
|
|
622 |
% id@m$. This is done by replacing all occurring type variables
|
|
623 |
% by $\alpha :: c$. Original axioms that do not contain any type
|
|
624 |
% variable will be prefixed by the logical precondition
|
|
625 |
% $\texttt{OFCLASS}(\alpha :: \texttt{logic}, c\texttt{_class})$.
|
|
626 |
|
|
627 |
% Another axiom of name $c\texttt{I}$ --- the ``class $c$ introduction
|
|
628 |
% rule'' --- is built from the respective universal closures of
|
|
629 |
% $axm@1, \ldots, axm@m$ appropriately.
|
|
630 |
|
|
631 |
|
|
632 |
% \section{The instance section}
|
|
633 |
|
|
634 |
% Section \texttt{instance} proves class inclusions or type arities at the
|
|
635 |
% logical level and then transfers these into the type signature.
|
|
636 |
|
|
637 |
% Its concrete syntax is:
|
|
638 |
|
|
639 |
% \begin{matharray}{l}
|
|
640 |
% \texttt{instance} \\
|
|
641 |
% \ \ [\ c@1 \texttt{ < } c@2 \ |\
|
|
642 |
% t \texttt{ ::\ (}sort@1\texttt, \ldots \texttt, sort@n\texttt) sort\ ] \\
|
|
643 |
% \ \ [\ \texttt(name@1 \texttt, \ldots\texttt, name@m\texttt)\ ] \\
|
|
644 |
% \ \ [\ \texttt{\{|} text \texttt{|\}}\ ]
|
|
645 |
% \emphnd{matharray}
|
|
646 |
|
|
647 |
% Where $c@1, c@2$ are classes and $t$ is an $n$-place type constructor
|
|
648 |
% (all of category $id$ or $string)$. Furthermore,
|
|
649 |
% $sort@i$ are sorts in the usual Isabelle-syntax.
|
|
650 |
|
|
651 |
% \medskip
|
|
652 |
|
|
653 |
% Internally, \texttt{instance} first sets up an appropriate goal that
|
|
654 |
% expresses the class inclusion or type arity as a meta-proposition.
|
|
655 |
% Then tactic \texttt{AxClass.axclass_tac} is applied with all preceding
|
|
656 |
% meta-definitions of the current theory file and the user-supplied
|
|
657 |
% witnesses. The latter are $name@1, \ldots, name@m$, where
|
|
658 |
% $id$ refers to an \ML-name of a theorem, and $string$ to an
|
|
659 |
% axiom of the current theory node\footnote{Thus, the user may reference
|
|
660 |
% axioms from above this \texttt{instance} in the theory file. Note
|
|
661 |
% that new axioms appear at the \ML-toplevel only after the file is
|
|
662 |
% processed completely.}.
|
|
663 |
|
|
664 |
% Tactic \texttt{AxClass.axclass_tac} first unfolds the class definition by
|
|
665 |
% resolving with rule $c\texttt\texttt{I}$, and then applies the witnesses
|
|
666 |
% according to their form: Meta-definitions are unfolded, all other
|
|
667 |
% formulas are repeatedly resolved\footnote{This is done in a way that
|
|
668 |
% enables proper object-\emph{rules} to be used as witnesses for
|
|
669 |
% corresponding class axioms.} with.
|
|
670 |
|
|
671 |
% The final optional argument $text$ is \ML-code of an arbitrary
|
|
672 |
% user tactic which is applied last to any remaining goals.
|
|
673 |
|
|
674 |
% \medskip
|
|
675 |
|
|
676 |
% Because of the complexity of \texttt{instance}'s witnessing mechanisms,
|
|
677 |
% new users of the axclass package are advised to only use the simple
|
|
678 |
% form $\texttt{instance}\ \ldots\ (id@1, \ldots, id@!m)$, where
|
|
679 |
% the identifiers refer to theorems that are appropriate type instances
|
|
680 |
% of the class axioms. This typically requires an auxiliary theory,
|
|
681 |
% though, which defines some constants and then proves these witnesses.
|
|
682 |
|
|
683 |
|
|
684 |
%%% Local Variables:
|
|
685 |
%%% mode: latex
|
|
686 |
%%% TeX-master: "axclass"
|
|
687 |
%%% End:
|
|
688 |
% LocalWords: Isabelle FOL
|