doc-src/TutorialI/Types/document/Overloading.tex
author huffman
Wed, 18 Nov 2009 15:51:35 -0800
changeset 33776 5048b02c2bbb
parent 31682 358cdcdf56d2
child 38325 6daf896bca5e
permissions -rw-r--r--
automate proofs of REP equations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10305
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
     1
%
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
     2
\begin{isabellebody}%
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
     3
\def\isabellecontext{Overloading}%
17056
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
     4
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
     5
\isadelimtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
     6
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
     7
\endisadelimtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
     8
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
     9
\isatagtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    10
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    11
\endisatagtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    12
{\isafoldtheory}%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    13
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    14
\isadelimtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    15
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    16
\endisadelimtheory
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    17
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    18
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    19
Type classes allow \emph{overloading}; thus a constant may
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    20
have multiple definitions at non-overlapping types.%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    21
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    22
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    23
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    24
\isamarkupsubsubsection{Overloading%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    25
}
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    26
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    27
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    28
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    29
We can introduce a binary infix addition operator \isa{{\isasymotimes}}
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    30
for arbitrary types by means of a type class:%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    31
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    32
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    33
\isacommand{class}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    34
\ plus\ {\isacharequal}\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    35
\ \ \isakeyword{fixes}\ plus\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}{\isasymoplus}{\isachardoublequoteclose}\ {\isadigit{7}}{\isadigit{0}}{\isacharparenright}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    36
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    37
\noindent This introduces a new class \isa{plus},
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    38
along with a constant \isa{plus} with nice infix syntax.
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    39
\isa{plus} is also named \emph{class operation}.  The type
31682
358cdcdf56d2 corrected some issues
haftmann
parents: 31678
diff changeset
    40
of \isa{plus} carries a class constraint \isa{{\isachardoublequote}{\isacharprime}a\ {\isacharcolon}{\isacharcolon}\ plus{\isachardoublequote}} on its type variable, meaning that only types of class
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    41
\isa{plus} can be instantiated for \isa{{\isachardoublequote}{\isacharprime}a{\isachardoublequote}}.
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    42
To breathe life into \isa{plus} we need to declare a type
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    43
to be an \bfindex{instance} of \isa{plus}:%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    44
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    45
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    46
\isacommand{instantiation}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    47
\ nat\ {\isacharcolon}{\isacharcolon}\ plus\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    48
\isakeyword{begin}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    49
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    50
\noindent Command \isacommand{instantiation} opens a local
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    51
theory context.  Here we can now instantiate \isa{plus} on
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    52
\isa{nat}:%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    53
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    54
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    55
\isacommand{primrec}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    56
\ plus{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    57
\ \ \ \ {\isachardoublequoteopen}{\isacharparenleft}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}\ {\isasymoplus}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    58
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymoplus}\ n\ {\isacharequal}\ Suc\ {\isacharparenleft}m\ {\isasymoplus}\ n{\isacharparenright}{\isachardoublequoteclose}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    59
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    60
\noindent Note that the name \isa{plus} carries a
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    61
suffix \isa{{\isacharunderscore}nat}; by default, the local name of a class operation
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    62
\isa{f} to be instantiated on type constructor \isa{{\isasymkappa}} is mangled
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    63
as \isa{f{\isacharunderscore}{\isasymkappa}}.  In case of uncertainty, these names may be inspected
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    64
using the \hyperlink{command.print-context}{\mbox{\isa{\isacommand{print{\isacharunderscore}context}}}} command or the corresponding
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    65
ProofGeneral button.
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    66
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    67
Although class \isa{plus} has no axioms, the instantiation must be
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    68
formally concluded by a (trivial) instantiation proof ``..'':%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    69
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    70
\isamarkuptrue%
17175
1eced27ee0e1 updated;
wenzelm
parents: 17056
diff changeset
    71
\isacommand{instance}\isamarkupfalse%
17056
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    72
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    73
\isadelimproof
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    74
\ %
17056
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    75
\endisadelimproof
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    76
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    77
\isatagproof
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    78
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    79
%
17056
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    80
\endisatagproof
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    81
{\isafoldproof}%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    82
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    83
\isadelimproof
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    84
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
    85
\endisadelimproof
11866
fbd097aec213 updated;
wenzelm
parents: 11494
diff changeset
    86
%
10305
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
    87
\begin{isamarkuptext}%
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    88
\noindent More interesting \isacommand{instance} proofs will
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    89
arise below.
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    90
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    91
The instantiation is finished by an explicit%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    92
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    93
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    94
\isacommand{end}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    95
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    96
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    97
\noindent From now on, terms like \isa{Suc\ {\isacharparenleft}m\ {\isasymoplus}\ {\isadigit{2}}{\isacharparenright}} are
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    98
legal.%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
    99
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   100
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   101
\isacommand{instantiation}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   102
\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}plus{\isacharcomma}\ plus{\isacharparenright}\ plus\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   103
\isakeyword{begin}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   104
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   105
\noindent Here we instantiate the product type \isa{{\isacharasterisk}} to
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   106
class \isa{plus}, given that its type arguments are of
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   107
class \isa{plus}:%
10305
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
   108
\end{isamarkuptext}%
17175
1eced27ee0e1 updated;
wenzelm
parents: 17056
diff changeset
   109
\isamarkuptrue%
31678
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   110
\isacommand{fun}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   111
\ plus{\isacharunderscore}prod\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   112
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}\ {\isasymoplus}\ {\isacharparenleft}w{\isacharcomma}\ z{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}x\ {\isasymoplus}\ w{\isacharcomma}\ y\ {\isasymoplus}\ z{\isacharparenright}{\isachardoublequoteclose}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   113
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   114
\noindent Obviously, overloaded specifications may include
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   115
recursion over the syntactic structure of types.%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   116
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   117
\isamarkuptrue%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   118
\isacommand{instance}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   119
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   120
\isadelimproof
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   121
\ %
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   122
\endisadelimproof
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   123
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   124
\isatagproof
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   125
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   126
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   127
\endisatagproof
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   128
{\isafoldproof}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   129
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   130
\isadelimproof
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   131
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   132
\endisadelimproof
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   133
\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   134
\isanewline
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   135
\isacommand{end}\isamarkupfalse%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   136
%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   137
\begin{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   138
\noindent This way we have encoded the canonical lifting of
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   139
binary operations to products by means of type classes.%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   140
\end{isamarkuptext}%
752f23a37240 reworked section on type classes
haftmann
parents: 27169
diff changeset
   141
\isamarkuptrue%
27169
89d5f117add3 fixed type
nipkow
parents: 17187
diff changeset
   142
%
17056
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   143
\isadelimtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   144
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   145
\endisadelimtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   146
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   147
\isatagtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   148
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   149
\endisatagtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   150
{\isafoldtheory}%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   151
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   152
\isadelimtheory
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   153
%
05fc32a23b8b updated;
wenzelm
parents: 16353
diff changeset
   154
\endisadelimtheory
11866
fbd097aec213 updated;
wenzelm
parents: 11494
diff changeset
   155
\end{isabellebody}%
10305
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
   156
%%% Local Variables:
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
   157
%%% mode: latex
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
   158
%%% TeX-master: "root"
adff80268127 *** empty log message ***
nipkow
parents:
diff changeset
   159
%%% End: