doc-src/IsarAdvanced/Functions/Thy/document/Functions.tex
author wenzelm
Mon, 12 May 2008 23:01:13 +0200
changeset 26876 d50ef6b952ba
parent 25731 b3e415b0cf5c
child 27026 3602b81665b5
permissions -rw-r--r--
updated generated file;
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
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   191
  automatically. If a 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
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   209
\left[\;\begin{minipage}{0.45\textwidth}\vspace{6pt}
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%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   301
\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
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
   321
  The predefined function \isa{measure{\isasymColon}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ {\isasymRightarrow}\ bool} 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}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   443
The 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
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   456
  printed. Looking at these will often point to a missing lemma.
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   457
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   458
%  As a more real example, here is quicksort:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   459
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   460
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   461
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   462
\isamarkupsection{Mutual Recursion%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   463
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   464
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   465
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   466
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   467
If two or more functions call one another mutually, they have to be defined
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   468
  in one step. Here are \isa{even} and \isa{odd}:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   469
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   470
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   471
\isacommand{function}\isamarkupfalse%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   472
\ even\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   473
\ \ \ \ \isakeyword{and}\ odd\ \ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   474
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   475
\ \ {\isachardoublequoteopen}even\ {\isadigit{0}}\ {\isacharequal}\ True{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   476
{\isacharbar}\ {\isachardoublequoteopen}odd\ {\isadigit{0}}\ {\isacharequal}\ False{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   477
{\isacharbar}\ {\isachardoublequoteopen}even\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ odd\ n{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   478
{\isacharbar}\ {\isachardoublequoteopen}odd\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ even\ n{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   479
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   480
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   481
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   482
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   483
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   484
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   485
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   486
\ pat{\isacharunderscore}completeness\ auto%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   487
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   488
{\isafoldproof}%
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
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   491
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   492
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   493
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   494
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   495
To eliminate the mutual dependencies, Isabelle internally
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   496
  creates a single function operating on the sum
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   497
  type \isa{nat\ {\isacharplus}\ nat}. Then, \isa{even} and \isa{odd} are
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   498
  defined as projections. Consequently, termination has to be proved
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   499
  simultaneously for both functions, by specifying a measure on the
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   500
  sum type:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   501
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   502
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   503
\isacommand{termination}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   504
\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   505
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   506
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   507
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   508
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   509
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   510
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   511
\isacommand{by}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   512
\ {\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
   513
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   514
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   515
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   516
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   517
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   518
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   519
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   520
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   521
We could also have used \isa{lexicographic{\isacharunderscore}order}, which
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   522
  supports mutual recursive termination proofs to a certain extent.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   523
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   524
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   525
%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   526
\isamarkupsubsection{Induction for mutual recursion%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   527
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   528
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   529
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   530
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   531
When functions are mutually recursive, proving properties about them
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   532
  generally requires simultaneous induction. The induction rule \isa{even{\isacharunderscore}odd{\isachardot}induct}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   533
  generated from the above definition reflects this.
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   534
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   535
  Let us prove something about \isa{even} and \isa{odd}:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   536
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   537
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   538
\isacommand{lemma}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   539
\ even{\isacharunderscore}odd{\isacharunderscore}mod{\isadigit{2}}{\isacharcolon}\isanewline
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   540
\ \ {\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
   541
\ \ {\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
   542
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   543
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   544
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   545
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   546
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   547
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   548
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   549
We apply simultaneous induction, specifying the induction variable
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   550
  for both goals, separated by \cmd{and}:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   551
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   552
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   553
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   554
\ {\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
   555
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   556
We get four subgoals, which correspond to the clauses in the
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   557
  definition of \isa{even} and \isa{odd}:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   558
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   559
\ {\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
   560
\ {\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
   561
\ {\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
   562
\ {\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
   563
\end{isabelle}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   564
  Simplification solves the first two goals, leaving us with two
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   565
  statements about the \isa{mod} operation to prove:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   566
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   567
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   568
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   569
\ simp{\isacharunderscore}all%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   570
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   571
\begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   572
\ {\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
   573
\ {\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
   574
\end{isabelle} 
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   575
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   576
  \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
   577
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   578
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   579
\isacommand{apply}\isamarkupfalse%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   580
\ arith\isanewline
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   581
\isacommand{apply}\isamarkupfalse%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   582
\ arith\isanewline
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   583
\isacommand{done}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   584
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   585
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   586
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   587
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   588
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   589
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   590
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   591
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   592
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   593
In proofs like this, the simultaneous induction is really essential:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   594
  Even if we are just interested in one of the results, the other
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   595
  one is necessary to strengthen the induction hypothesis. If we leave
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   596
  out the statement about \isa{odd} (by substituting it with \isa{True}), the same proof fails:%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   597
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   598
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   599
\isacommand{lemma}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   600
\ failed{\isacharunderscore}attempt{\isacharcolon}\isanewline
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   601
\ \ {\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
   602
\ \ {\isachardoublequoteopen}True{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   603
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   604
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   605
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   606
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   607
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   608
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   609
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   610
\ {\isacharparenleft}induct\ n\ rule{\isacharcolon}\ even{\isacharunderscore}odd{\isachardot}induct{\isacharparenright}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   611
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   612
\noindent Now the third subgoal is a dead end, since we have no
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   613
  useful induction hypothesis available:
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   614
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   615
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   616
\ {\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
   617
\ {\isadigit{2}}{\isachardot}\ True\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   618
\ {\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
   619
\ {\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
   620
\end{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   621
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   622
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   623
\isacommand{oops}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   624
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   625
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   626
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   627
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   628
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   629
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   630
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   631
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   632
\isamarkupsection{General pattern matching%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   633
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   634
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   635
%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   636
\begin{isamarkuptext}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   637
\label{genpats}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   638
\end{isamarkuptext}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   639
\isamarkuptrue%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   640
%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   641
\isamarkupsubsection{Avoiding automatic pattern splitting%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   642
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   643
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   644
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   645
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   646
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
   647
  patterns were always disjoint and complete, and if they weren't,
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   648
  they were made disjoint automatically like in the definition of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   649
  \isa{sep} in \S\ref{patmatch}.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   650
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   651
  This automatic splitting can significantly increase the number of
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   652
  equations involved, and this is not always desirable. The following
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   653
  example shows the problem:
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   654
  
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   655
  Suppose we are modeling incomplete knowledge about the world by a
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   656
  three-valued datatype, which has values \isa{T}, \isa{F}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   657
  and \isa{X} for true, false and uncertain propositions, respectively.%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   658
\end{isamarkuptext}%
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
\isacommand{datatype}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   661
\ P{\isadigit{3}}\ {\isacharequal}\ T\ {\isacharbar}\ F\ {\isacharbar}\ X%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   662
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   663
\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
   664
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   665
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   666
\isacommand{fun}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   667
\ 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
   668
\isakeyword{where}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   669
\ \ {\isachardoublequoteopen}And\ T\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   670
{\isacharbar}\ {\isachardoublequoteopen}And\ p\ T\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   671
{\isacharbar}\ {\isachardoublequoteopen}And\ p\ F\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   672
{\isacharbar}\ {\isachardoublequoteopen}And\ F\ p\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   673
{\isacharbar}\ {\isachardoublequoteopen}And\ X\ X\ {\isacharequal}\ X{\isachardoublequoteclose}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   674
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   675
This definition is useful, because the equations can directly be used
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   676
  as simplification rules rules. But the patterns overlap: For example,
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   677
  the expression \isa{And\ T\ T} is matched by both the first and
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   678
  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
   679
  splitting them up, producing instances:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   680
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   681
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   682
\isacommand{thm}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   683
\ And{\isachardot}simps%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   684
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   685
\isa{And\ T\ {\isacharquery}p\ {\isacharequal}\ {\isacharquery}p\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   686
And\ F\ T\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   687
And\ X\ T\ {\isacharequal}\ X\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   688
And\ F\ F\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   689
And\ X\ F\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   690
And\ F\ X\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   691
And\ X\ X\ {\isacharequal}\ X}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   692
  
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   693
  \vspace*{1em}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   694
  \noindent There are several problems with this:
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   695
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   696
  \begin{enumerate}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   697
  \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
   698
  explosion of equations. For \isa{And}, we get seven instead of
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   699
  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
   700
  example.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   701
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   702
  \item Since splitting makes the equations \qt{less general}, they
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   703
  do not always match in rewriting. While the term \isa{And\ x\ F}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   704
  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
   705
  (manual) case split on \isa{x} is now necessary.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   706
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   707
  \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
   708
  means that our induction proofs will have more cases.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   709
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   710
  \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
   711
  back which we put in.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   712
  \end{enumerate}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   713
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   714
  If we do not want the automatic splitting, we can switch it off by
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   715
  leaving out the \cmd{sequential} option. However, we will have to
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   716
  prove that our pattern matching is consistent\footnote{This prevents
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   717
  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
   718
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   719
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   720
\isacommand{function}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   721
\ 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
   722
\isakeyword{where}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   723
\ \ {\isachardoublequoteopen}And{\isadigit{2}}\ T\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   724
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ p\ T\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   725
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ p\ F\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   726
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ F\ p\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
   727
{\isacharbar}\ {\isachardoublequoteopen}And{\isadigit{2}}\ X\ X\ {\isacharequal}\ X{\isachardoublequoteclose}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   728
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   729
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   730
\endisadelimproof
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
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   733
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   734
\begin{isamarkuptxt}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   735
\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
   736
  function definition. In this case, they are:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   737
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   738
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   739
\ {\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
   740
\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
   741
\isaindent{\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}P\ x{\isachardot}\ }{\isasymLongrightarrow}\ P\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   742
\ {\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
   743
\ {\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
   744
\ {\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
   745
\ {\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
   746
\ {\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
   747
\ {\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
   748
\ {\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
   749
\ {\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
   750
\ {\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
   751
\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
   752
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   753
  The first subgoal expresses the completeness of the patterns. It has
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   754
  the form of an elimination rule and states that every \isa{x} of
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   755
  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
   756
  be equivalently stated as a disjunction of existential statements: 
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   757
\isa{{\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}T{\isacharcomma}\ p{\isacharparenright}{\isacharparenright}\ {\isasymor}\ {\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}{\isacharparenright}\ {\isasymor}\ {\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}p{\isacharcomma}\ F{\isacharparenright}{\isacharparenright}\ {\isasymor}\ {\isacharparenleft}{\isasymexists}p{\isachardot}\ x\ {\isacharequal}\ {\isacharparenleft}F{\isacharcomma}\ p{\isacharparenright}{\isacharparenright}\ {\isasymor}\ x\ {\isacharequal}\ {\isacharparenleft}X{\isacharcomma}\ X{\isacharparenright}}.}. If the patterns just involve
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   758
  datatypes, we can solve it with the \isa{pat{\isacharunderscore}completeness}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   759
  method:%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   760
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   761
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   762
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   763
\ pat{\isacharunderscore}completeness%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   764
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   765
The remaining subgoals express \emph{pattern compatibility}. We do
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   766
  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
   767
  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
   768
  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
   769
  subgoal. Usually this needs injectivity of the constructors, which
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   770
  is used automatically by \isa{auto}.%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   771
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   772
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   773
\isacommand{by}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   774
\ auto%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   775
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   776
{\isafoldproof}%
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
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   779
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   780
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   781
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   782
\isamarkupsubsection{Non-constructor patterns%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   783
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   784
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   785
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   786
\begin{isamarkuptext}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   787
Most of Isabelle's basic types take the form of inductive datatypes,
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   788
  and usually pattern matching works on the constructors of such types. 
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   789
  However, this need not be always the case, and the \cmd{function}
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   790
  command handles other kind of patterns, too.
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   791
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   792
  One well-known instance of non-constructor patterns are
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   793
  so-called \emph{$n+k$-patterns}, which are a little controversial in
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   794
  the functional programming world. Here is the initial fibonacci
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   795
  example with $n+k$-patterns:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   796
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   797
\isamarkuptrue%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   798
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   799
\ fib{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   800
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   801
\ \ {\isachardoublequoteopen}fib{\isadigit{2}}\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   802
{\isacharbar}\ {\isachardoublequoteopen}fib{\isadigit{2}}\ {\isadigit{1}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   803
{\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
   804
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   805
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   806
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   807
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   808
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   809
\isatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   810
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   811
\endisatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   812
{\isafoldML}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   813
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   814
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   815
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   816
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   817
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   818
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   819
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   820
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   821
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   822
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   823
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   824
\begin{isamarkuptxt}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   825
This kind of matching is again justified by the proof of pattern
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   826
  completeness and compatibility. 
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   827
  The proof obligation for pattern completeness states that every natural number is
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   828
  either \isa{{\isadigit{0}}}, \isa{{\isadigit{1}}} or \isa{n\ {\isacharplus}\ {\isadigit{2}}}:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   829
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   830
  \begin{isabelle}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   831
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}P\ x{\isachardot}\ {\isasymlbrakk}x\ {\isacharequal}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\ x\ {\isacharequal}\ {\isadigit{1}}\ {\isasymLongrightarrow}\ P{\isacharsemicolon}\ {\isasymAnd}n{\isachardot}\ x\ {\isacharequal}\ n\ {\isacharplus}\ {\isadigit{2}}\ {\isasymLongrightarrow}\ P{\isasymrbrakk}\ {\isasymLongrightarrow}\ P%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   832
\end{isabelle}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   833
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   834
  This is an arithmetic triviality, but unfortunately the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   835
  \isa{arith} method cannot handle this specific form of an
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
   836
  elimination rule. However, we can use the method \isa{atomize{\isacharunderscore}elim} to do an ad-hoc conversion to a disjunction of
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   837
  existentials, which can then be soved by the arithmetic decision procedure.
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   838
  Pattern compatibility and termination are automatic as usual.%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   839
\end{isamarkuptxt}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   840
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   841
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   842
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   843
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   844
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   845
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   846
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   847
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   848
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   849
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   850
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   851
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   852
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   853
\isatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   854
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   855
\endisatagML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   856
{\isafoldML}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   857
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   858
\isadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   859
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   860
\endisadelimML
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   861
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   862
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   863
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   864
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   865
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   866
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   867
\isacommand{apply}\isamarkupfalse%
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
   868
\ atomize{\isacharunderscore}elim\isanewline
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   869
\isacommand{apply}\isamarkupfalse%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   870
\ arith\isanewline
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   871
\isacommand{apply}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   872
\ auto\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   873
\isacommand{done}\isamarkupfalse%
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
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   882
\isacommand{termination}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   883
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   884
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   885
\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   886
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   887
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   888
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   889
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   890
\ lexicographic{\isacharunderscore}order%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   891
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   892
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   893
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   894
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   895
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   896
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   897
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   898
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   899
We can stretch the notion of pattern matching even more. The
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   900
  following function is not a sensible functional program, but a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   901
  perfectly valid mathematical definition:%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   902
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   903
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   904
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   905
\ ev\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   906
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   907
\ \ {\isachardoublequoteopen}ev\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   908
{\isacharbar}\ {\isachardoublequoteopen}ev\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}\ {\isacharequal}\ False{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   909
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   910
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   911
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   912
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   913
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   914
\isatagproof
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   915
\isacommand{apply}\isamarkupfalse%
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
   916
\ atomize{\isacharunderscore}elim\isanewline
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   917
\isacommand{by}\isamarkupfalse%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
   918
\ arith{\isacharplus}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   919
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   920
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   921
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   922
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   923
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   924
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   925
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   926
\isacommand{termination}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   927
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   928
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   929
\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   930
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   931
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   932
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   933
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   934
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}{\isacharparenright}\ simp%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   935
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   936
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   937
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   938
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   939
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   940
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   941
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   942
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   943
This general notion of pattern matching gives you the full freedom
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   944
  of mathematical specifications. However, as always, freedom should
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   945
  be used with care:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   946
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   947
  If we leave the area of constructor
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   948
  patterns, we have effectively departed from the world of functional
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   949
  programming. This means that it is no longer possible to use the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   950
  code generator, and expect it to generate ML code for our
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   951
  definitions. Also, such a specification might not work very well together with
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   952
  simplification. Your mileage may vary.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   953
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   954
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   955
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   956
\isamarkupsubsection{Conditional equations%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   957
}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   958
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   959
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   960
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   961
The function package also supports conditional equations, which are
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   962
  similar to guards in a language like Haskell. Here is Euclid's
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   963
  algorithm written with conditional patterns\footnote{Note that the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   964
  patterns are also overlapping in the base case}:%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   965
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   966
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   967
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   968
\ gcd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   969
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   970
\ \ {\isachardoublequoteopen}gcd\ x\ {\isadigit{0}}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   971
{\isacharbar}\ {\isachardoublequoteopen}gcd\ {\isadigit{0}}\ y\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   972
{\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
   973
{\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
   974
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   975
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   976
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   977
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   978
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   979
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   980
\isacommand{by}\isamarkupfalse%
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
   981
\ {\isacharparenleft}atomize{\isacharunderscore}elim{\isacharcomma}\ auto{\isacharcomma}\ arith{\isacharparenright}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   982
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   983
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   984
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   985
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   986
\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   987
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   988
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   989
\isacommand{termination}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   990
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   991
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   992
\ %
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   993
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   994
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   995
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   996
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   997
\ lexicographic{\isacharunderscore}order%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   998
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
   999
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1000
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1001
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1002
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1003
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1004
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1005
\begin{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1006
By now, you can probably guess what the proof obligations for the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1007
  pattern completeness and compatibility look like. 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1008
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1009
  Again, functions with conditional patterns are not supported by the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1010
  code generator.%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1011
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1012
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1013
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1014
\isamarkupsubsection{Pattern matching on strings%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1015
}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1016
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1017
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1018
\begin{isamarkuptext}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1019
As strings (as lists of characters) are normal datatypes, pattern
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1020
  matching on them is possible, but somewhat problematic. Consider the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1021
  following definition:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1022
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1023
\end{isamarkuptext}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1024
\noindent\cmd{fun} \isa{check\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}string\ {\isasymRightarrow}\ bool{\isachardoublequote}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1025
\cmd{where}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1026
\hspace*{2ex}\isa{{\isachardoublequote}check\ {\isacharparenleft}{\isacharprime}{\isacharprime}good{\isacharprime}{\isacharprime}{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequote}}\\%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1027
\isa{{\isacharbar}\ {\isachardoublequote}check\ s\ {\isacharequal}\ False{\isachardoublequote}}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1028
\begin{isamarkuptext}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1029
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1030
  \noindent An invocation of the above \cmd{fun} command does not
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1031
  terminate. What is the problem? Strings are lists of characters, and
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1032
  characters are a datatype with a lot of constructors. Splitting the
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1033
  catch-all pattern thus leads to an explosion of cases, which cannot
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1034
  be handled by Isabelle.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1035
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1036
  There are two things we can do here. Either we write an explicit
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1037
  \isa{if} on the right hand side, or we can use conditional patterns:%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1038
\end{isamarkuptext}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1039
\isamarkuptrue%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1040
\isacommand{function}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1041
\ check\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}string\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1042
\isakeyword{where}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1043
\ \ {\isachardoublequoteopen}check\ {\isacharparenleft}{\isacharprime}{\isacharprime}good{\isacharprime}{\isacharprime}{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1044
{\isacharbar}\ {\isachardoublequoteopen}s\ {\isasymnoteq}\ {\isacharprime}{\isacharprime}good{\isacharprime}{\isacharprime}\ {\isasymLongrightarrow}\ check\ s\ {\isacharequal}\ False{\isachardoublequoteclose}\isanewline
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1045
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1046
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1047
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1048
\endisadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1049
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1050
\isatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1051
\isacommand{by}\isamarkupfalse%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1052
\ auto%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1053
\endisatagproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1054
{\isafoldproof}%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1055
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1056
\isadelimproof
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1057
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1058
\endisadelimproof
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1059
%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1060
\isamarkupsection{Partiality%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1061
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1062
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1063
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1064
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1065
In HOL, all functions are total. A function \isa{f} applied to
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1066
  \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
  1067
  of undefinedness. 
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1068
  This is why we have to do termination
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1069
  proofs when defining functions: The proof justifies that the
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1070
  function can be defined by wellfounded recursion.
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1071
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1072
  However, the \cmd{function} package does support partiality to a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1073
  certain extent. Let's look at the following function which looks
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1074
  for a zero of a given function f.%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1075
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1076
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1077
\isacommand{function}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1078
\ findzero\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1079
\isakeyword{where}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1080
\ \ {\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
  1081
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1082
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1083
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1084
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1085
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1086
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1087
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1088
\ pat{\isacharunderscore}completeness\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1089
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1090
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1091
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1092
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1093
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1094
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1095
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1096
\begin{isamarkuptext}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1097
\noindent Clearly, any attempt of a termination proof must fail. And without
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1098
  that, we do not get the usual rules \isa{findzero{\isachardot}simp} and 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1099
  \isa{findzero{\isachardot}induct}. So what was the definition good for at all?%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1100
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1101
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1102
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1103
\isamarkupsubsection{Domain predicates%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1104
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1105
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1106
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1107
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1108
The trick is that Isabelle has not only defined the function \isa{findzero}, but also
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1109
  a predicate \isa{findzero{\isacharunderscore}dom} that characterizes the values where the function
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1110
  terminates: the \emph{domain} of the function. If we treat a
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1111
  partial function just as a total function with an additional domain
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1112
  predicate, we can derive simplification and
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1113
  induction rules as we do for total functions. They are guarded
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1114
  by domain conditions and are called \isa{psimps} and \isa{pinduct}:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1115
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1116
\isamarkuptrue%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1117
%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1118
\begin{isamarkuptext}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1119
\noindent\begin{minipage}{0.79\textwidth}\begin{isabelle}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1120
findzero{\isacharunderscore}dom\ {\isacharparenleft}{\isacharquery}f{\isacharcomma}\ {\isacharquery}n{\isacharparenright}\ {\isasymLongrightarrow}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1121
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
  1122
\end{isabelle}\end{minipage}
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1123
  \hfill(\isa{findzero{\isachardot}psimps})
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1124
  \vspace{1em}
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1125
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1126
  \noindent\begin{minipage}{0.79\textwidth}\begin{isabelle}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1127
{\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
  1128
\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
  1129
{\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
  1130
\end{isabelle}\end{minipage}
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1131
  \hfill(\isa{findzero{\isachardot}pinduct})%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1132
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1133
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1134
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1135
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1136
Remember that all we
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1137
  are doing here is use some tricks to make a total function appear
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1138
  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
  1139
  to some natural number, although we might not be able to find out
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1140
  which one. The function is \emph{underdefined}.
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1141
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1142
  But it is defined enough to prove something interesting about it. We
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1143
  can prove that if \isa{findzero\ f\ n}
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1144
  terminates, it indeed returns a zero of \isa{f}:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1145
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1146
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1147
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1148
\ 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
  1149
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1150
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1151
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1152
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1153
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1154
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1155
\begin{isamarkuptxt}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1156
\noindent We apply induction as usual, but using the partial induction
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1157
  rule:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1158
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1159
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1160
\isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1161
\ {\isacharparenleft}induct\ f\ n\ rule{\isacharcolon}\ findzero{\isachardot}pinduct{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1162
\begin{isamarkuptxt}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1163
\noindent This gives the following subgoals:
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1164
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1165
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1166
\ {\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
  1167
\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
  1168
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1169
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1170
  \noindent The hypothesis in our lemma was used to satisfy the first premise in
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1171
  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
  1172
  allows to unfold \isa{findzero\ f\ n} using the \isa{psimps}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1173
  rule, and the rest is trivial. Since the \isa{psimps} rules carry the
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1174
  \isa{{\isacharbrackleft}simp{\isacharbrackright}} attribute by default, we just need a single step:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1175
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1176
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1177
\isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1178
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1179
\isacommand{done}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1180
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1181
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1182
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1183
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1184
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1185
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1186
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1187
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1188
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1189
Proofs about partial functions are often not harder than for total
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1190
  functions. Fig.~\ref{findzero_isar} shows a slightly more
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1191
  complicated proof written in Isar. It is verbose enough to show how
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1192
  partiality comes into play: From the partial induction, we get an
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1193
  additional domain condition hypothesis. Observe how this condition
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1194
  is applied when calls to \isa{findzero} are unfolded.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1195
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1196
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1197
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1198
\begin{figure}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1199
\hrule\vspace{6pt}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1200
\begin{minipage}{0.8\textwidth}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1201
\isabellestyle{it}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1202
\isastyle\isamarkuptrue
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1203
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1204
\ {\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
  1205
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1206
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1207
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1208
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1209
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1210
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1211
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1212
\ {\isacharparenleft}induct\ rule{\isacharcolon}\ findzero{\isachardot}pinduct{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1213
\ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1214
\ f\ n\ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1215
\ dom{\isacharcolon}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1216
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \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
  1217
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \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
  1218
\ \ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1219
\ {\isachardoublequoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1220
\ \ \isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1221
\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1222
\ \ \ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1223
\ {\isachardoublequoteopen}f\ n\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1224
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1225
\ dom\ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1226
\ {\isachardoublequoteopen}findzero\ f\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1227
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1228
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1229
\ x{\isacharunderscore}range\ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1230
\ False\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1231
\ auto\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1232
\ \ \isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1233
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1234
\ \ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1235
\ \ \isacommand{from}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1236
\ x{\isacharunderscore}range\ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1237
\ {\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
  1238
\ auto\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1239
\ \ \isacommand{thus}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1240
\ {\isachardoublequoteopen}f\ x\ {\isasymnoteq}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1241
\ \ \isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1242
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1243
\ \ \ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1244
\ {\isachardoublequoteopen}x\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1245
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1246
\ {\isacharbackquoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isacharbackquoteclose}\ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1247
\ {\isacharquery}thesis\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1248
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1249
\ \ \isacommand{next}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1250
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1251
\ \ \ \ \isacommand{assume}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1252
\ {\isachardoublequoteopen}x\ {\isasymin}\ {\isacharbraceleft}Suc\ n\ {\isachardot}{\isachardot}{\isacharless}\ findzero\ f\ n{\isacharbraceright}{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1253
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1254
\ dom\ \isakeyword{and}\ {\isacharbackquoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isacharbackquoteclose}\ \isacommand{have}\isamarkupfalse%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1255
\ {\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
  1256
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1257
\ \ \ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1258
\ IH\ \isakeyword{and}\ {\isacharbackquoteopen}f\ n\ {\isasymnoteq}\ {\isadigit{0}}{\isacharbackquoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1259
\ \ \ \ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1260
\ {\isacharquery}thesis\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1261
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1262
\ \ \isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1263
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1264
\isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1265
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1266
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1267
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1268
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1269
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1270
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1271
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1272
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1273
\isamarkupfalse\isabellestyle{tt}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1274
\end{minipage}\vspace{6pt}\hrule
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1275
\caption{A proof about a partial function}\label{findzero_isar}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1276
\end{figure}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1277
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1278
\isamarkupsubsection{Partial termination proofs%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1279
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1280
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1281
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1282
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1283
Now that we have proved some interesting properties about our
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1284
  function, we should turn to the domain predicate and see if it is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1285
  actually true for some values. Otherwise we would have just proved
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1286
  lemmas with \isa{False} as a premise.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1287
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1288
  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
  1289
  introduction rules automatically. But since they are not used very
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1290
  often (they are almost never needed if the function is total), this
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1291
  functionality is disabled by default for efficiency reasons. So we have to go
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1292
  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
  1293
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1294
\vspace{1ex}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1295
\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
  1296
\cmd{where}\isanewline%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1297
\ \ \ldots\\
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1298
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1299
  \noindent Now the package has proved an introduction rule for \isa{findzero{\isacharunderscore}dom}:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1300
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1301
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1302
\isacommand{thm}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1303
\ findzero{\isachardot}domintros%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1304
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1305
\begin{isabelle}%
25182
64e3f45dc6f4 updated;
wenzelm
parents: 25160
diff changeset
  1306
{\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
  1307
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1308
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1309
  Domain introduction rules allow to show that a given value lies in the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1310
  domain of a function, if the arguments of all recursive calls
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1311
  are in the domain as well. They allow to do a \qt{single step} in a
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1312
  termination proof. Usually, you want to combine them with a suitable
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1313
  induction principle.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1314
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1315
  Since our function increases its argument at recursive calls, we
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1316
  need an induction principle which works \qt{backwards}. We will use
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1317
  \isa{inc{\isacharunderscore}induct}, which allows to do induction from a fixed number
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1318
  \qt{downwards}:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1319
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1320
  \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
  1321
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1322
  Figure \ref{findzero_term} gives a detailed Isar proof of the fact
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1323
  that \isa{findzero} terminates if there is a zero which is greater
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1324
  or equal to \isa{n}. First we derive two useful rules which will
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1325
  solve the base case and the step case of the induction. The
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1326
  induction is then straightforward, except for the unusual induction
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1327
  principle.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1328
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1329
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1330
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1331
\begin{figure}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1332
\hrule\vspace{6pt}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1333
\begin{minipage}{0.8\textwidth}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1334
\isabellestyle{it}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1335
\isastyle\isamarkuptrue
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1336
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1337
\ findzero{\isacharunderscore}termination{\isacharcolon}\isanewline
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1338
\ \ \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
  1339
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1340
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1341
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1342
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1343
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1344
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1345
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1346
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1347
\ {\isacharminus}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1348
\ \ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1349
\ base{\isacharcolon}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ x{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1350
\ \ \ \ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1351
\ {\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
  1352
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1353
\ \ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1354
\ step{\isacharcolon}\ {\isachardoublequoteopen}{\isasymAnd}i{\isachardot}\ findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ Suc\ i{\isacharparenright}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1355
\ \ \ \ {\isasymLongrightarrow}\ findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ i{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1356
\ \ \ \ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1357
\ {\isacharparenleft}rule\ findzero{\isachardot}domintros{\isacharparenright}\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1358
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1359
\ \ \isacommand{from}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1360
\ {\isacharbackquoteopen}x\ {\isasymge}\ n{\isacharbackquoteclose}\ \isacommand{show}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1361
\ {\isacharquery}thesis\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1362
\ \ \isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1363
\ {\isacharparenleft}induct\ rule{\isacharcolon}inc{\isacharunderscore}induct{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1364
\ \ \ \ \isacommand{show}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1365
\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ x{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1366
\ {\isacharparenleft}rule\ base{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1367
\ \ \isacommand{next}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1368
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1369
\ \ \ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1370
\ i\ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1371
\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ Suc\ i{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1372
\ \ \ \ \isacommand{thus}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1373
\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ i{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1374
\ {\isacharparenleft}rule\ step{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1375
\ \ \isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1376
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1377
\isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1378
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1379
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1380
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1381
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1382
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1383
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1384
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1385
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1386
\isamarkupfalse\isabellestyle{tt}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1387
\end{minipage}\vspace{6pt}\hrule
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1388
\caption{Termination proof for \isa{findzero}}\label{findzero_term}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1389
\end{figure}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1390
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1391
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1392
Again, the proof given in Fig.~\ref{findzero_term} has a lot of
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1393
  detail in order to explain the principles. Using more automation, we
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1394
  can also have a short proof:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1395
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1396
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1397
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1398
\ findzero{\isacharunderscore}termination{\isacharunderscore}short{\isacharcolon}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1399
\ \ \isakeyword{assumes}\ zero{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isachargreater}{\isacharequal}\ n{\isachardoublequoteclose}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1400
\ \ \isakeyword{assumes}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}f\ x\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1401
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}findzero{\isacharunderscore}dom\ {\isacharparenleft}f{\isacharcomma}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1402
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1403
\isadelimproof
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1404
%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1405
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1406
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1407
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1408
\isacommand{using}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1409
\ zero\isanewline
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1410
\isacommand{by}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1411
\ {\isacharparenleft}induct\ rule{\isacharcolon}inc{\isacharunderscore}induct{\isacharparenright}\ {\isacharparenleft}auto\ intro{\isacharcolon}\ findzero{\isachardot}domintros{\isacharparenright}%
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
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1420
\noindent It is simple to combine the partial correctness result with the
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1421
  termination lemma:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1422
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1423
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1424
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1425
\ findzero{\isacharunderscore}total{\isacharunderscore}correctness{\isacharcolon}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1426
\ \ {\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
  1427
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1428
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1429
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1430
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1431
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1432
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1433
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1434
\ {\isacharparenleft}blast\ intro{\isacharcolon}\ findzero{\isacharunderscore}zero\ findzero{\isacharunderscore}termination{\isacharparenright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1435
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1436
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1437
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1438
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1439
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1440
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1441
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1442
\isamarkupsubsection{Definition of the domain predicate%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1443
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1444
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1445
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1446
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1447
Sometimes it is useful to know what the definition of the domain
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1448
  predicate looks like. Actually, \isa{findzero{\isacharunderscore}dom} is just an
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1449
  abbreviation:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1450
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1451
  \begin{isabelle}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1452
findzero{\isacharunderscore}dom\ {\isasymequiv}\ accp\ findzero{\isacharunderscore}rel%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1453
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1454
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1455
  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
  1456
  package. \isa{findzero{\isacharunderscore}rel} is just a normal
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1457
  inductive predicate, so we can inspect its definition by
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1458
  looking at the introduction rules \isa{findzero{\isacharunderscore}rel{\isachardot}intros}.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1459
  In our case there is just a single rule:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1460
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1461
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1462
{\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
  1463
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1464
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1465
  The predicate \isa{findzero{\isacharunderscore}rel}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1466
  describes the \emph{recursion relation} of the function
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1467
  definition. The recursion relation is a binary relation on
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1468
  the arguments of the function that relates each argument to its
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1469
  recursive calls. In general, there is one introduction rule for each
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1470
  recursive call.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1471
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1472
  The predicate \isa{findzero{\isacharunderscore}dom} is the accessible part of
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1473
  that relation. An argument belongs to the accessible part, if it can
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1474
  be reached in a finite number of steps (cf.~its definition in \isa{Accessible{\isacharunderscore}Part{\isachardot}thy}).
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1475
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1476
  Since the domain predicate is just an abbreviation, you can use
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1477
  lemmas for \isa{accp} and \isa{findzero{\isacharunderscore}rel} directly. Some
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1478
  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
  1479
  for the recursion relation \isa{findzero{\isachardot}intros} and \isa{findzero{\isachardot}cases}.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1480
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1481
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1482
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1483
\isamarkupsubsection{A Useful Special Case: Tail recursion%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1484
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1485
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1486
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1487
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1488
The domain predicate is our trick that allows us to model partiality
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1489
  in a world of total functions. The downside of this is that we have
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1490
  to carry it around all the time. The termination proof above allowed
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1491
  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
  1492
  concrete \isa{n\ {\isasymle}\ x\ {\isasymand}\ f\ x\ {\isacharequal}\ {\isadigit{0}}}, but the condition is still
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1493
  there and can only be discharged for special cases.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1494
  In particular, the domain predicate guards the unfolding of our
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1495
  function, since it is there as a condition in the \isa{psimp}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1496
  rules. 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1497
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1498
  Now there is an important special case: We can actually get rid
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1499
  of the condition in the simplification rules, \emph{if the function
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1500
  is tail-recursive}. The reason is that for all tail-recursive
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1501
  equations there is a total function satisfying them, even if they
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1502
  are non-terminating. 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1503
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1504
%  A function is tail recursive, if each call to the function is either
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1505
%  equal
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1506
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1507
%  So the outer form of the 
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1508
%
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1509
%if it can be written in the following
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1510
%  form:
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1511
%  {term[display] "f x = (if COND x then BASE x else f (LOOP x))"}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1512
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1513
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1514
  The function package internally does the right construction and can
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1515
  derive the unconditional simp rules, if we ask it to do so. Luckily,
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1516
  our \isa{findzero} function is tail-recursive, so we can just go
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1517
  back and add another option to the \cmd{function} command:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1518
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1519
\vspace{1ex}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1520
\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
  1521
\cmd{where}\isanewline%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1522
\ \ \ldots\\%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1523
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1524
  
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1525
  \noindent Now, we actually get unconditional simplification rules, even
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1526
  though the function is partial:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1527
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1528
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1529
\isacommand{thm}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1530
\ findzero{\isachardot}simps%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1531
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1532
\begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1533
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
  1534
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1535
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1536
  \noindent Of course these would make the simplifier loop, so we better remove
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1537
  them from the simpset:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1538
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1539
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1540
\isacommand{declare}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1541
\ findzero{\isachardot}simps{\isacharbrackleft}simp\ del{\isacharbrackright}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1542
\begin{isamarkuptext}%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1543
Getting rid of the domain conditions in the simplification rules is
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1544
  not only useful because it simplifies proofs. It is also required in
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1545
  order to use Isabelle's code generator to generate ML code
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1546
  from a function definition.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1547
  Since the code generator only works with equations, it cannot be
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1548
  used with \isa{psimp} rules. Thus, in order to generate code for
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1549
  partial functions, they must be defined as a tail recursion.
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1550
  Luckily, many functions have a relatively natural tail recursive
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1551
  definition.%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1552
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1553
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1554
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
  1555
\isamarkupsection{Nested recursion%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1556
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1557
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1558
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  1559
\begin{isamarkuptext}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1560
Recursive calls which are nested in one another frequently cause
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1561
  complications, since their termination proof can depend on a partial
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1562
  correctness property of the function itself. 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1563
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1564
  As a small example, we define the \qt{nested zero} function:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1565
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1566
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1567
\isacommand{function}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1568
\ nz\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1569
\isakeyword{where}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1570
\ \ {\isachardoublequoteopen}nz\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1571
{\isacharbar}\ {\isachardoublequoteopen}nz\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ nz\ {\isacharparenleft}nz\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1572
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1573
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1574
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1575
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1576
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1577
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1578
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1579
\ pat{\isacharunderscore}completeness\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1580
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1581
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1582
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1583
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1584
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1585
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1586
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1587
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1588
If we attempt to prove termination using the identity measure on
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1589
  naturals, this fails:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1590
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1591
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1592
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1593
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1594
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1595
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1596
\ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1597
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1598
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1599
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1600
\isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1601
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1602
\ \ \isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1603
\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1604
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1605
We get stuck with the subgoal
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1606
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1607
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1608
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}n{\isachardot}\ nz{\isacharunderscore}dom\ n\ {\isasymLongrightarrow}\ nz\ n\ {\isacharless}\ Suc\ n%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1609
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1610
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1611
  Of course this statement is true, since we know that \isa{nz} is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1612
  the zero function. And in fact we have no problem proving this
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1613
  property by induction.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1614
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1615
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1616
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1617
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1618
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1619
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1620
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1621
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1622
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1623
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1624
\ 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
  1625
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1626
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1627
\ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1628
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1629
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1630
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1631
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1632
\ {\isacharparenleft}induct\ rule{\isacharcolon}nz{\isachardot}pinduct{\isacharparenright}\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1633
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1634
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1635
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1636
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1637
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1638
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1639
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1640
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1641
We formulate this as a partial correctness lemma with the condition
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1642
  \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
  1643
  the termination proof works as expected:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1644
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1645
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1646
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1647
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1648
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1649
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1650
\ \ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1651
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1652
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1653
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1654
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1655
\ {\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
  1656
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1657
{\isafoldproof}%
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
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1664
As a general strategy, one should prove the statements needed for
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1665
  termination as a partial property first. Then they can be used to do
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1666
  the termination proof. This also works for less trivial
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1667
  examples. Figure \ref{f91} defines the 91-function, a well-known
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1668
  challenge problem due to John McCarthy, and proves its termination.%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1669
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1670
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1671
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1672
\begin{figure}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1673
\hrule\vspace{6pt}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1674
\begin{minipage}{0.8\textwidth}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1675
\isabellestyle{it}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1676
\isastyle\isamarkuptrue
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1677
\isacommand{function}\isamarkupfalse%
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1678
\ f{\isadigit{9}}{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1679
\isakeyword{where}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1680
\ \ {\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
  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
\ pat{\isacharunderscore}completeness\ auto%
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
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1694
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1695
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1696
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1697
\isacommand{lemma}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1698
\ f{\isadigit{9}}{\isadigit{1}}{\isacharunderscore}estimate{\isacharcolon}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1699
\ \ \isakeyword{assumes}\ trm{\isacharcolon}\ {\isachardoublequoteopen}f{\isadigit{9}}{\isadigit{1}}{\isacharunderscore}dom\ n{\isachardoublequoteclose}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1700
\ \ \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
  1701
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1702
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1703
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1704
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1705
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1706
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1707
\isacommand{using}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1708
\ trm\ \isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1709
\ induct\ auto%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1710
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1711
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1712
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1713
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1714
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1715
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1716
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1717
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1718
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1719
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1720
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1721
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1722
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1723
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1724
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1725
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1726
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1727
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1728
\ \ \isacommand{let}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1729
\ {\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
  1730
\ \ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1731
\ {\isachardoublequoteopen}wf\ {\isacharquery}R{\isachardoublequoteclose}\ \isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1732
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1733
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1734
\ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1735
\ n\ {\isacharcolon}{\isacharcolon}\ nat\ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1736
\ {\isachardoublequoteopen}{\isasymnot}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isacharless}\ n{\isachardoublequoteclose}\ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1737
\isamarkupcmt{Assumptions for both calls%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1738
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1739
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1740
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1741
\ \ \isacommand{thus}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1742
\ {\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
  1743
\ simp\ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1744
\isamarkupcmt{Inner call%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1745
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1746
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1747
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1748
\ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1749
\ 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
  1750
\isamarkupcmt{Outer call%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1751
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1752
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1753
\ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1754
\ f{\isadigit{9}}{\isadigit{1}}{\isacharunderscore}estimate\ \isacommand{have}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1755
\ {\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
  1756
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1757
\ \ \isacommand{with}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1758
\ {\isacharbackquoteopen}{\isasymnot}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isacharless}\ n{\isacharbackquoteclose}\ \isacommand{show}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1759
\ {\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
  1760
\ simp\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1761
\isacommand{qed}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1762
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1763
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1764
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1765
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1766
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1767
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1768
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1769
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1770
\isamarkupfalse\isabellestyle{tt}
23188
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1771
\end{minipage}
595a0e24bd8e updated
krauss
parents: 23003
diff changeset
  1772
\vspace{6pt}\hrule
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1773
\caption{McCarthy's 91-function}\label{f91}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1774
\end{figure}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1775
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1776
\isamarkupsection{Higher-Order Recursion%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1777
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1778
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1779
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1780
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1781
Higher-order recursion occurs when recursive calls
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1782
  are passed as arguments to higher-order combinators such as \isa{map}, \isa{filter} etc.
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1783
  As an example, imagine a datatype of n-ary trees:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1784
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1785
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1786
\isacommand{datatype}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1787
\ {\isacharprime}a\ tree\ {\isacharequal}\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1788
\ \ Leaf\ {\isacharprime}a\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1789
{\isacharbar}\ Branch\ {\isachardoublequoteopen}{\isacharprime}a\ tree\ list{\isachardoublequoteclose}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1790
\begin{isamarkuptext}%
25278
3026df96941d changed "treemap" example to "mirror"
krauss
parents: 25182
diff changeset
  1791
\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
  1792
  list functions \isa{rev} and \isa{map}:%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1793
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1794
\isamarkuptrue%
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
  1795
\isacommand{function}\isamarkupfalse%
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
  1796
\ mirror\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ tree\ {\isasymRightarrow}\ {\isacharprime}a\ tree{\isachardoublequoteclose}\isanewline
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
  1797
\isakeyword{where}\isanewline
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
  1798
\ \ {\isachardoublequoteopen}mirror\ {\isacharparenleft}Leaf\ n{\isacharparenright}\ {\isacharequal}\ Leaf\ n{\isachardoublequoteclose}\isanewline
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
  1799
{\isacharbar}\ {\isachardoublequoteopen}mirror\ {\isacharparenleft}Branch\ l{\isacharparenright}\ {\isacharequal}\ Branch\ {\isacharparenleft}rev\ {\isacharparenleft}map\ mirror\ l{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1800
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1801
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1802
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1803
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1804
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1805
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1806
\isacommand{by}\isamarkupfalse%
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
  1807
\ pat{\isacharunderscore}completeness\ auto%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1808
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1809
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1810
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1811
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1812
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1813
\endisadelimproof
26876
d50ef6b952ba updated generated file;
wenzelm
parents: 25731
diff changeset
  1814
%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1815
\begin{isamarkuptext}%
25731
b3e415b0cf5c updated;
wenzelm
parents: 25278
diff changeset
  1816
\emph{Note: The handling of size functions is currently changing 
b3e415b0cf5c updated;
wenzelm
parents: 25278
diff changeset
  1817
  in the developers version of Isabelle. So this documentation is outdated.}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1818
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1819
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1820
\isacommand{termination}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1821
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1822
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1823
\ %
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1824
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1825
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1826
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1827
\isacommand{proof}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1828
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1829
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1830
As usual, we have to give a wellfounded relation, such that the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1831
  arguments of the recursive calls get smaller. But what exactly are
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1832
  the arguments of the recursive calls? Isabelle gives us the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1833
  subgoals
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1834
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1835
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1836
\ {\isadigit{1}}{\isachardot}\ wf\ {\isacharquery}R\isanewline
25278
3026df96941d changed "treemap" example to "mirror"
krauss
parents: 25182
diff changeset
  1837
\ {\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
  1838
\end{isabelle} 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1839
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1840
  So Isabelle seems to know that \isa{map} behaves nicely and only
25278
3026df96941d changed "treemap" example to "mirror"
krauss
parents: 25182
diff changeset
  1841
  applies the recursive call \isa{mirror} to elements
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1842
  of \isa{l}. Before we discuss where this knowledge comes from,
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1843
  let us finish the termination proof:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1844
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1845
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1846
\ \ \isacommand{show}\isamarkupfalse%
25278
3026df96941d changed "treemap" example to "mirror"
krauss
parents: 25182
diff changeset
  1847
\ {\isachardoublequoteopen}wf\ {\isacharparenleft}measure\ size{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1848
\ simp\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1849
\isacommand{next}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1850
\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1851
\ \ \isacommand{fix}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1852
\ f\ l\ \isakeyword{and}\ x\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ tree{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1853
\ \ \isacommand{assume}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1854
\ {\isachardoublequoteopen}x\ {\isasymin}\ set\ l{\isachardoublequoteclose}\isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1855
\ \ \isacommand{thus}\isamarkupfalse%
25278
3026df96941d changed "treemap" example to "mirror"
krauss
parents: 25182
diff changeset
  1856
\ {\isachardoublequoteopen}{\isacharparenleft}x{\isacharcomma}\ Branch\ l{\isacharparenright}\ {\isasymin}\ measure\ size{\isachardoublequoteclose}\isanewline
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1857
\ \ \ \ \isacommand{apply}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1858
\ simp%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1859
\begin{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1860
Simplification returns the following subgoal: 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1861
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1862
      \begin{isabelle}%
25731
b3e415b0cf5c updated;
wenzelm
parents: 25278
diff changeset
  1863
{\isadigit{1}}{\isachardot}\ x\ {\isasymin}\ set\ l\ {\isasymLongrightarrow}\ size\ x\ {\isacharless}\ Suc\ {\isacharparenleft}list{\isacharunderscore}size\ size\ l{\isacharparenright}%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1864
\end{isabelle} 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1865
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1866
      We are lacking a property about the function \isa{tree{\isacharunderscore}list{\isacharunderscore}size}, which was generated automatically at the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1867
      definition of the \isa{tree} type. We should go back and prove
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1868
      it, by induction.%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1869
\end{isamarkuptxt}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1870
\isamarkuptrue%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1871
\ \ \ \ %
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1872
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1873
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1874
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1875
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1876
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1877
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1878
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1879
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1880
Now the whole termination proof is automatic:%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1881
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1882
\isamarkuptrue%
25278
3026df96941d changed "treemap" example to "mirror"
krauss
parents: 25182
diff changeset
  1883
\isacommand{termination}\isamarkupfalse%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1884
\ \isanewline
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1885
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1886
\isadelimproof
25278
3026df96941d changed "treemap" example to "mirror"
krauss
parents: 25182
diff changeset
  1887
\ \ %
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1888
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1889
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1890
\isatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1891
\isacommand{by}\isamarkupfalse%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1892
\ lexicographic{\isacharunderscore}order%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1893
\endisatagproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1894
{\isafoldproof}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1895
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1896
\isadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1897
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1898
\endisadelimproof
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1899
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1900
\isamarkupsubsection{Congruence Rules%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1901
}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1902
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1903
%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1904
\begin{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1905
Let's come back to the question how Isabelle knows about \isa{map}.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1906
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1907
  The knowledge about map is encoded in so-called congruence rules,
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1908
  which are special theorems known to the \cmd{function} command. The
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1909
  rule for map is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1910
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1911
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1912
{\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
  1913
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1914
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1915
  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
  1916
  coincide on the elements of the list. This means that for the value 
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1917
  \isa{map\ f\ l} we only have to know how \isa{f} behaves on
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1918
  \isa{l}.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1919
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1920
  Usually, one such congruence rule is
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1921
  needed for each higher-order construct that is used when defining
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1922
  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
  1923
  rule for \isa{If} states that the \isa{then} branch is only
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1924
  relevant if the condition is true, and the \isa{else} branch only if it
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1925
  is false:
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1926
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1927
  \begin{isabelle}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1928
{\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
  1929
{\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
  1930
\end{isabelle}
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1931
  
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1932
  Congruence rules can be added to the
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1933
  function package by giving them the \isa{fundef{\isacharunderscore}cong} attribute.
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1934
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1935
  The constructs that are predefined in Isabelle, usually
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1936
  come with the respective congruence rules.
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1937
  But if you define your own higher-order functions, you will have to
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1938
  come up with the congruence rules yourself, if you want to use your
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1939
  functions in recursive definitions.%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1940
\end{isamarkuptext}%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1941
\isamarkuptrue%
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1942
%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1943
\isamarkupsubsection{Congruence Rules and Evaluation Order%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1944
}
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1945
\isamarkuptrue%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1946
%
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1947
\begin{isamarkuptext}%
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1948
Higher order logic differs from functional programming languages in
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1949
  that it has no built-in notion of evaluation order. A program is
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1950
  just a set of equations, and it is not specified how they must be
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1951
  evaluated. 
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1952
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1953
  However for the purpose of function definition, we must talk about
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1954
  evaluation order implicitly, when we reason about termination.
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1955
  Congruence rules express that a certain evaluation order is
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1956
  consistent with the logical definition. 
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1957
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1958
  Consider the following function.%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1959
\end{isamarkuptext}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1960
\isamarkuptrue%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1961
\isacommand{function}\isamarkupfalse%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1962
\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1963
\isakeyword{where}\isanewline
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1964
\ \ {\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
  1965
\isadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1966
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1967
\endisadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1968
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1969
\isatagproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1970
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1971
\endisatagproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1972
{\isafoldproof}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1973
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1974
\isadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1975
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1976
\endisadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1977
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1978
\begin{isamarkuptext}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1979
As given above, the definition fails. The default configuration
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1980
  specifies no congruence rule for disjunction. We have to add a
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1981
  congruence rule that specifies left-to-right evaluation order:
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1982
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1983
  \vspace{1ex}
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1984
  \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
  1985
  \vspace{1ex}
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1986
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1987
  Now the definition works without problems. Note how the termination
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1988
  proof depends on the extra condition that we get from the congruence
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1989
  rule.
23003
4b0bf04a4d68 updated
krauss
parents: 22065
diff changeset
  1990
23805
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1991
  However, as evaluation is not a hard-wired concept, we
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1992
  could just turn everything around by declaring a different
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1993
  congruence rule. Then we can make the reverse definition:%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1994
\end{isamarkuptext}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1995
\isamarkuptrue%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1996
\isacommand{lemma}\isamarkupfalse%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1997
\ disj{\isacharunderscore}cong{\isadigit{2}}{\isacharbrackleft}fundef{\isacharunderscore}cong{\isacharbrackright}{\isacharcolon}\ \isanewline
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  1998
\ \ {\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
  1999
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2000
\isadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2001
\ \ %
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2002
\endisadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2003
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2004
\isatagproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2005
\isacommand{by}\isamarkupfalse%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2006
\ blast%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2007
\endisatagproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2008
{\isafoldproof}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2009
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2010
\isadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2011
\isanewline
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2012
%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2013
\endisadelimproof
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2014
\isanewline
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2015
\isacommand{fun}\isamarkupfalse%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2016
\ f{\isacharprime}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2017
\isakeyword{where}\isanewline
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2018
\ \ {\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
  2019
\begin{isamarkuptext}%
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2020
\noindent These examples show that, in general, there is no \qt{best} set of
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2021
  congruence rules.
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2022
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2023
  However, such tweaking should rarely be necessary in
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2024
  practice, as most of the time, the default set of congruence rules
953eb3c5f793 updated
krauss
parents: 23188
diff changeset
  2025
  works well.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2026
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2027
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2028
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2029
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2030
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2031
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2032
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2033
\isatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2034
\isacommand{end}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2035
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2036
\endisatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2037
{\isafoldtheory}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2038
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2039
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2040
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2041
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2042
\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2043
\end{isabellebody}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2044
%%% Local Variables:
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2045
%%% mode: latex
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2046
%%% TeX-master: "root"
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
  2047
%%% End: