doc-src/IsarAdvanced/Functions/Thy/document/Functions.tex
author wenzelm
Tue, 05 Jun 2007 16:26:04 +0200
changeset 23252 67268bb40b21
parent 23188 595a0e24bd8e
child 23805 953eb3c5f793
permissions -rw-r--r--
Semiring normalization and Groebner Bases.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     1
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     2
\begin{isabellebody}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     3
\def\isabellecontext{Functions}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     4
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     5
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     6
\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     7
\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     8
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
     9
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    10
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    11
\isatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    12
\isacommand{theory}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    13
\ Functions\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    14
\isakeyword{imports}\ Main\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    15
\isakeyword{begin}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    16
\endisatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    17
{\isafoldtheory}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    18
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    19
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    20
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    21
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    22
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    23
\isamarkupsection{Function Definitions for Dummies%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    24
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    25
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    26
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    27
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    28
In most cases, defining a recursive function is just as simple as other definitions:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    29
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    30
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    31
\isacommand{fun}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    32
\ fib\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    33
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    34
\ \ {\isachardoublequoteopen}fib\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    35
{\isacharbar}\ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
21346
c8aa120fa05d updated
krauss
parents: 21212
diff changeset
    36
{\isacharbar}\ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ fib\ n\ {\isacharplus}\ fib\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isachardoublequoteclose}%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    37
\begin{isamarkuptext}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    38
The syntax is rather self-explanatory: We introduce a function by
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    39
  giving its name, its type and a set of defining recursive
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    40
  equations. Note that the function is not primitive recursive.%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    41
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    42
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    43
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    44
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    45
The function always terminates, since its argument gets smaller in
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    46
  every recursive call. 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    47
  Since HOL is a logic of total functions, termination is a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    48
  fundamental requirement to prevent inconsistencies\footnote{From the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    49
  \qt{definition} \isa{f{\isacharparenleft}n{\isacharparenright}\ {\isacharequal}\ f{\isacharparenleft}n{\isacharparenright}\ {\isacharplus}\ {\isadigit{1}}} we could prove 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    50
  \isa{{\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}} by subtracting \isa{f{\isacharparenleft}n{\isacharparenright}} on both sides.}.
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    51
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    52
  Isabelle tries to prove termination automatically when a definition
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    53
  is made. In \S\ref{termination}, we will look at cases where this
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    54
  fails and see what to do then.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    55
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    56
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    57
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    58
\isamarkupsubsection{Pattern matching%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    59
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    60
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    61
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    62
\begin{isamarkuptext}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    63
\label{patmatch}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    64
  Like in functional programming, we can use pattern matching to
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
    65
  define functions. At the moment we will only consider \emph{constructor
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    66
  patterns}, which only consist of datatype constructors and
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    67
  (linear occurrences of) variables.
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    68
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    69
  If patterns overlap, the order of the equations is taken into
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    70
  account. The following function inserts a fixed element between any
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    71
  two elements of a list:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    72
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    73
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    74
\isacommand{fun}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    75
\ sep\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    76
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    77
\ \ {\isachardoublequoteopen}sep\ a\ {\isacharparenleft}x{\isacharhash}y{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x\ {\isacharhash}\ a\ {\isacharhash}\ sep\ a\ {\isacharparenleft}y\ {\isacharhash}\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    78
{\isacharbar}\ {\isachardoublequoteopen}sep\ a\ xs\ \ \ \ \ \ \ {\isacharequal}\ xs{\isachardoublequoteclose}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    79
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
    80
Overlapping patterns are interpreted as \qt{increments} to what is
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    81
  already there: The second equation is only meant for the cases where
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    82
  the first one does not match. Consequently, Isabelle replaces it
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    83
  internally by the remaining cases, making the patterns disjoint:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    84
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    85
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    86
\isacommand{thm}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    87
\ sep{\isachardot}simps%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    88
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    89
\begin{isabelle}%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    90
sep\ a\ {\isacharparenleft}x\ {\isacharhash}\ y\ {\isacharhash}\ xs{\isacharparenright}\ {\isacharequal}\ x\ {\isacharhash}\ a\ {\isacharhash}\ sep\ a\ {\isacharparenleft}y\ {\isacharhash}\ xs{\isacharparenright}\isasep\isanewline%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    91
sep\ a\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\isasep\isanewline%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    92
sep\ a\ {\isacharbrackleft}v{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}v{\isacharbrackright}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    93
\end{isabelle}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    94
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    95
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    96
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    97
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    98
The equations from function definitions are automatically used in
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    99
  simplification:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   100
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   101
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   102
\isacommand{lemma}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   103
\ {\isachardoublequoteopen}sep\ {\isadigit{0}}\ {\isacharbrackleft}{\isadigit{1}}{\isacharcomma}\ {\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isadigit{1}}{\isacharcomma}\ {\isadigit{0}}{\isacharcomma}\ {\isadigit{2}}{\isacharcomma}\ {\isadigit{0}}{\isacharcomma}\ {\isadigit{3}}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   104
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   105
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   106
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   107
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   108
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   109
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   110
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   111
\ simp%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   112
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   113
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   114
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   115
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   116
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   117
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   118
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   119
\isamarkupsubsection{Induction%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   120
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   121
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   122
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   123
\begin{isamarkuptext}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   124
Isabelle provides customized induction rules for recursive functions.  
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   125
  See \cite[\S3.5.4]{isa-tutorial}. \fixme{Cases?}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   126
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   127
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   128
  With the \cmd{fun} command, you can define about 80\% of the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   129
  functions that occur in practice. The rest of this tutorial explains
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   130
  the remaining 20\%.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   131
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   132
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   133
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   134
\isamarkupsection{fun vs.\ function%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   135
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   136
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   137
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   138
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   139
The \cmd{fun} command provides a
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   140
  convenient shorthand notation for simple function definitions. In
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   141
  this mode, Isabelle tries to solve all the necessary proof obligations
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   142
  automatically. If a proof fails, the definition is
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   143
  rejected. This can either mean that the definition is indeed faulty,
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   144
  or that the default proof procedures are just not smart enough (or
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   145
  rather: not designed) to handle the definition.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   146
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   147
  By expanding the abbreviation to the more verbose \cmd{function} command, these proof obligations become visible and can be analyzed or
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   148
  solved manually. The expansion from \cmd{fun} to \cmd{function} is as follows:
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   149
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   150
\end{isamarkuptext}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   151
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   152
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   153
\[\left[\;\begin{minipage}{0.25\textwidth}\vspace{6pt}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   154
\cmd{fun} \isa{f\ {\isacharcolon}{\isacharcolon}\ {\isasymtau}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   155
\cmd{where}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   156
\hspace*{2ex}{\it equations}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   157
\hspace*{2ex}\vdots\vspace*{6pt}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   158
\end{minipage}\right]
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   159
\quad\equiv\quad
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   160
\left[\;\begin{minipage}{0.45\textwidth}\vspace{6pt}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   161
\cmd{function} \isa{{\isacharparenleft}}\cmd{sequential}\isa{{\isacharparenright}\ f\ {\isacharcolon}{\isacharcolon}\ {\isasymtau}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   162
\cmd{where}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   163
\hspace*{2ex}{\it equations}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   164
\hspace*{2ex}\vdots\\%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   165
\cmd{by} \isa{pat{\isacharunderscore}completeness\ auto}\\%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   166
\cmd{termination by} \isa{lexicographic{\isacharunderscore}order}\vspace{6pt}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   167
\end{minipage}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   168
\right]\]
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   169
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   170
\begin{isamarkuptext}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   171
  \vspace*{1em}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   172
  \noindent Some details have now become explicit:
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   173
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   174
  \begin{enumerate}
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   175
  \item The \cmd{sequential} option enables the preprocessing of
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   176
  pattern overlaps we already saw. Without this option, the equations
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   177
  must already be disjoint and complete. The automatic completion only
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   178
  works with constructor patterns.
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   179
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   180
  \item A function definition produces a proof obligation which
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   181
  expresses completeness and compatibility of patterns (we talk about
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   182
  this later). The combination of the methods \isa{pat{\isacharunderscore}completeness} and
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   183
  \isa{auto} is used to solve this proof obligation.
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   184
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   185
  \item A termination proof follows the definition, started by the
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   186
  \cmd{termination} command. This will be explained in \S\ref{termination}.
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   187
 \end{enumerate}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   188
  Whenever a \cmd{fun} command fails, it is usually a good idea to
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   189
  expand the syntax to the more verbose \cmd{function} form, to see
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   190
  what is actually going on.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   191
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   192
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   193
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   194
\isamarkupsection{Termination%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   195
}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   196
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   197
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   198
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   199
\label{termination}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   200
  The \isa{lexicographic{\isacharunderscore}order} method can prove termination of a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   201
  certain class of functions by searching for a suitable lexicographic
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   202
  combination of size measures. Of course, not all functions have such
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   203
  a simple termination argument.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   204
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   205
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   206
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   207
\isamarkupsubsection{The {\tt relation} method%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   208
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   209
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   210
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   211
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   212
Consider the following function, which sums up natural numbers up to
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   213
  \isa{N}, using a counter \isa{i}:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   214
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   215
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   216
\isacommand{function}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   217
\ sum\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   218
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   219
\ \ {\isachardoublequoteopen}sum\ i\ N\ {\isacharequal}\ {\isacharparenleft}if\ i\ {\isachargreater}\ N\ then\ {\isadigit{0}}\ else\ i\ {\isacharplus}\ sum\ {\isacharparenleft}Suc\ i{\isacharparenright}\ N{\isacharparenright}{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   220
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   221
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   222
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   223
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   224
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   225
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   226
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   227
\ pat{\isacharunderscore}completeness\ auto%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   228
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   229
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   230
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   231
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   232
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   233
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   234
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   235
\begin{isamarkuptext}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   236
\noindent The \isa{lexicographic{\isacharunderscore}order} method fails on this example, because none of the
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   237
  arguments decreases in the recursive call.
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   238
  % FIXME: simps and induct only appear after "termination"
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   239
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   240
  The easiest way of doing termination proofs is to supply a wellfounded
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   241
  relation on the argument type, and to show that the argument
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   242
  decreases in every recursive call. 
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   243
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   244
  The termination argument for \isa{sum} is based on the fact that
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   245
  the \emph{difference} between \isa{i} and \isa{N} gets
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   246
  smaller in every step, and that the recursion stops when \isa{i}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   247
  is greater then \isa{n}. Phrased differently, the expression 
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   248
  \isa{N\ {\isacharplus}\ {\isadigit{1}}\ {\isacharminus}\ i} decreases in every recursive call.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   249
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   250
  We can use this expression as a measure function suitable to prove termination.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   251
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   252
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   253
\isacommand{termination}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   254
\ sum\isanewline
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   255
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   256
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   257
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   258
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   259
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   260
\isatagproof
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   261
\isacommand{apply}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   262
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}i{\isacharcomma}N{\isacharparenright}{\isachardot}\ N\ {\isacharplus}\ {\isadigit{1}}\ {\isacharminus}\ i{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   263
\begin{isamarkuptxt}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   264
The \cmd{termination} command sets up the termination goal for the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   265
  specified function \isa{sum}. If the function name is omitted, it
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   266
  implicitly refers to the last function definition.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   267
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   268
  The \isa{relation} method takes a relation of
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   269
  type \isa{{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ set}, where \isa{{\isacharprime}a} is the argument type of
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   270
  the function. If the function has multiple curried arguments, then
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   271
  these are packed together into a tuple, as it happened in the above
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   272
  example.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   273
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   274
  The predefined function \isa{measure{\isasymColon}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ set} constructs a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   275
  wellfounded relation from a mapping into the natural numbers (a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   276
  \emph{measure function}). 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   277
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   278
  After the invocation of \isa{relation}, we must prove that (a)
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   279
  the relation we supplied is wellfounded, and (b) that the arguments
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   280
  of recursive calls indeed decrease with respect to the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   281
  relation:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   282
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   283
  \begin{isabelle}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   284
\ {\isadigit{1}}{\isachardot}\ wf\ {\isacharparenleft}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}i{\isacharcomma}\ N{\isacharparenright}{\isachardot}\ N\ {\isacharplus}\ {\isadigit{1}}\ {\isacharminus}\ i{\isacharparenright}{\isacharparenright}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   285
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}i\ N{\isachardot}\ {\isasymnot}\ N\ {\isacharless}\ i\ {\isasymLongrightarrow}\ {\isacharparenleft}{\isacharparenleft}Suc\ i{\isacharcomma}\ N{\isacharparenright}{\isacharcomma}\ i{\isacharcomma}\ N{\isacharparenright}\ {\isasymin}\ measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}i{\isacharcomma}\ N{\isacharparenright}{\isachardot}\ N\ {\isacharplus}\ {\isadigit{1}}\ {\isacharminus}\ i{\isacharparenright}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   286
\end{isabelle}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   287
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   288
  These goals are all solved by \isa{auto}:%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   289
\end{isamarkuptxt}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   290
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   291
\isacommand{apply}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   292
\ auto\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   293
\isacommand{done}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   294
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   295
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   296
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   297
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   298
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   299
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   300
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   301
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   302
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   303
Let us complicate the function a little, by adding some more
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   304
  recursive calls:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   305
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   306
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   307
\isacommand{function}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   308
\ foo\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   309
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   310
\ \ {\isachardoublequoteopen}foo\ i\ N\ {\isacharequal}\ {\isacharparenleft}if\ i\ {\isachargreater}\ N\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   311
\ \ \ \ \ \ \ \ \ \ \ \ \ \ then\ {\isacharparenleft}if\ N\ {\isacharequal}\ {\isadigit{0}}\ then\ {\isadigit{0}}\ else\ foo\ {\isadigit{0}}\ {\isacharparenleft}N\ {\isacharminus}\ {\isadigit{1}}{\isacharparenright}{\isacharparenright}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   312
\ \ \ \ \ \ \ \ \ \ \ \ \ \ else\ i\ {\isacharplus}\ foo\ {\isacharparenleft}Suc\ i{\isacharparenright}\ N{\isacharparenright}{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   313
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   314
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   315
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   316
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   317
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   318
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   319
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   320
\ pat{\isacharunderscore}completeness\ auto%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   321
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   322
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   323
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   324
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   325
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   326
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   327
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   328
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   329
When \isa{i} has reached \isa{N}, it starts at zero again
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   330
  and \isa{N} is decremented.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   331
  This corresponds to a nested
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   332
  loop where one index counts up and the other down. Termination can
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   333
  be proved using a lexicographic combination of two measures, namely
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   334
  the value of \isa{N} and the above difference. The \isa{measures} combinator generalizes \isa{measure} by taking a
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   335
  list of measure functions.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   336
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   337
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   338
\isacommand{termination}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   339
\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   340
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   341
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   342
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   343
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   344
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   345
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   346
\isacommand{by}\isamarkupfalse%
21346
c8aa120fa05d updated
krauss
parents: 21212
diff changeset
   347
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measures\ {\isacharbrackleft}{\isasymlambda}{\isacharparenleft}i{\isacharcomma}\ N{\isacharparenright}{\isachardot}\ N{\isacharcomma}\ {\isasymlambda}{\isacharparenleft}i{\isacharcomma}N{\isacharparenright}{\isachardot}\ N\ {\isacharplus}\ {\isadigit{1}}\ {\isacharminus}\ i{\isacharbrackright}{\isachardoublequoteclose}{\isacharparenright}\ auto%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   348
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   349
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   350
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   351
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   352
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   353
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   354
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   355
\isamarkupsubsection{How \isa{lexicographic{\isacharunderscore}order} works%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   356
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   357
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   358
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   359
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   360
To see how the automatic termination proofs work, let's look at an
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   361
  example where it fails\footnote{For a detailed discussion of the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   362
  termination prover, see \cite{bulwahnKN07}}:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   363
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   364
\end{isamarkuptext}  
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   365
\cmd{fun} \isa{fails\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymRightarrow}\ nat\ list\ {\isasymRightarrow}\ nat{\isachardoublequote}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   366
\cmd{where}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   367
\hspace*{2ex}\isa{{\isachardoublequote}fails\ a\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ a{\isachardoublequote}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   368
|\hspace*{1.5ex}\isa{{\isachardoublequote}fails\ a\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ fails\ {\isacharparenleft}x\ {\isacharplus}\ a{\isacharparenright}\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}{\isachardoublequote}}\\
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   369
\begin{isamarkuptext}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   370
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   371
\noindent Isabelle responds with the following error:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   372
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   373
\begin{isabelle}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   374
*** Could not find lexicographic termination order:\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   375
*** \ \ \ \ 1\ \ 2  \newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   376
*** a:  N   <= \newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   377
*** Calls:\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   378
*** a) \isa{{\isacharparenleft}a{\isacharcomma}\ x\ {\isacharhash}\ xs{\isacharparenright}\ {\isacharminus}{\isacharminus}{\isachargreater}{\isachargreater}\ {\isacharparenleft}x\ {\isacharplus}\ a{\isacharcomma}\ x\ {\isacharhash}\ xs{\isacharparenright}}\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   379
*** Measures:\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   380
*** 1) \isa{{\isasymlambda}x{\isachardot}\ size\ {\isacharparenleft}fst\ x{\isacharparenright}}\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   381
*** 2) \isa{{\isasymlambda}x{\isachardot}\ size\ {\isacharparenleft}snd\ x{\isacharparenright}}\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   382
*** Unfinished subgoals:\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   383
*** \isa{{\isasymAnd}a\ x\ xs{\isachardot}}\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   384
*** \quad \isa{{\isacharparenleft}x{\isachardot}\ size\ {\isacharparenleft}fst\ x{\isacharparenright}{\isacharparenright}\ {\isacharparenleft}x\ {\isacharplus}\ a{\isacharcomma}\ x\ {\isacharhash}\ xs{\isacharparenright}}\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   385
***  \quad \isa{{\isasymle}\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ size\ {\isacharparenleft}fst\ x{\isacharparenright}{\isacharparenright}\ {\isacharparenleft}a{\isacharcomma}\ x\ {\isacharhash}\ xs{\isacharparenright}}\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   386
***  \isa{{\isadigit{1}}{\isachardot}\ {\isasymAnd}x{\isachardot}\ x\ {\isacharequal}\ {\isadigit{0}}}\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   387
*** At command "fun".\newline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   388
\end{isabelle}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   389
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   390
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   391
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   392
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   393
The the key to this error message is the matrix at the top. The rows
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   394
  of that matrix correspond to the different recursive calls (In our
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   395
  case, there is just one). The columns are the function's arguments 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   396
  (expressed through different measure functions, which map the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   397
  argument tuple to a natural number). 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   398
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   399
  The contents of the matrix summarize what is known about argument
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   400
  descents: The second argument has a weak descent (\isa{{\isacharless}{\isacharequal}}) at the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   401
  recursive call, and for the first argument nothing could be proved,
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   402
  which is expressed by \isa{N}. In general, there are the values
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   403
  \isa{{\isacharless}}, \isa{{\isacharless}{\isacharequal}} and \isa{N}.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   404
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   405
  For the failed proof attempts, the unfinished subgoals are also
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   406
  printed. Looking at these will often point us to a missing lemma.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   407
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   408
%  As a more real example, here is quicksort:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   409
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   410
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   411
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   412
\isamarkupsection{Mutual Recursion%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   413
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   414
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   415
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   416
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   417
If two or more functions call one another mutually, they have to be defined
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   418
  in one step. Here are \isa{even} and \isa{odd}:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   419
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   420
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   421
\isacommand{function}\isamarkupfalse%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   422
\ even\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   423
\ \ \ \ \isakeyword{and}\ odd\ \ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   424
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   425
\ \ {\isachardoublequoteopen}even\ {\isadigit{0}}\ {\isacharequal}\ True{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   426
{\isacharbar}\ {\isachardoublequoteopen}odd\ {\isadigit{0}}\ {\isacharequal}\ False{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   427
{\isacharbar}\ {\isachardoublequoteopen}even\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ odd\ n{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   428
{\isacharbar}\ {\isachardoublequoteopen}odd\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ even\ n{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   429
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   430
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   431
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   432
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   433
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   434
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   435
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   436
\ pat{\isacharunderscore}completeness\ auto%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   437
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   438
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   439
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   440
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   441
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   442
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   443
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   444
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   445
To eliminate the mutual dependencies, Isabelle internally
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   446
  creates a single function operating on the sum
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   447
  type \isa{nat\ {\isacharplus}\ nat}. Then, \isa{even} and \isa{odd} are
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   448
  defined as projections. Consequently, termination has to be proved
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   449
  simultaneously for both functions, by specifying a measure on the
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   450
  sum type:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   451
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   452
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   453
\isacommand{termination}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   454
\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   455
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   456
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   457
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   458
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   459
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   460
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   461
\isacommand{by}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   462
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ case\ x\ of\ Inl\ n\ {\isasymRightarrow}\ n\ {\isacharbar}\ Inr\ n\ {\isasymRightarrow}\ n{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\ auto%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   463
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   464
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   465
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   466
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   467
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   468
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   469
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   470
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   471
We could also have used \isa{lexicographic{\isacharunderscore}order}, which
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   472
  supports mutual recursive termination proofs to a certain extent.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   473
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   474
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   475
%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   476
\isamarkupsubsection{Induction for mutual recursion%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   477
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   478
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   479
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   480
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   481
When functions are mutually recursive, proving properties about them
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   482
  generally requires simultaneous induction. The induction rule \isa{even{\isacharunderscore}odd{\isachardot}induct}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   483
  generated from the above definition reflects this.
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   484
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   485
  Let us prove something about \isa{even} and \isa{odd}:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   486
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   487
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   488
\isacommand{lemma}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   489
\ even{\isacharunderscore}odd{\isacharunderscore}mod{\isadigit{2}}{\isacharcolon}\isanewline
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   490
\ \ {\isachardoublequoteopen}even\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   491
\ \ {\isachardoublequoteopen}odd\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   492
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   493
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   494
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   495
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   496
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   497
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   498
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   499
We apply simultaneous induction, specifying the induction variable
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   500
  for both goals, separated by \cmd{and}:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   501
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   502
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   503
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   504
\ {\isacharparenleft}induct\ n\ \isakeyword{and}\ n\ rule{\isacharcolon}\ even{\isacharunderscore}odd{\isachardot}induct{\isacharparenright}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   505
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   506
We get four subgoals, which correspond to the clauses in the
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   507
  definition of \isa{even} and \isa{odd}:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   508
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   509
\ {\isadigit{1}}{\isachardot}\ even\ {\isadigit{0}}\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   510
\ {\isadigit{2}}{\isachardot}\ odd\ {\isadigit{0}}\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{1}}{\isacharparenright}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   511
\ {\isadigit{3}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ odd\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{1}}{\isacharparenright}\ {\isasymLongrightarrow}\ even\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Suc\ n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   512
\ {\isadigit{4}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ even\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\ {\isasymLongrightarrow}\ odd\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Suc\ n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{1}}{\isacharparenright}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   513
\end{isabelle}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   514
  Simplification solves the first two goals, leaving us with two
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   515
  statements about the \isa{mod} operation to prove:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   516
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   517
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   518
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   519
\ simp{\isacharunderscore}all%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   520
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   521
\begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   522
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ odd\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isacharparenright}\ {\isasymLongrightarrow}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Suc\ n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   523
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ even\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\ {\isasymLongrightarrow}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Suc\ n\ mod\ {\isadigit{2}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isacharparenright}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   524
\end{isabelle} 
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   525
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   526
  \noindent These can be handeled by the descision procedure for
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   527
  arithmethic.%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   528
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   529
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   530
\isacommand{apply}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   531
\ presburger\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   532
\isamarkupcmt{\fixme{arith}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   533
}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   534
\isanewline
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   535
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   536
\ presburger\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   537
\isacommand{done}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   538
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   539
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   540
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   541
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   542
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   543
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   544
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   545
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   546
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   547
In proofs like this, the simultaneous induction is really essential:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   548
  Even if we are just interested in one of the results, the other
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   549
  one is necessary to strengthen the induction hypothesis. If we leave
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   550
  out the statement about \isa{odd} (by substituting it with \isa{True}), the same proof fails:%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   551
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   552
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   553
\isacommand{lemma}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   554
\ failed{\isacharunderscore}attempt{\isacharcolon}\isanewline
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   555
\ \ {\isachardoublequoteopen}even\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   556
\ \ {\isachardoublequoteopen}True{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   557
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   558
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   559
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   560
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   561
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   562
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   563
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   564
\ {\isacharparenleft}induct\ n\ rule{\isacharcolon}\ even{\isacharunderscore}odd{\isachardot}induct{\isacharparenright}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   565
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   566
\noindent Now the third subgoal is a dead end, since we have no
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   567
  useful induction hypothesis available:
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   568
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   569
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   570
\ {\isadigit{1}}{\isachardot}\ even\ {\isadigit{0}}\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   571
\ {\isadigit{2}}{\isachardot}\ True\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   572
\ {\isadigit{3}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ True\ {\isasymLongrightarrow}\ even\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Suc\ n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   573
\ {\isadigit{4}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ even\ n\ {\isacharequal}\ {\isacharparenleft}n\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}{\isacharparenright}\ {\isasymLongrightarrow}\ True%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   574
\end{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   575
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   576
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   577
\isacommand{oops}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   578
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   579
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   580
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   581
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   582
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   583
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   584
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   585
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   586
\isamarkupsection{General pattern matching%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   587
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   588
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   589
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   590
\isamarkupsubsection{Avoiding automatic pattern splitting%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   591
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   592
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   593
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   594
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   595
Up to now, we used pattern matching only on datatypes, and the
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   596
  patterns were always disjoint and complete, and if they weren't,
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   597
  they were made disjoint automatically like in the definition of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   598
  \isa{sep} in \S\ref{patmatch}.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   599
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   600
  This automatic splitting can significantly increase the number of
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   601
  equations involved, and this is not always desirable. The following
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   602
  example shows the problem:
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   603
  
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   604
  Suppose we are modelling incomplete knowledge about the world by a
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   605
  three-valued datatype, which has values \isa{T}, \isa{F}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   606
  and \isa{X} for true, false and uncertain propositions, respectively.%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   607
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   608
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   609
\isacommand{datatype}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   610
\ P{\isadigit{3}}\ {\isacharequal}\ T\ {\isacharbar}\ F\ {\isacharbar}\ X%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   611
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   612
\noindent Then the conjunction of such values can be defined as follows:%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   613
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   614
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   615
\isacommand{fun}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   616
\ And\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}P{\isadigit{3}}\ {\isasymRightarrow}\ P{\isadigit{3}}\ {\isasymRightarrow}\ P{\isadigit{3}}{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   617
\isakeyword{where}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   618
\ \ {\isachardoublequoteopen}And\ T\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   619
{\isacharbar}\ {\isachardoublequoteopen}And\ p\ T\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   620
{\isacharbar}\ {\isachardoublequoteopen}And\ p\ F\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   621
{\isacharbar}\ {\isachardoublequoteopen}And\ F\ p\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   622
{\isacharbar}\ {\isachardoublequoteopen}And\ X\ X\ {\isacharequal}\ X{\isachardoublequoteclose}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   623
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   624
This definition is useful, because the equations can directly be used
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   625
  as simplifcation rules rules. But the patterns overlap: For example,
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   626
  the expression \isa{And\ T\ T} is matched by both the first and
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   627
  the second equation. By default, Isabelle makes the patterns disjoint by
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   628
  splitting them up, producing instances:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   629
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   630
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   631
\isacommand{thm}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   632
\ And{\isachardot}simps%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   633
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   634
\isa{And\ T\ {\isacharquery}p\ {\isacharequal}\ {\isacharquery}p\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   635
And\ F\ T\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   636
And\ X\ T\ {\isacharequal}\ X\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   637
And\ F\ F\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   638
And\ X\ F\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   639
And\ F\ X\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   640
And\ X\ X\ {\isacharequal}\ X}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   641
  
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   642
  \vspace*{1em}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   643
  \noindent There are several problems with this:
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   644
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   645
  \begin{enumerate}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   646
  \item If the datatype has many constructors, there can be an
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   647
  explosion of equations. For \isa{And}, we get seven instead of
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   648
  five equations, which can be tolerated, but this is just a small
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   649
  example.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   650
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   651
  \item Since splitting makes the equations \qt{less general}, they
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   652
  do not always match in rewriting. While the term \isa{And\ x\ F}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   653
  can be simplified to \isa{F} with the original equations, a
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   654
  (manual) case split on \isa{x} is now necessary.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   655
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   656
  \item The splitting also concerns the induction rule \isa{And{\isachardot}induct}. Instead of five premises it now has seven, which
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   657
  means that our induction proofs will have more cases.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   658
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   659
  \item In general, it increases clarity if we get the same definition
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   660
  back which we put in.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   661
  \end{enumerate}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   662
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   663
  If we do not want the automatic splitting, we can switch it off by
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   664
  leaving out the \cmd{sequential} option. However, we will have to
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   665
  prove that our pattern matching is consistent\footnote{This prevents
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   666
  us from defining something like \isa{f\ x\ {\isacharequal}\ True} and \isa{f\ x\ {\isacharequal}\ False} simultaneously.}:%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   667
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   668
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   669
\isacommand{function}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   670
\ And{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}P{\isadigit{3}}\ {\isasymRightarrow}\ P{\isadigit{3}}\ {\isasymRightarrow}\ P{\isadigit{3}}{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   671
\isakeyword{where}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   672
\ \ {\isachardoublequoteopen}And{\isadigit{2}}\ T\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   673
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ p\ T\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   674
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ p\ F\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   675
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ F\ p\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   676
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ X\ X\ {\isacharequal}\ X{\isachardoublequoteclose}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   677
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   678
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   679
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   680
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   681
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   682
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   683
\begin{isamarkuptxt}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   684
\noindent Now let's look at the proof obligations generated by a
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   685
  function definition. In this case, they are:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   686
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   687
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   688
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}P\ x{\isachardot}\ {\isasymlbrakk}{\isasymAnd}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\ {\isasymAnd}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\ {\isasymAnd}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}p{\isacharcomma}\ F{\isacharparenright}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   689
\isaindent{\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}P\ x{\isachardot}\ \ }{\isasymAnd}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}F{\isacharcomma}\ p{\isacharparenright}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\ x\ {\isacharequal}\ {\isacharparenleft}X{\isacharcomma}\ X{\isacharparenright}\ {\isasymLongrightarrow}\ P{\isasymrbrakk}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   690
\isaindent{\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}P\ x{\isachardot}\ }{\isasymLongrightarrow}\ P\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   691
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}p\ pa{\isachardot}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}T{\isacharcomma}\ pa{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ pa\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   692
\ {\isadigit{3}}{\isachardot}\ {\isasymAnd}p\ pa{\isachardot}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}pa{\isacharcomma}\ T{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ pa\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   693
\ {\isadigit{4}}{\isachardot}\ {\isasymAnd}p\ pa{\isachardot}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}pa{\isacharcomma}\ F{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ F\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   694
\ {\isadigit{5}}{\isachardot}\ {\isasymAnd}p\ pa{\isachardot}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}F{\isacharcomma}\ pa{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ F\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   695
\ {\isadigit{6}}{\isachardot}\ {\isasymAnd}p{\isachardot}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}X{\isacharcomma}\ X{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ X\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   696
\ {\isadigit{7}}{\isachardot}\ {\isasymAnd}p\ pa{\isachardot}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}pa{\isacharcomma}\ T{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ pa\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   697
\ {\isadigit{8}}{\isachardot}\ {\isasymAnd}p\ pa{\isachardot}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}pa{\isacharcomma}\ F{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ F\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   698
\ {\isadigit{9}}{\isachardot}\ {\isasymAnd}p\ pa{\isachardot}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}F{\isacharcomma}\ pa{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ F\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   699
\ {\isadigit{1}}{\isadigit{0}}{\isachardot}\ {\isasymAnd}p{\isachardot}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}X{\isacharcomma}\ X{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ X%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   700
\end{isabelle}\vspace{-1.2em}\hspace{3cm}\vdots\vspace{1.2em}
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   701
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   702
  The first subgoal expresses the completeness of the patterns. It has
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   703
  the form of an elimination rule and states that every \isa{x} of
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   704
  the function's input type must match at least one of the patterns\footnote{Completeness could
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   705
  be equivalently stated as a disjunction of existential statements: 
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   706
\isa{{\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}{\isacharparenright}\ {\isasymor}\ {\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}{\isacharparenright}\ {\isasymor}\ {\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}p{\isacharcomma}\ F{\isacharparenright}{\isacharparenright}\ {\isasymor}\ {\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}F{\isacharcomma}\ p{\isacharparenright}{\isacharparenright}\ {\isasymor}\ x\ {\isacharequal}\ {\isacharparenleft}X{\isacharcomma}\ X{\isacharparenright}}.}. If the patterns just involve
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   707
  datatypes, we can solve it with the \isa{pat{\isacharunderscore}completeness}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   708
  method:%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   709
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   710
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   711
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   712
\ pat{\isacharunderscore}completeness%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   713
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   714
The remaining subgoals express \emph{pattern compatibility}. We do
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   715
  allow that an input value matches multiple patterns, but in this
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   716
  case, the result (i.e.~the right hand sides of the equations) must
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   717
  also be equal. For each pair of two patterns, there is one such
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   718
  subgoal. Usually this needs injectivity of the constructors, which
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   719
  is used automatically by \isa{auto}.%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   720
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   721
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   722
\isacommand{by}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   723
\ auto%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   724
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   725
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   726
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   727
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   728
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   729
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   730
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   731
\isamarkupsubsection{Non-constructor patterns%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   732
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   733
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   734
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   735
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   736
Most of Isabelle's basic types take the form of inductive data types
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   737
  with constructors. However, this is not true for all of them. The
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   738
  integers, for instance, are defined using the usual algebraic
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   739
  quotient construction, thus they are not an \qt{official} datatype.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   740
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   741
  Of course, we might want to do pattern matching there, too. So%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   742
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   743
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   744
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   745
\ Abs\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}int\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   746
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   747
\ \ {\isachardoublequoteopen}Abs\ {\isacharparenleft}int\ n{\isacharparenright}\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   748
{\isacharbar}\ {\isachardoublequoteopen}Abs\ {\isacharparenleft}{\isacharminus}\ int\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   749
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   750
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   751
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   752
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   753
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   754
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   755
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   756
\ {\isacharparenleft}erule\ int{\isacharunderscore}cases{\isacharparenright}\ auto%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   757
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   758
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   759
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   760
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   761
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   762
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   763
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   764
\isacommand{termination}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   765
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   766
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   767
\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   768
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   769
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   770
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   771
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   772
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}{\isacharparenright}\ simp%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   773
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   774
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   775
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   776
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   777
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   778
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   779
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   780
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   781
This kind of matching is again justified by the proof of pattern
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   782
  completeness and compatibility. Here, the existing lemma \isa{int{\isacharunderscore}cases} is used:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   783
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   784
  \begin{center}\isa{{\isasymlbrakk}{\isasymAnd}n{\isachardot}\ {\isacharquery}z\ {\isacharequal}\ int\ n\ {\isasymLongrightarrow}\ {\isacharquery}P{\isacharsemicolon}\ {\isasymAnd}n{\isachardot}\ {\isacharquery}z\ {\isacharequal}\ {\isacharminus}\ int\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isasymLongrightarrow}\ {\isacharquery}P{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharquery}P}\hfill(\isa{int{\isacharunderscore}cases})\end{center}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   785
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   786
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   787
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   788
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   789
One well-known instance of non-constructor patterns are the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   790
  so-called \emph{$n+k$-patterns}, which are a little controversial in
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   791
  the functional programming world. Here is the initial fibonacci
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   792
  example with $n+k$-patterns:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   793
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   794
\isamarkuptrue%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   795
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   796
\ fib{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   797
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   798
\ \ {\isachardoublequoteopen}fib{\isadigit{2}}\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   799
{\isacharbar}\ {\isachardoublequoteopen}fib{\isadigit{2}}\ {\isadigit{1}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   800
{\isacharbar}\ {\isachardoublequoteopen}fib{\isadigit{2}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ fib{\isadigit{2}}\ n\ {\isacharplus}\ fib{\isadigit{2}}\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   801
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   802
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   803
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   804
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   805
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   806
\isatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   807
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   808
\endisatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   809
{\isafoldML}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   810
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   811
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   812
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   813
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   814
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   815
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   816
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   817
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   818
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   819
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   820
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   821
\begin{isamarkuptxt}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   822
The proof obligation for pattern completeness states that every natural number is
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   823
  either \isa{{\isadigit{0}}}, \isa{{\isadigit{1}}} or \isa{n\ {\isacharplus}\ {\isadigit{2}}}:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   824
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   825
  \begin{isabelle}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   826
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}P\ x{\isachardot}\ {\isasymlbrakk}x\ {\isacharequal}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\ x\ {\isacharequal}\ {\isadigit{1}}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\ {\isasymAnd}n{\isachardot}\ x\ {\isacharequal}\ n\ {\isacharplus}\ {\isadigit{2}}\ {\isasymLongrightarrow}\ P{\isasymrbrakk}\ {\isasymLongrightarrow}\ P%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   827
\end{isabelle}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   828
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   829
  This is an arithmetic triviality, but unfortunately the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   830
  \isa{arith} method cannot handle this specific form of an
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   831
  elimination rule. We have to do a case split on \isa{P} first,
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   832
  which can be conveniently done using the \isa{classical} rule. Pattern compatibility and termination are automatic as usual.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   833
\end{isamarkuptxt}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   834
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   835
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   836
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   837
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   838
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   839
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   840
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   841
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   842
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   843
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   844
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   845
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   846
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   847
\isatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   848
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   849
\endisatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   850
{\isafoldML}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   851
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   852
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   853
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   854
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   855
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   856
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   857
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   858
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   859
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   860
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   861
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   862
\isacommand{apply}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   863
\ {\isacharparenleft}rule\ classical{\isacharcomma}\ simp{\isacharcomma}\ arith{\isacharparenright}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   864
\isacommand{apply}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   865
\ auto\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   866
\isacommand{done}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   867
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   868
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   869
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   870
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   871
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   872
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   873
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   874
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   875
\isacommand{termination}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   876
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   877
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   878
\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   879
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   880
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   881
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   882
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   883
\ lexicographic{\isacharunderscore}order%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   884
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   885
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   886
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   887
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   888
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   889
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   890
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   891
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   892
We can stretch the notion of pattern matching even more. The
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   893
  following function is not a sensible functional program, but a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   894
  perfectly valid mathematical definition:%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   895
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   896
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   897
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   898
\ ev\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   899
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   900
\ \ {\isachardoublequoteopen}ev\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   901
{\isacharbar}\ {\isachardoublequoteopen}ev\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}\ {\isacharequal}\ False{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   902
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   903
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   904
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   905
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   906
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   907
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   908
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   909
\ {\isacharparenleft}rule\ classical{\isacharcomma}\ simp{\isacharparenright}\ arith{\isacharplus}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   910
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   911
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   912
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   913
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   914
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   915
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   916
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   917
\isacommand{termination}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   918
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   919
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   920
\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   921
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   922
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   923
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   924
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   925
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}{\isacharparenright}\ simp%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   926
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   927
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   928
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   929
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   930
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   931
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   932
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   933
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   934
This general notion of pattern matching gives you the full freedom
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   935
  of mathematical specifications. However, as always, freedom should
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   936
  be used with care:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   937
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   938
  If we leave the area of constructor
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   939
  patterns, we have effectively departed from the world of functional
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   940
  programming. This means that it is no longer possible to use the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   941
  code generator, and expect it to generate ML code for our
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   942
  definitions. Also, such a specification might not work very well together with
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   943
  simplification. Your mileage may vary.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   944
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   945
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   946
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   947
\isamarkupsubsection{Conditional equations%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   948
}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   949
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   950
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   951
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   952
The function package also supports conditional equations, which are
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   953
  similar to guards in a language like Haskell. Here is Euclid's
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   954
  algorithm written with conditional patterns\footnote{Note that the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   955
  patterns are also overlapping in the base case}:%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   956
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   957
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   958
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   959
\ gcd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   960
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   961
\ \ {\isachardoublequoteopen}gcd\ x\ {\isadigit{0}}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   962
{\isacharbar}\ {\isachardoublequoteopen}gcd\ {\isadigit{0}}\ y\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   963
{\isacharbar}\ {\isachardoublequoteopen}x\ {\isacharless}\ y\ {\isasymLongrightarrow}\ gcd\ {\isacharparenleft}Suc\ x{\isacharparenright}\ {\isacharparenleft}Suc\ y{\isacharparenright}\ {\isacharequal}\ gcd\ {\isacharparenleft}Suc\ x{\isacharparenright}\ {\isacharparenleft}y\ {\isacharminus}\ x{\isacharparenright}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   964
{\isacharbar}\ {\isachardoublequoteopen}{\isasymnot}\ x\ {\isacharless}\ y\ {\isasymLongrightarrow}\ gcd\ {\isacharparenleft}Suc\ x{\isacharparenright}\ {\isacharparenleft}Suc\ y{\isacharparenright}\ {\isacharequal}\ gcd\ {\isacharparenleft}x\ {\isacharminus}\ y{\isacharparenright}\ {\isacharparenleft}Suc\ y{\isacharparenright}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   965
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   966
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   967
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   968
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   969
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   970
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   971
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   972
\ {\isacharparenleft}rule\ classical{\isacharcomma}\ auto{\isacharcomma}\ arith{\isacharparenright}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   973
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   974
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   975
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   976
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   977
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   978
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   979
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   980
\isacommand{termination}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   981
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   982
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   983
\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   984
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   985
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   986
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   987
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   988
\ lexicographic{\isacharunderscore}order%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   989
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   990
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   991
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   992
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   993
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   994
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   995
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   996
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   997
By now, you can probably guess what the proof obligations for the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   998
  pattern completeness and compatibility look like. 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   999
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1000
  Again, functions with conditional patterns are not supported by the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1001
  code generator.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1002
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1003
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1004
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1005
\isamarkupsubsection{Pattern matching on strings%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1006
}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1007
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1008
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1009
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1010
As strings (as lists of characters) are normal data types, pattern
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1011
  matching on them is possible, but somewhat problematic. Consider the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1012
  following definition:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1013
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1014
\end{isamarkuptext}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1015
\noindent\cmd{fun} \isa{check\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}string\ {\isasymRightarrow}\ bool{\isachardoublequote}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1016
\cmd{where}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1017
\hspace*{2ex}\isa{{\isachardoublequote}check\ {\isacharparenleft}{\isacharprime}{\isacharprime}good{\isacharprime}{\isacharprime}{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequote}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1018
\isa{{\isacharbar}\ {\isachardoublequote}check\ s\ {\isacharequal}\ False{\isachardoublequote}}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1019
\begin{isamarkuptext}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1020
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1021
  An invocation of the above \cmd{fun} command does not
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1022
  terminate. What is the problem? Strings are lists of characters, and
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1023
  characters are a data type with a lot of constructors. Splitting the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1024
  catch-all pattern thus leads to an explosion of cases, which cannot
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1025
  be handled by Isabelle.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1026
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1027
  There are two things we can do here. Either we write an explicit
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1028
  \isa{if} on the right hand side, or we can use conditional patterns:%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1029
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1030
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1031
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1032
\ check\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}string\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1033
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1034
\ \ {\isachardoublequoteopen}check\ {\isacharparenleft}{\isacharprime}{\isacharprime}good{\isacharprime}{\isacharprime}{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1035
{\isacharbar}\ {\isachardoublequoteopen}s\ {\isasymnoteq}\ {\isacharprime}{\isacharprime}good{\isacharprime}{\isacharprime}\ {\isasymLongrightarrow}\ check\ s\ {\isacharequal}\ False{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1036
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1037
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1038
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1039
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1040
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1041
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1042
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1043
\ auto%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1044
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1045
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1046
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1047
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1048
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1049
\endisadelimproof
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1050
%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1051
\isamarkupsection{Partiality%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1052
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1053
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1054
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1055
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1056
In HOL, all functions are total. A function \isa{f} applied to
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1057
  \isa{x} always has the value \isa{f\ x}, and there is no notion
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1058
  of undefinedness. 
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1059
  
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1060
  This is why we have to do termination
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1061
  proofs when defining functions: The proof justifies that the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1062
  function can be defined by wellfounded recursion.
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1063
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1064
  However, the \cmd{function} package does support partiality to a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1065
  certain extent. Let's look at the following function which looks
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1066
  for a zero of a given function f.%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1067
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1068
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1069
\isacommand{function}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1070
\ findzero\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1071
\isakeyword{where}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1072
\ \ {\isachardoublequoteopen}findzero\ f\ n\ {\isacharequal}\ {\isacharparenleft}if\ f\ n\ {\isacharequal}\ {\isadigit{0}}\ then\ n\ else\ findzero\ f\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1073
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1074
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1075
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1076
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1077
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1078
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1079
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1080
\ pat{\isacharunderscore}completeness\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1081
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1082
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1083
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1084
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1085
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1086
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1087
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1088
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1089
Clearly, any attempt of a termination proof must fail. And without
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1090
  that, we do not get the usual rules \isa{findzero{\isachardot}simp} and 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1091
  \isa{findzero{\isachardot}induct}. So what was the definition good for at all?%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1092
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1093
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1094
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1095
\isamarkupsubsection{Domain predicates%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1096
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1097
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1098
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1099
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1100
The trick is that Isabelle has not only defined the function \isa{findzero}, but also
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1101
  a predicate \isa{findzero{\isacharunderscore}dom} that characterizes the values where the function
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1102
  terminates: the \emph{domain} of the function. If we treat a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1103
  partial function just as a total function with an additional domain
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1104
  predicate, we can derive simplification and
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1105
  induction rules as we do for total functions. They are guarded
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1106
  by domain conditions and are called \isa{psimps} and \isa{pinduct}:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1107
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1108
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1109
\isacommand{thm}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1110
\ findzero{\isachardot}psimps%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1111
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1112
\begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1113
findzero{\isacharunderscore}dom\ {\isacharparenleft}{\isacharquery}f{\isacharcomma}\ {\isacharquery}n{\isacharparenright}\ {\isasymLongrightarrow}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1114
findzero\ {\isacharquery}f\ {\isacharquery}n\ {\isacharequal}\ {\isacharparenleft}if\ {\isacharquery}f\ {\isacharquery}n\ {\isacharequal}\ {\isadigit{0}}\ then\ {\isacharquery}n\ else\ findzero\ {\isacharquery}f\ {\isacharparenleft}Suc\ {\isacharquery}n{\isacharparenright}{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1115
\end{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1116
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1117
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1118
\isacommand{thm}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1119
\ findzero{\isachardot}pinduct%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1120
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1121
\begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1122
{\isasymlbrakk}findzero{\isacharunderscore}dom\ {\isacharparenleft}{\isacharquery}a{\isadigit{0}}{\isachardot}{\isadigit{0}}{\isacharcomma}\ {\isacharquery}a{\isadigit{1}}{\isachardot}{\isadigit{0}}{\isacharparenright}{\isacharsemicolon}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1123
\isaindent{\ }{\isasymAnd}f\ n{\isachardot}\ {\isasymlbrakk}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isacharsemicolon}\ f\ n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ {\isacharquery}P\ f\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharquery}P\ f\ n{\isasymrbrakk}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1124
{\isasymLongrightarrow}\ {\isacharquery}P\ {\isacharquery}a{\isadigit{0}}{\isachardot}{\isadigit{0}}\ {\isacharquery}a{\isadigit{1}}{\isachardot}{\isadigit{0}}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1125
\end{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1126
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1127
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1128
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1129
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1130
Remember that all we
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1131
  are doing here is use some tricks to make a total function appear
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1132
  as if it was partial. We can still write the term \isa{findzero\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ {\isadigit{1}}{\isacharparenright}\ {\isadigit{0}}} and like any other term of type \isa{nat} it is equal
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1133
  to some natural number, although we might not be able to find out
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1134
  which one. The function is \emph{underdefined}.
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1135
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1136
  But it is enough defined to prove something interesting about it. We
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1137
  can prove that if \isa{findzero\ f\ n}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1138
  it terminates, it indeed returns a zero of \isa{f}:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1139
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1140
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1141
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1142
\ findzero{\isacharunderscore}zero{\isacharcolon}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}\ {\isasymLongrightarrow}\ f\ {\isacharparenleft}findzero\ f\ n{\isacharparenright}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1143
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1144
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1145
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1146
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1147
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1148
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1149
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1150
We apply induction as usual, but using the partial induction
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1151
  rule:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1152
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1153
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1154
\isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1155
\ {\isacharparenleft}induct\ f\ n\ rule{\isacharcolon}\ findzero{\isachardot}pinduct{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1156
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1157
This gives the following subgoals:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1158
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1159
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1160
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}f\ n{\isachardot}\ {\isasymlbrakk}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isacharsemicolon}\ f\ n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ f\ {\isacharparenleft}findzero\ f\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isadigit{0}}{\isasymrbrakk}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1161
\isaindent{\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}f\ n{\isachardot}\ }{\isasymLongrightarrow}\ f\ {\isacharparenleft}findzero\ f\ n{\isacharparenright}\ {\isacharequal}\ {\isadigit{0}}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1162
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1163
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1164
  The hypothesis in our lemma was used to satisfy the first premise in
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1165
  the induction rule. However, we also get \isa{findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}} as a local assumption in the induction step. This
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1166
  allows to unfold \isa{findzero\ f\ n} using the \isa{psimps}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1167
  rule, and the rest is trivial. Since the \isa{psimps} rules carry the
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1168
  \isa{{\isacharbrackleft}simp{\isacharbrackright}} attribute by default, we just need a single step:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1169
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1170
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1171
\isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1172
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1173
\isacommand{done}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1174
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1175
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1176
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1177
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1178
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1179
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1180
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1181
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1182
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1183
Proofs about partial functions are often not harder than for total
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1184
  functions. Fig.~\ref{findzero_isar} shows a slightly more
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1185
  complicated proof written in Isar. It is verbose enough to show how
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1186
  partiality comes into play: From the partial induction, we get an
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1187
  additional domain condition hypothesis. Observe how this condition
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1188
  is applied when calls to \isa{findzero} are unfolded.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1189
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1190
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1191
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1192
\begin{figure}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1193
\hrule\vspace{6pt}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1194
\begin{minipage}{0.8\textwidth}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1195
\isabellestyle{it}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1196
\isastyle\isamarkuptrue
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1197
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1198
\ {\isachardoublequoteopen}{\isasymlbrakk}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isacharsemicolon}\ x\ {\isasymin}\ {\isacharbraceleft}n\ {\isachardot}{\isachardot}{\isacharless}\ findzero\ f\ n{\isacharbraceright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ f\ x\ {\isasymnoteq}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1199
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1200
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1201
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1202
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1203
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1204
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1205
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1206
\ {\isacharparenleft}induct\ rule{\isacharcolon}\ findzero{\isachardot}pinduct{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1207
\ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1208
\ f\ n\ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1209
\ dom{\isacharcolon}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1210
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isakeyword{and}\ IH{\isacharcolon}\ {\isachardoublequoteopen}{\isasymlbrakk}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isacharsemicolon}\ x\ {\isasymin}\ {\isacharbraceleft}Suc\ n\ {\isachardot}{\isachardot}{\isacharless}\ findzero\ f\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharbraceright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ f\ x\ {\isasymnoteq}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1211
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isakeyword{and}\ x{\isacharunderscore}range{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymin}\ {\isacharbraceleft}n\ {\isachardot}{\isachardot}{\isacharless}\ findzero\ f\ n{\isacharbraceright}{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1212
\ \ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1213
\ {\isachardoublequoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1214
\ \ \isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1215
\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1216
\ \ \ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1217
\ {\isachardoublequoteopen}f\ n\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1218
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1219
\ dom\ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1220
\ {\isachardoublequoteopen}findzero\ f\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1221
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1222
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1223
\ x{\isacharunderscore}range\ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1224
\ False\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1225
\ auto\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1226
\ \ \isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1227
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1228
\ \ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1229
\ \ \isacommand{from}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1230
\ x{\isacharunderscore}range\ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1231
\ {\isachardoublequoteopen}x\ {\isacharequal}\ n\ {\isasymor}\ x\ {\isasymin}\ {\isacharbraceleft}Suc\ n\ {\isachardot}{\isachardot}{\isacharless}\ findzero\ f\ n{\isacharbraceright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1232
\ auto\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1233
\ \ \isacommand{thus}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1234
\ {\isachardoublequoteopen}f\ x\ {\isasymnoteq}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1235
\ \ \isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1236
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1237
\ \ \ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1238
\ {\isachardoublequoteopen}x\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1239
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1240
\ {\isacharbackquoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isacharbackquoteclose}\ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1241
\ {\isacharquery}thesis\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1242
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1243
\ \ \isacommand{next}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1244
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1245
\ \ \ \ \isacommand{assume}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1246
\ {\isachardoublequoteopen}x\ {\isasymin}\ {\isacharbraceleft}Suc\ n\ {\isachardot}{\isachardot}{\isacharless}\ findzero\ f\ n{\isacharbraceright}{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1247
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1248
\ dom\ \isakeyword{and}\ {\isacharbackquoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isacharbackquoteclose}\ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1249
\ {\isachardoublequoteopen}x\ {\isasymin}\ {\isacharbraceleft}Suc\ n\ {\isachardot}{\isachardot}{\isacharless}\ findzero\ f\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharbraceright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1250
\ \ \ \ \ \ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1251
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1252
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1253
\ IH\ \isakeyword{and}\ {\isacharbackquoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isacharbackquoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1254
\ \ \ \ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1255
\ {\isacharquery}thesis\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1256
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1257
\ \ \isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1258
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1259
\isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1260
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1261
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1262
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1263
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1264
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1265
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1266
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1267
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1268
\isamarkupfalse\isabellestyle{tt}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1269
\end{minipage}\vspace{6pt}\hrule
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1270
\caption{A proof about a partial function}\label{findzero_isar}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1271
\end{figure}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1272
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1273
\isamarkupsubsection{Partial termination proofs%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1274
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1275
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1276
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1277
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1278
Now that we have proved some interesting properties about our
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1279
  function, we should turn to the domain predicate and see if it is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1280
  actually true for some values. Otherwise we would have just proved
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1281
  lemmas with \isa{False} as a premise.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1282
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1283
  Essentially, we need some introduction rules for \isa{findzero{\isacharunderscore}dom}. The function package can prove such domain
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1284
  introduction rules automatically. But since they are not used very
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1285
  often (they are almost never needed if the function is total), this
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1286
  functionality is disabled by default for efficiency reasons. So we have to go
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1287
  back and ask for them explicitly by passing the \isa{{\isacharparenleft}domintros{\isacharparenright}} option to the function package:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1288
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1289
\vspace{1ex}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1290
\noindent\cmd{function} \isa{{\isacharparenleft}domintros{\isacharparenright}\ findzero\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}nat\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequote}}\\%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1291
\cmd{where}\isanewline%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1292
\ \ \ldots\\
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1293
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1294
  \noindent Now the package has proved an introduction rule for \isa{findzero{\isacharunderscore}dom}:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1295
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1296
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1297
\isacommand{thm}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1298
\ findzero{\isachardot}domintros%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1299
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1300
\begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1301
{\isacharparenleft}{\isadigit{0}}\ {\isacharless}\ {\isacharquery}f\ {\isacharquery}n\ {\isasymLongrightarrow}\ findzero{\isacharunderscore}dom\ {\isacharparenleft}{\isacharquery}f{\isacharcomma}\ Suc\ {\isacharquery}n{\isacharparenright}{\isacharparenright}\ {\isasymLongrightarrow}\ findzero{\isacharunderscore}dom\ {\isacharparenleft}{\isacharquery}f{\isacharcomma}\ {\isacharquery}n{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1302
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1303
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1304
  Domain introduction rules allow to show that a given value lies in the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1305
  domain of a function, if the arguments of all recursive calls
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1306
  are in the domain as well. They allow to do a \qt{single step} in a
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1307
  termination proof. Usually, you want to combine them with a suitable
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1308
  induction principle.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1309
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1310
  Since our function increases its argument at recursive calls, we
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1311
  need an induction principle which works \qt{backwards}. We will use
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1312
  \isa{inc{\isacharunderscore}induct}, which allows to do induction from a fixed number
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1313
  \qt{downwards}:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1314
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1315
  \begin{center}\isa{{\isasymlbrakk}{\isacharquery}i\ {\isasymle}\ {\isacharquery}j{\isacharsemicolon}\ {\isacharquery}P\ {\isacharquery}j{\isacharsemicolon}\ {\isasymAnd}i{\isachardot}\ {\isasymlbrakk}i\ {\isacharless}\ {\isacharquery}j{\isacharsemicolon}\ {\isacharquery}P\ {\isacharparenleft}Suc\ i{\isacharparenright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharquery}P\ i{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharquery}P\ {\isacharquery}i}\hfill(\isa{inc{\isacharunderscore}induct})\end{center}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1316
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1317
  Figure \ref{findzero_term} gives a detailed Isar proof of the fact
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1318
  that \isa{findzero} terminates if there is a zero which is greater
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1319
  or equal to \isa{n}. First we derive two useful rules which will
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1320
  solve the base case and the step case of the induction. The
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1321
  induction is then straightforward, except for the unusal induction
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1322
  principle.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1323
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1324
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1325
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1326
\begin{figure}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1327
\hrule\vspace{6pt}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1328
\begin{minipage}{0.8\textwidth}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1329
\isabellestyle{it}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1330
\isastyle\isamarkuptrue
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1331
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1332
\ findzero{\isacharunderscore}termination{\isacharcolon}\isanewline
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1333
\ \ \isakeyword{assumes}\ {\isachardoublequoteopen}x\ {\isasymge}\ n{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}f\ x\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1334
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1335
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1336
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1337
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1338
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1339
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1340
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1341
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1342
\ {\isacharminus}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1343
\ \ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1344
\ base{\isacharcolon}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ x{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1345
\ \ \ \ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1346
\ {\isacharparenleft}rule\ findzero{\isachardot}domintros{\isacharparenright}\ {\isacharparenleft}simp\ add{\isacharcolon}{\isacharbackquoteopen}f\ x\ {\isacharequal}\ {\isadigit{0}}{\isacharbackquoteclose}{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1347
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1348
\ \ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1349
\ step{\isacharcolon}\ {\isachardoublequoteopen}{\isasymAnd}i{\isachardot}\ findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ Suc\ i{\isacharparenright}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1350
\ \ \ \ {\isasymLongrightarrow}\ findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ i{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1351
\ \ \ \ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1352
\ {\isacharparenleft}rule\ findzero{\isachardot}domintros{\isacharparenright}\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1353
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1354
\ \ \isacommand{from}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1355
\ {\isacharbackquoteopen}x\ {\isasymge}\ n{\isacharbackquoteclose}\ \isacommand{show}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1356
\ {\isacharquery}thesis\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1357
\ \ \isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1358
\ {\isacharparenleft}induct\ rule{\isacharcolon}inc{\isacharunderscore}induct{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1359
\ \ \ \ \isacommand{show}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1360
\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ x{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1361
\ {\isacharparenleft}rule\ base{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1362
\ \ \isacommand{next}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1363
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1364
\ \ \ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1365
\ i\ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1366
\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ Suc\ i{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1367
\ \ \ \ \isacommand{thus}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1368
\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ i{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1369
\ {\isacharparenleft}rule\ step{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1370
\ \ \isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1371
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1372
\isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1373
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1374
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1375
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1376
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1377
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1378
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1379
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1380
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1381
\isamarkupfalse\isabellestyle{tt}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1382
\end{minipage}\vspace{6pt}\hrule
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1383
\caption{Termination proof for \isa{findzero}}\label{findzero_term}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1384
\end{figure}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1385
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1386
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1387
Again, the proof given in Fig.~\ref{findzero_term} has a lot of
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1388
  detail in order to explain the principles. Using more automation, we
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1389
  can also have a short proof:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1390
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1391
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1392
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1393
\ findzero{\isacharunderscore}termination{\isacharunderscore}short{\isacharcolon}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1394
\ \ \isakeyword{assumes}\ zero{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isachargreater}{\isacharequal}\ n{\isachardoublequoteclose}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1395
\ \ \isakeyword{assumes}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}f\ x\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1396
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1397
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1398
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1399
\ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1400
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1401
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1402
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1403
\isacommand{using}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1404
\ zero\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1405
\ \ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1406
\ {\isacharparenleft}induct\ rule{\isacharcolon}inc{\isacharunderscore}induct{\isacharparenright}\ {\isacharparenleft}auto\ intro{\isacharcolon}\ findzero{\isachardot}domintros{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1407
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1408
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1409
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1410
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1411
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1412
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1413
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1414
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1415
\noindent It is simple to combine the partial correctness result with the
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1416
  termination lemma:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1417
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1418
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1419
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1420
\ findzero{\isacharunderscore}total{\isacharunderscore}correctness{\isacharcolon}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1421
\ \ {\isachardoublequoteopen}f\ x\ {\isacharequal}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ f\ {\isacharparenleft}findzero\ f\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1422
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1423
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1424
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1425
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1426
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1427
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1428
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1429
\ {\isacharparenleft}blast\ intro{\isacharcolon}\ findzero{\isacharunderscore}zero\ findzero{\isacharunderscore}termination{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1430
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1431
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1432
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1433
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1434
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1435
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1436
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1437
\isamarkupsubsection{Definition of the domain predicate%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1438
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1439
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1440
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1441
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1442
Sometimes it is useful to know what the definition of the domain
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1443
  predicate actually is. Actually, \isa{findzero{\isacharunderscore}dom} is just an
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1444
  abbreviation:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1445
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1446
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1447
findzero{\isacharunderscore}dom\ {\isasymequiv}\ acc\ findzero{\isacharunderscore}rel%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1448
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1449
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1450
  The domain predicate is the \emph{accessible part} of a relation \isa{findzero{\isacharunderscore}rel}, which was also created internally by the function
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1451
  package. \isa{findzero{\isacharunderscore}rel} is just a normal
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1452
  inductive predicate, so we can inspect its definition by
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1453
  looking at the introduction rules \isa{findzero{\isacharunderscore}rel{\isachardot}intros}.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1454
  In our case there is just a single rule:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1455
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1456
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1457
{\isacharquery}f\ {\isacharquery}n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ findzero{\isacharunderscore}rel\ {\isacharparenleft}{\isacharquery}f{\isacharcomma}\ Suc\ {\isacharquery}n{\isacharparenright}\ {\isacharparenleft}{\isacharquery}f{\isacharcomma}\ {\isacharquery}n{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1458
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1459
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1460
  The predicate \isa{findzero{\isacharunderscore}rel}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1461
  describes the \emph{recursion relation} of the function
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1462
  definition. The recursion relation is a binary relation on
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1463
  the arguments of the function that relates each argument to its
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1464
  recursive calls. In general, there is one introduction rule for each
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1465
  recursive call.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1466
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1467
  The predicate \isa{findzero{\isacharunderscore}dom} is the accessible part of
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1468
  that relation. An argument belongs to the accessible part, if it can
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1469
  be reached in a finite number of steps (cf.~its definition in \isa{Accessible{\isacharunderscore}Part{\isachardot}thy}).
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1470
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1471
  Since the domain predicate is just an abbreviation, you can use
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1472
  lemmas for \isa{acc} and \isa{findzero{\isacharunderscore}rel} directly. Some
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1473
  lemmas which are occasionally useful are \isa{accI}, \isa{acc{\isacharunderscore}downward}, and of course the introduction and elimination rules
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1474
  for the recursion relation \isa{findzero{\isachardot}intros} and \isa{findzero{\isachardot}cases}.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1475
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1476
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1477
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1478
\isamarkupsubsection{A Useful Special Case: Tail recursion%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1479
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1480
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1481
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1482
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1483
The domain predicate is our trick that allows us to model partiality
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1484
  in a world of total functions. The downside of this is that we have
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1485
  to carry it around all the time. The termination proof above allowed
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1486
  us to replace the abstract \isa{findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}} by the more
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1487
  concrete \isa{n\ {\isasymle}\ x\ {\isasymand}\ f\ x\ {\isacharequal}\ {\isadigit{0}}}, but the condition is still
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1488
  there and can only be discharged for special cases.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1489
  In particular, the domain predicate guards the unfolding of our
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1490
  function, since it is there as a condition in the \isa{psimp}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1491
  rules. 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1492
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1493
  Now there is an important special case: We can actually get rid
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1494
  of the condition in the simplification rules, \emph{if the function
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1495
  is tail-recursive}. The reason is that for all tail-recursive
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1496
  equations there is a total function satisfying them, even if they
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1497
  are non-terminating. 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1498
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1499
%  A function is tail recursive, if each call to the function is either
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1500
%  equal
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1501
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1502
%  So the outer form of the 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1503
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1504
%if it can be written in the following
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1505
%  form:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1506
%  {term[display] "f x = (if COND x then BASE x else f (LOOP x))"}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1507
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1508
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1509
  The function package internally does the right construction and can
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1510
  derive the unconditional simp rules, if we ask it to do so. Luckily,
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1511
  our \isa{findzero} function is tail-recursive, so we can just go
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1512
  back and add another option to the \cmd{function} command:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1513
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1514
\vspace{1ex}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1515
\noindent\cmd{function} \isa{{\isacharparenleft}domintros{\isacharcomma}\ tailrec{\isacharparenright}\ findzero\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}nat\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequote}}\\%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1516
\cmd{where}\isanewline%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1517
\ \ \ldots\\%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1518
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1519
  
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1520
  \noindent Now, we actually get unconditional simplification rules, even
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1521
  though the function is partial:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1522
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1523
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1524
\isacommand{thm}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1525
\ findzero{\isachardot}simps%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1526
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1527
\begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1528
findzero\ {\isacharquery}f\ {\isacharquery}n\ {\isacharequal}\ {\isacharparenleft}if\ {\isacharquery}f\ {\isacharquery}n\ {\isacharequal}\ {\isadigit{0}}\ then\ {\isacharquery}n\ else\ findzero\ {\isacharquery}f\ {\isacharparenleft}Suc\ {\isacharquery}n{\isacharparenright}{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1529
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1530
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1531
  \noindent Of course these would make the simplifier loop, so we better remove
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1532
  them from the simpset:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1533
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1534
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1535
\isacommand{declare}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1536
\ findzero{\isachardot}simps{\isacharbrackleft}simp\ del{\isacharbrackright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1537
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1538
Getting rid of the domain conditions in the simplification rules is
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1539
  not only useful because it simplifies proofs. It is also required in
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1540
  order to use Isabelle's code generator to generate ML code
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1541
  from a function definition.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1542
  Since the code generator only works with equations, it cannot be
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1543
  used with \isa{psimp} rules. Thus, in order to generate code for
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1544
  partial functions, they must be defined as a tail recursion.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1545
  Luckily, many functions have a relatively natural tail recursive
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1546
  definition.%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1547
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1548
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1549
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1550
\isamarkupsection{Nested recursion%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1551
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1552
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1553
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1554
\begin{isamarkuptext}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1555
Recursive calls which are nested in one another frequently cause
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1556
  complications, since their termination proof can depend on a partial
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1557
  correctness property of the function itself. 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1558
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1559
  As a small example, we define the \qt{nested zero} function:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1560
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1561
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1562
\isacommand{function}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1563
\ nz\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1564
\isakeyword{where}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1565
\ \ {\isachardoublequoteopen}nz\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1566
{\isacharbar}\ {\isachardoublequoteopen}nz\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ nz\ {\isacharparenleft}nz\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1567
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1568
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1569
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1570
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1571
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1572
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1573
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1574
\ pat{\isacharunderscore}completeness\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1575
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1576
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1577
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1578
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1579
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1580
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1581
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1582
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1583
If we attempt to prove termination using the identity measure on
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1584
  naturals, this fails:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1585
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1586
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1587
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1588
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1589
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1590
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1591
\ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1592
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1593
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1594
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1595
\isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1596
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1597
\ \ \isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1598
\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1599
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1600
We get stuck with the subgoal
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1601
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1602
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1603
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ nz{\isacharunderscore}dom\ n\ {\isasymLongrightarrow}\ nz\ n\ {\isacharless}\ Suc\ n%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1604
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1605
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1606
  Of course this statement is true, since we know that \isa{nz} is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1607
  the zero function. And in fact we have no problem proving this
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1608
  property by induction.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1609
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1610
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1611
\isacommand{oops}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1612
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1613
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1614
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1615
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1616
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1617
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1618
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1619
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1620
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1621
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1622
\ nz{\isacharunderscore}is{\isacharunderscore}zero{\isacharcolon}\ {\isachardoublequoteopen}nz{\isacharunderscore}dom\ n\ {\isasymLongrightarrow}\ nz\ n\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1623
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1624
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1625
\ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1626
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1627
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1628
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1629
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1630
\ {\isacharparenleft}induct\ rule{\isacharcolon}nz{\isachardot}pinduct{\isacharparenright}\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1631
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1632
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1633
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1634
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1635
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1636
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1637
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1638
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1639
We formulate this as a partial correctness lemma with the condition
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1640
  \isa{nz{\isacharunderscore}dom\ n}. This allows us to prove it with the \isa{pinduct} rule before we have proved termination. With this lemma,
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1641
  the termination proof works as expected:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1642
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1643
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1644
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1645
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1646
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1647
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1648
\ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1649
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1650
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1651
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1652
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1653
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\ {\isacharparenleft}auto\ simp{\isacharcolon}\ nz{\isacharunderscore}is{\isacharunderscore}zero{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1654
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1655
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1656
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1657
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1658
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1659
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1660
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1661
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1662
As a general strategy, one should prove the statements needed for
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1663
  termination as a partial property first. Then they can be used to do
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1664
  the termination proof. This also works for less trivial
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1665
  examples. Figure \ref{f91} defines the 91-function, a well-known
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1666
  challenge problem due to John McCarthy, and proves its termination.%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1667
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1668
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1669
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1670
\begin{figure}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1671
\hrule\vspace{6pt}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1672
\begin{minipage}{0.8\textwidth}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1673
\isabellestyle{it}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1674
\isastyle\isamarkuptrue
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1675
\isacommand{function}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1676
\ f{\isadigit{9}}{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1677
\isakeyword{where}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1678
\ \ {\isachardoublequoteopen}f{\isadigit{9}}{\isadigit{1}}\ n\ {\isacharequal}\ {\isacharparenleft}if\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isacharless}\ n\ then\ n\ {\isacharminus}\ {\isadigit{1}}{\isadigit{0}}\ else\ f{\isadigit{9}}{\isadigit{1}}\ {\isacharparenleft}f{\isadigit{9}}{\isadigit{1}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1679
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1680
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1681
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1682
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1683
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1684
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1685
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1686
\ pat{\isacharunderscore}completeness\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1687
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1688
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1689
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1690
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1691
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1692
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1693
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1694
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1695
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1696
\ f{\isadigit{9}}{\isadigit{1}}{\isacharunderscore}estimate{\isacharcolon}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1697
\ \ \isakeyword{assumes}\ trm{\isacharcolon}\ {\isachardoublequoteopen}f{\isadigit{9}}{\isadigit{1}}{\isacharunderscore}dom\ n{\isachardoublequoteclose}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1698
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}n\ {\isacharless}\ f{\isadigit{9}}{\isadigit{1}}\ n\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1699
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1700
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1701
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1702
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1703
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1704
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1705
\isacommand{using}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1706
\ trm\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1707
\ induct\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1708
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1709
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1710
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1711
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1712
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1713
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1714
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1715
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1716
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1717
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1718
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1719
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1720
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1721
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1722
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1723
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1724
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1725
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1726
\ \ \isacommand{let}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1727
\ {\isacharquery}R\ {\isacharequal}\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{1}}\ {\isacharminus}\ x{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1728
\ \ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1729
\ {\isachardoublequoteopen}wf\ {\isacharquery}R{\isachardoublequoteclose}\ \isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1730
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1731
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1732
\ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1733
\ n\ {\isacharcolon}{\isacharcolon}\ nat\ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1734
\ {\isachardoublequoteopen}{\isasymnot}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isacharless}\ n{\isachardoublequoteclose}\ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1735
\isamarkupcmt{Assumptions for both calls%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1736
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1737
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1738
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1739
\ \ \isacommand{thus}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1740
\ {\isachardoublequoteopen}{\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}{\isacharcomma}\ n{\isacharparenright}\ {\isasymin}\ {\isacharquery}R{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1741
\ simp\ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1742
\isamarkupcmt{Inner call%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1743
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1744
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1745
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1746
\ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1747
\ inner{\isacharunderscore}trm{\isacharcolon}\ {\isachardoublequoteopen}f{\isadigit{9}}{\isadigit{1}}{\isacharunderscore}dom\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1748
\isamarkupcmt{Outer call%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1749
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1750
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1751
\ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1752
\ f{\isadigit{9}}{\isadigit{1}}{\isacharunderscore}estimate\ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1753
\ {\isachardoublequoteopen}n\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}\ {\isacharless}\ f{\isadigit{9}}{\isadigit{1}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}{\isacharparenright}\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}{\isachardoublequoteclose}\ \isacommand{{\isachardot}}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1754
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1755
\ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1756
\ {\isacharbackquoteopen}{\isasymnot}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isacharless}\ n{\isacharbackquoteclose}\ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1757
\ {\isachardoublequoteopen}{\isacharparenleft}f{\isadigit{9}}{\isadigit{1}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isadigit{1}}{\isacharparenright}{\isacharcomma}\ n{\isacharparenright}\ {\isasymin}\ {\isacharquery}R{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1758
\ simp\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1759
\isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1760
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1761
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1762
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1763
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1764
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1765
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1766
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1767
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1768
\isamarkupfalse\isabellestyle{tt}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1769
\end{minipage}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1770
\vspace{6pt}\hrule
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1771
\caption{McCarthy's 91-function}\label{f91}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1772
\end{figure}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1773
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1774
\isamarkupsection{Higher-Order Recursion%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1775
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1776
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1777
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1778
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1779
Higher-order recursion occurs when recursive calls
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1780
  are passed as arguments to higher-order combinators such as \isa{map}, \isa{filter} etc.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1781
  As an example, imagine a data type of n-ary trees:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1782
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1783
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1784
\isacommand{datatype}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1785
\ {\isacharprime}a\ tree\ {\isacharequal}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1786
\ \ Leaf\ {\isacharprime}a\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1787
{\isacharbar}\ Branch\ {\isachardoublequoteopen}{\isacharprime}a\ tree\ list{\isachardoublequoteclose}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1788
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1789
\noindent We can define a map function for trees, using the predefined
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1790
  map function for lists.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1791
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1792
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1793
\isacommand{function}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1794
\ treemap\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ tree\ {\isasymRightarrow}\ {\isacharprime}a\ tree{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1795
\isakeyword{where}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1796
\ \ {\isachardoublequoteopen}treemap\ f\ {\isacharparenleft}Leaf\ n{\isacharparenright}\ {\isacharequal}\ Leaf\ {\isacharparenleft}f\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1797
{\isacharbar}\ {\isachardoublequoteopen}treemap\ f\ {\isacharparenleft}Branch\ l{\isacharparenright}\ {\isacharequal}\ Branch\ {\isacharparenleft}map\ {\isacharparenleft}treemap\ f{\isacharparenright}\ l{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1798
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1799
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1800
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1801
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1802
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1803
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1804
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1805
\ pat{\isacharunderscore}completeness\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1806
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1807
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1808
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1809
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1810
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1811
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1812
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1813
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1814
We do the termination proof manually, to point out what happens
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1815
  here:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1816
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1817
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1818
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1819
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1820
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1821
\ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1822
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1823
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1824
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1825
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1826
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1827
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1828
As usual, we have to give a wellfounded relation, such that the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1829
  arguments of the recursive calls get smaller. But what exactly are
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1830
  the arguments of the recursive calls? Isabelle gives us the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1831
  subgoals
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1832
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1833
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1834
\ {\isadigit{1}}{\isachardot}\ wf\ {\isacharquery}R\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1835
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}f\ l\ x{\isachardot}\ x\ {\isasymin}\ set\ l\ {\isasymLongrightarrow}\ {\isacharparenleft}{\isacharparenleft}f{\isacharcomma}\ x{\isacharparenright}{\isacharcomma}\ f{\isacharcomma}\ Branch\ l{\isacharparenright}\ {\isasymin}\ {\isacharquery}R%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1836
\end{isabelle} 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1837
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1838
  So Isabelle seems to know that \isa{map} behaves nicely and only
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1839
  applies the recursive call \isa{treemap\ f} to elements
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1840
  of \isa{l}. Before we discuss where this knowledge comes from,
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1841
  let us finish the termination proof:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1842
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1843
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1844
\ \ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1845
\ {\isachardoublequoteopen}wf\ {\isacharparenleft}measure\ {\isacharparenleft}size\ o\ snd{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1846
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1847
\isacommand{next}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1848
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1849
\ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1850
\ f\ l\ \isakeyword{and}\ x\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ tree{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1851
\ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1852
\ {\isachardoublequoteopen}x\ {\isasymin}\ set\ l{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1853
\ \ \isacommand{thus}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1854
\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharparenleft}f{\isacharcomma}\ x{\isacharparenright}{\isacharcomma}\ {\isacharparenleft}f{\isacharcomma}\ Branch\ l{\isacharparenright}{\isacharparenright}\ {\isasymin}\ measure\ {\isacharparenleft}size\ o\ snd{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1855
\ \ \ \ \isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1856
\ simp%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1857
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1858
Simplification returns the following subgoal: 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1859
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1860
      \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1861
\ {\isadigit{1}}{\isachardot}\ x{\isacharunderscore}{\isacharunderscore}\ {\isasymin}\ set\ l{\isacharunderscore}{\isacharunderscore}\ {\isasymLongrightarrow}\ size\ x{\isacharunderscore}{\isacharunderscore}\ {\isacharless}\ Suc\ {\isacharparenleft}tree{\isacharunderscore}list{\isacharunderscore}size\ l{\isacharunderscore}{\isacharunderscore}{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1862
\end{isabelle} 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1863
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1864
      We are lacking a property about the function \isa{tree{\isacharunderscore}list{\isacharunderscore}size}, which was generated automatically at the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1865
      definition of the \isa{tree} type. We should go back and prove
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1866
      it, by induction.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1867
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1868
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1869
\ \ \ \ \isacommand{oops}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1870
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1871
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1872
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1873
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1874
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1875
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1876
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1877
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1878
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1879
\ \ \isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1880
\ tree{\isacharunderscore}list{\isacharunderscore}size{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymin}\ set\ l\ {\isasymLongrightarrow}\ size\ x\ {\isacharless}\ Suc\ {\isacharparenleft}tree{\isacharunderscore}list{\isacharunderscore}size\ l{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1881
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1882
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1883
\ \ \ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1884
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1885
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1886
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1887
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1888
\ {\isacharparenleft}induct\ l{\isacharparenright}\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1889
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1890
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1891
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1892
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1893
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1894
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1895
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1896
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1897
Now the whole termination proof is automatic:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1898
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1899
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1900
\ \ \isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1901
\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1902
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1903
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1904
\ \ \ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1905
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1906
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1907
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1908
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1909
\ lexicographic{\isacharunderscore}order%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1910
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1911
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1912
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1913
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1914
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1915
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1916
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1917
\isamarkupsubsection{Congruence Rules%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1918
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1919
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1920
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1921
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1922
Let's come back to the question how Isabelle knows about \isa{map}.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1923
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1924
  The knowledge about map is encoded in so-called congruence rules,
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1925
  which are special theorems known to the \cmd{function} command. The
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1926
  rule for map is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1927
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1928
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1929
{\isasymlbrakk}{\isacharquery}xs\ {\isacharequal}\ {\isacharquery}ys{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ x\ {\isasymin}\ set\ {\isacharquery}ys\ {\isasymLongrightarrow}\ {\isacharquery}f\ x\ {\isacharequal}\ {\isacharquery}g\ x{\isasymrbrakk}\ {\isasymLongrightarrow}\ map\ {\isacharquery}f\ {\isacharquery}xs\ {\isacharequal}\ map\ {\isacharquery}g\ {\isacharquery}ys%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1930
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1931
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1932
  You can read this in the following way: Two applications of \isa{map} are equal, if the list arguments are equal and the functions
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1933
  coincide on the elements of the list. This means that for the value 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1934
  \isa{map\ f\ l} we only have to know how \isa{f} behaves on
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1935
  \isa{l}.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1936
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1937
  Usually, one such congruence rule is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1938
  needed for each higher-order construct that is used when defining
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1939
  new functions. In fact, even basic functions like \isa{If} and \isa{Let} are handeled by this mechanism. The congruence
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1940
  rule for \isa{If} states that the \isa{then} branch is only
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1941
  relevant if the condition is true, and the \isa{else} branch only if it
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1942
  is false:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1943
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1944
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1945
{\isasymlbrakk}{\isacharquery}b\ {\isacharequal}\ {\isacharquery}c{\isacharsemicolon}\ {\isacharquery}c\ {\isasymLongrightarrow}\ {\isacharquery}x\ {\isacharequal}\ {\isacharquery}u{\isacharsemicolon}\ {\isasymnot}\ {\isacharquery}c\ {\isasymLongrightarrow}\ {\isacharquery}y\ {\isacharequal}\ {\isacharquery}v{\isasymrbrakk}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1946
{\isasymLongrightarrow}\ {\isacharparenleft}if\ {\isacharquery}b\ then\ {\isacharquery}x\ else\ {\isacharquery}y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ {\isacharquery}c\ then\ {\isacharquery}u\ else\ {\isacharquery}v{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1947
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1948
  
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1949
  Congruence rules can be added to the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1950
  function package by giving them the \isa{fundef{\isacharunderscore}cong} attribute.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1951
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1952
  Isabelle comes with predefined congruence rules for most of the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1953
  definitions.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1954
  But if you define your own higher-order constructs, you will have to
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1955
  come up with the congruence rules yourself, if you want to use your
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1956
  functions in recursive definitions. Since the structure of
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1957
  congruence rules is a little unintuitive, here are some exercises:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1958
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1959
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1960
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1961
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1962
\begin{exercise}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1963
    Find a suitable congruence rule for the following function which
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1964
  maps only over the even numbers in a list:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1965
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1966
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1967
mapeven\ {\isacharquery}f\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\isasep\isanewline%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1968
mapeven\ {\isacharquery}f\ {\isacharparenleft}{\isacharquery}x\ {\isacharhash}\ {\isacharquery}xs{\isacharparenright}\ {\isacharequal}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1969
{\isacharparenleft}if\ {\isacharquery}x\ mod\ {\isadigit{2}}\ {\isacharequal}\ {\isadigit{0}}\ then\ {\isacharquery}f\ {\isacharquery}x\ {\isacharhash}\ mapeven\ {\isacharquery}f\ {\isacharquery}xs\ else\ {\isacharquery}x\ {\isacharhash}\ mapeven\ {\isacharquery}f\ {\isacharquery}xs{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1970
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1971
  \end{exercise}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1972
  
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1973
  \begin{exercise}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1974
  Try what happens if the congruence rule for \isa{If} is
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1975
  disabled by declaring \isa{if{\isacharunderscore}cong{\isacharbrackleft}fundef{\isacharunderscore}cong\ del{\isacharbrackright}}?
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1976
  \end{exercise}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1977
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1978
  Note that in some cases there is no \qt{best} congruence rule.
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1979
  \fixme{}%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1980
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1981
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1982
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1983
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1984
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1985
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1986
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1987
\isatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1988
\isacommand{end}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1989
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1990
\endisatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1991
{\isafoldtheory}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1992
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1993
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1994
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1995
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1996
\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1997
\end{isabellebody}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1998
%%% Local Variables:
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1999
%%% mode: latex
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2000
%%% TeX-master: "root"
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2001
%%% End: