8890
|
1 |
|
8903
|
2 |
header {* Syntactic classes *};
|
8890
|
3 |
|
|
4 |
theory Product = Main:;
|
|
5 |
|
8903
|
6 |
text {*
|
|
7 |
\medskip\noindent There is still a feature of Isabelle's type system
|
|
8 |
left that we have not yet used: when declaring polymorphic constants
|
|
9 |
$c :: \sigma$, the type variables occurring in $\sigma$ may be
|
|
10 |
constrained by type classes (or even general sorts) in an arbitrary
|
|
11 |
way. Note that by default, in Isabelle/HOL the declaration $\TIMES
|
|
12 |
:: \alpha \To \alpha \To \alpha$ is actually an abbreviation for
|
|
13 |
$\TIMES :: (\alpha::term) \To \alpha \To \alpha$. Since class $term$
|
|
14 |
is the universal class of HOL, this is not really a restriction at
|
|
15 |
all.
|
|
16 |
|
|
17 |
The $product$ class below provides a less degenerate example of
|
|
18 |
syntactic type classes.
|
|
19 |
*};
|
|
20 |
|
8890
|
21 |
axclass
|
|
22 |
product < "term";
|
|
23 |
consts
|
|
24 |
product :: "'a::product \\<Rightarrow> 'a \\<Rightarrow> 'a" (infixl "\\<otimes>" 70);
|
|
25 |
|
8903
|
26 |
text {*
|
|
27 |
Here class $product$ is defined as subclass of $term$ without any
|
|
28 |
additional axioms. This effects in logical equivalence of $product$
|
|
29 |
and $term$, as is reflected by the trivial introduction rule
|
|
30 |
generated for this definition.
|
|
31 |
|
|
32 |
\medskip So what is the difference of declaring $\TIMES :: (\alpha ::
|
|
33 |
product) \To \alpha \To \alpha$ vs.\ declaring $\TIMES :: (\alpha ::
|
|
34 |
term) \To \alpha \To \alpha$ anyway? In this particular case where
|
|
35 |
$product \equiv term$, it should be obvious that both declarations
|
|
36 |
are the same from the logic's point of view. It even makes the most
|
|
37 |
sense to remove sort constraints from constant declarations, as far
|
|
38 |
as the purely logical meaning is concerned \cite{Wenzel:1997:TPHOL}.
|
|
39 |
|
|
40 |
On the other hand there are syntactic differences, of course.
|
|
41 |
Constants $\TIMES^\tau$ are rejected by the type-checker, unless the
|
|
42 |
arity $\tau :: product$ is part of the type signature. In our
|
|
43 |
example, this arity may be always added when required by means of an
|
|
44 |
$\isarkeyword{instance}$ with the trivial proof $\BY{intro_classes}$.
|
|
45 |
|
|
46 |
\medskip Thus, we may observe the following discipline of using
|
|
47 |
syntactic classes. Overloaded polymorphic constants have their type
|
|
48 |
arguments restricted to an associated (logically trivial) class $c$.
|
|
49 |
Only immediately before \emph{specifying} these constants on a
|
|
50 |
certain type $\tau$ do we instantiate $\tau :: c$.
|
|
51 |
|
|
52 |
This is done for class $product$ and type $bool$ as follows.
|
|
53 |
*};
|
|
54 |
|
8890
|
55 |
instance bool :: product;
|
|
56 |
by intro_classes;
|
|
57 |
defs
|
|
58 |
product_bool_def: "x \\<otimes> y \\<equiv> x \\<and> y";
|
|
59 |
|
8903
|
60 |
text {*
|
|
61 |
The definition $prod_bool_def$ becomes syntactically well-formed only
|
|
62 |
after the arity $bool :: product$ is made known to the type checker.
|
|
63 |
|
|
64 |
\medskip It is very important to see that above $\DEFS$ are not
|
|
65 |
directly connected with $\isarkeyword{instance}$ at all! We were
|
|
66 |
just following our convention to specify $\TIMES$ on $bool$ after
|
|
67 |
having instantiated $bool :: product$. Isabelle does not require
|
|
68 |
these definitions, which is in contrast to programming languages like
|
|
69 |
Haskell \cite{haskell-report}.
|
|
70 |
|
|
71 |
\medskip While Isabelle type classes and those of Haskell are almost
|
|
72 |
the same as far as type-checking and type inference are concerned,
|
|
73 |
there are major semantic differences. Haskell classes require their
|
|
74 |
instances to \emph{provide operations} of certain \emph{names}.
|
|
75 |
Therefore, its \texttt{instance} has a \texttt{where} part that tells
|
|
76 |
the system what these ``member functions'' should be.
|
|
77 |
|
|
78 |
This style of \texttt{instance} won't make much sense in Isabelle,
|
|
79 |
because its meta-logic has no corresponding notion of ``providing
|
|
80 |
operations'' or ``names''.
|
|
81 |
*};
|
|
82 |
|
8890
|
83 |
end; |