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