doc-src/TutorialI/Types/document/Overloading1.tex
author nipkow
Tue, 01 May 2001 22:26:55 +0200
changeset 11277 a2bff98d6e5d
parent 11161 166f7d87b37f
child 11310 51e70b7bc315
permissions -rw-r--r--
*** empty log message ***
nipkow@10305
     1
%
nipkow@10305
     2
\begin{isabellebody}%
nipkow@10305
     3
\def\isabellecontext{Overloading{\isadigit{1}}}%
nipkow@10305
     4
%
paulson@10878
     5
\isamarkupsubsubsection{Controlled Overloading with Type Classes%
paulson@10397
     6
}
nipkow@10305
     7
%
nipkow@10305
     8
\begin{isamarkuptext}%
nipkow@10305
     9
We now start with the theory of ordering relations, which we want to phrase
nipkow@11277
    10
in terms of the two binary symbols \isa{{\isacharless}{\isacharless}} and \isa{{\isacharless}{\isacharless}{\isacharequal}}
nipkow@11277
    11
to avoid clashes with \isa{{\isacharless}} and \isa{{\isasymle}} in theory \isa{Main}. To restrict the application of \isa{{\isacharless}{\isacharless}} and \isa{{\isacharless}{\isacharless}{\isacharequal}} we
nipkow@10305
    12
introduce the class \isa{ordrel}:%
nipkow@10305
    13
\end{isamarkuptext}%
nipkow@10305
    14
\isacommand{axclass}\ ordrel\ {\isacharless}\ {\isachardoublequote}term{\isachardoublequote}%
nipkow@10305
    15
\begin{isamarkuptext}%
nipkow@10305
    16
\noindent
nipkow@10328
    17
This introduces a new class \isa{ordrel} and makes it a subclass of
nipkow@10328
    18
the predefined class \isa{term}\footnote{The quotes around \isa{term}
nipkow@11277
    19
simply avoid the clash with the command \isacommand{term}.} --- \isa{term}
nipkow@10328
    20
is the class of all HOL types, like ``Object'' in Java.
nipkow@10305
    21
This is a degenerate form of axiomatic type class without any axioms.
nipkow@10305
    22
Its sole purpose is to restrict the use of overloaded constants to meaningful
nipkow@10305
    23
instances:%
nipkow@10305
    24
\end{isamarkuptext}%
nipkow@10328
    25
\isacommand{consts}\ less\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcolon}{\isacharcolon}ordrel{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ bool{\isachardoublequote}\ \ \ \ \ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequote}{\isacharless}{\isacharless}{\isachardoublequote}\ \ {\isadigit{5}}{\isadigit{0}}{\isacharparenright}\isanewline
nipkow@10328
    26
\ \ \ \ \ \ \ le\ \ \ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcolon}{\isacharcolon}ordrel{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ bool{\isachardoublequote}\ \ \ \ \ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequote}{\isacharless}{\isacharless}{\isacharequal}{\isachardoublequote}\ {\isadigit{5}}{\isadigit{0}}{\isacharparenright}%
nipkow@10328
    27
\begin{isamarkuptext}%
nipkow@10328
    28
\noindent
nipkow@10396
    29
Note that only one occurrence of a type variable in a type needs to be
nipkow@10396
    30
constrained with a class; the constraint is propagated to the other
nipkow@10396
    31
occurrences automatically.
nipkow@10396
    32
nipkow@10328
    33
So far there is not a single type of class \isa{ordrel}. To breathe life
nipkow@10328
    34
into \isa{ordrel} we need to declare a type to be an \bfindex{instance} of
nipkow@10328
    35
\isa{ordrel}:%
nipkow@10328
    36
\end{isamarkuptext}%
nipkow@10328
    37
\isacommand{instance}\ bool\ {\isacharcolon}{\isacharcolon}\ ordrel%
nipkow@10328
    38
\begin{isamarkuptxt}%
nipkow@10328
    39
\noindent
nipkow@10328
    40
Command \isacommand{instance} actually starts a proof, namely that
nipkow@10328
    41
\isa{bool} satisfies all axioms of \isa{ordrel}.
nipkow@10328
    42
There are none, but we still need to finish that proof, which we do
nipkow@10328
    43
by invoking a fixed predefined method:%
nipkow@10328
    44
\end{isamarkuptxt}%
nipkow@10328
    45
\isacommand{by}\ intro{\isacharunderscore}classes%
nipkow@10328
    46
\begin{isamarkuptext}%
nipkow@10328
    47
\noindent
nipkow@10328
    48
More interesting \isacommand{instance} proofs will arise below
nipkow@10328
    49
in the context of proper axiomatic type classes.
nipkow@10328
    50
nipkow@11161
    51
Although terms like \isa{False\ {\isacharless}{\isacharless}{\isacharequal}\ P} are now legal, we still need to say
nipkow@10328
    52
what the relation symbols actually mean at type \isa{bool}:%
nipkow@10328
    53
\end{isamarkuptext}%
nipkow@10305
    54
\isacommand{defs}\ {\isacharparenleft}\isakeyword{overloaded}{\isacharparenright}\isanewline
nipkow@10305
    55
le{\isacharunderscore}bool{\isacharunderscore}def{\isacharcolon}\ \ {\isachardoublequote}P\ {\isacharless}{\isacharless}{\isacharequal}\ Q\ {\isasymequiv}\ P\ {\isasymlongrightarrow}\ Q{\isachardoublequote}\isanewline
nipkow@10305
    56
less{\isacharunderscore}bool{\isacharunderscore}def{\isacharcolon}\ {\isachardoublequote}P\ {\isacharless}{\isacharless}\ Q\ {\isasymequiv}\ {\isasymnot}P\ {\isasymand}\ Q{\isachardoublequote}%
nipkow@10305
    57
\begin{isamarkuptext}%
nipkow@10328
    58
\noindent
nipkow@10328
    59
Now \isa{False\ {\isacharless}{\isacharless}{\isacharequal}\ P} is provable%
nipkow@10305
    60
\end{isamarkuptext}%
nipkow@10328
    61
\isacommand{lemma}\ {\isachardoublequote}False\ {\isacharless}{\isacharless}{\isacharequal}\ P{\isachardoublequote}\isanewline
nipkow@10305
    62
\isacommand{by}{\isacharparenleft}simp\ add{\isacharcolon}\ le{\isacharunderscore}bool{\isacharunderscore}def{\isacharparenright}%
nipkow@10305
    63
\begin{isamarkuptext}%
nipkow@10305
    64
\noindent
nipkow@10305
    65
whereas \isa{{\isacharbrackleft}{\isacharbrackright}\ {\isacharless}{\isacharless}{\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} is not even welltyped. In order to make it welltyped
nipkow@10305
    66
we need to make lists a type of class \isa{ordrel}:%
nipkow@10305
    67
\end{isamarkuptext}%
nipkow@10305
    68
\end{isabellebody}%
nipkow@10305
    69
%%% Local Variables:
nipkow@10305
    70
%%% mode: latex
nipkow@10305
    71
%%% TeX-master: "root"
nipkow@10305
    72
%%% End: