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