doc-src/IsarAdvanced/Functions/Thy/document/Functions.tex
author krauss
Mon, 15 Jan 2007 10:15:55 +0100
changeset 22065 cdd077905eee
parent 21346 c8aa120fa05d
child 23003 4b0bf04a4d68
permissions -rw-r--r--
added sections on mutual induction and patterns
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
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    23
\isamarkupchapter{Defining Recursive Functions in Isabelle/HOL%
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
\isamarkupsection{Function Definition for Dummies%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    28
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    29
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    30
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    31
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    32
In most cases, defining a recursive function is just as simple as other definitions:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    33
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    34
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    35
\isacommand{fun}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    36
\ fib\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    37
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    38
\ \ {\isachardoublequoteopen}fib\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    39
{\isacharbar}\ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
21346
c8aa120fa05d updated
krauss
parents: 21212
diff changeset
    40
{\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
    41
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    42
The function always terminates, since the argument of gets smaller in every
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    43
  recursive call. Termination is an
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    44
  important requirement, since it prevents inconsistencies: From
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    45
  the "definition" \isa{f{\isacharparenleft}n{\isacharparenright}\ {\isacharequal}\ f{\isacharparenleft}n{\isacharparenright}\ {\isacharplus}\ {\isadigit{1}}} we could prove 
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    46
  \isa{{\isadigit{0}}\ \ {\isacharequal}\ {\isadigit{1}}} by subtracting \isa{f{\isacharparenleft}n{\isacharparenright}} on both sides.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    47
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    48
  Isabelle tries to prove termination automatically when a function is
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    49
  defined. We will later look at cases where this fails and see what to
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    50
  do then.%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    51
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    52
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    53
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    54
\isamarkupsubsection{Pattern matching%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    55
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    56
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    57
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    58
\begin{isamarkuptext}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    59
\label{patmatch}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    60
  Like in functional programming, functions can be defined by pattern
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    61
  matching. At the moment we will only consider \emph{datatype
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    62
  patterns}, which only consist of datatype constructors and
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    63
  variables.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    64
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    65
  If patterns overlap, the order of the equations is taken into
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    66
  account. The following function inserts a fixed element between any
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    67
  two elements of a list:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    68
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    69
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    70
\isacommand{fun}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    71
\ 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
    72
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    73
\ \ {\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
    74
{\isacharbar}\ {\isachardoublequoteopen}sep\ a\ xs\ \ \ \ \ \ \ {\isacharequal}\ xs{\isachardoublequoteclose}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    75
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    76
Overlapping patterns are interpreted as "increments" to what is
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    77
  already there: The second equation is only meant for the cases where
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    78
  the first one does not match. Consequently, Isabelle replaces it
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    79
  internally by the remaining cases, making the patterns disjoint:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    80
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    81
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    82
\isacommand{thm}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    83
\ sep{\isachardot}simps%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    84
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    85
\begin{isabelle}%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    86
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
    87
sep\ a\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\isasep\isanewline%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    88
sep\ a\ {\isacharbrackleft}v{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}v{\isacharbrackright}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    89
\end{isabelle}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    90
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    91
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    92
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    93
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    94
The equations from function definitions are automatically used in
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    95
  simplification:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    96
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    97
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
    98
\isacommand{lemma}\isamarkupfalse%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
    99
\ {\isachardoublequoteopen}sep\ {\isacharparenleft}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}\ {\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
   100
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   101
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   102
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   103
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   104
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   105
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   106
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   107
\ simp%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   108
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   109
{\isafoldproof}%
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
\isadelimproof
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
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   114
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   115
\isamarkupsubsection{Induction%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   116
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   117
\isamarkuptrue%
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
\begin{isamarkuptext}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   120
Isabelle provides customized induction rules for recursive functions.  
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   121
  See \cite[\S3.5.4]{isa-tutorial}.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   122
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   123
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   124
%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   125
\isamarkupsection{Full form definitions%
21212
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
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   128
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   129
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   130
Up to now, we were using the \cmd{fun} command, which provides a
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   131
  convenient shorthand notation for simple function definitions. In
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   132
  this mode, Isabelle tries to solve all the necessary proof obligations
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   133
  automatically. If a proof does not go through, the definition is
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   134
  rejected. This can either mean that the definition is indeed faulty,
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   135
  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
   136
  rather: not designed) to handle the definition.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   137
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   138
  By expanding the abbreviated \cmd{fun} to the full \cmd{function}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   139
  command, the proof obligations become visible and can be analyzed or
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   140
  solved manually.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   141
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   142
\end{isamarkuptext}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   143
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   144
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   145
\fbox{\parbox{\textwidth}{
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   146
\noindent\cmd{fun} \isa{f\ {\isacharcolon}{\isacharcolon}\ {\isasymtau}}\\%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   147
\cmd{where}\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   148
\ \ {\it equations}\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   149
\ \ \quad\vdots
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   150
}}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   151
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   152
\begin{isamarkuptext}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   153
\vspace*{1em}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   154
\noindent abbreviates
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   155
\end{isamarkuptext}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   156
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   157
\fbox{\parbox{\textwidth}{
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   158
\noindent\cmd{function} \isa{{\isacharparenleft}}\cmd{sequential}\isa{{\isacharparenright}\ f\ {\isacharcolon}{\isacharcolon}\ {\isasymtau}}\\%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   159
\cmd{where}\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   160
\ \ {\it equations}\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   161
\ \ \quad\vdots\\%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   162
\cmd{by} \isa{pat{\isacharunderscore}completeness\ auto}\\%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   163
\cmd{termination by} \isa{lexicographic{\isacharunderscore}order}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   164
}}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   165
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   166
\begin{isamarkuptext}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   167
  \vspace*{1em}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   168
  \noindent Some declarations and proofs have now become explicit:
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   169
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   170
  \begin{enumerate}
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   171
  \item The \cmd{sequential} option enables the preprocessing of
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   172
  pattern overlaps we already saw. Without this option, the equations
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   173
  must already be disjoint and complete. The automatic completion only
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   174
  works with datatype patterns.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   175
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   176
  \item A function definition now produces a proof obligation which
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   177
  expresses completeness and compatibility of patterns (We talk about
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   178
  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
   179
  \isa{auto} is used to solve this proof obligation.
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   180
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   181
  \item A termination proof follows the definition, started by the
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   182
  \cmd{termination} command, which sets up the goal. The \isa{lexicographic{\isacharunderscore}order} method can prove termination of a certain
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   183
  class of functions by searching for a suitable lexicographic
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   184
  combination of size measures.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   185
 \end{enumerate}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   186
  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
   187
  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
   188
  what is actually going on.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   189
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   190
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   191
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   192
\isamarkupsection{Proving termination%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   193
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   194
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   195
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   196
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   197
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
   198
  \isa{N}, using a counter \isa{i}:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   199
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   200
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   201
\isacommand{function}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   202
\ sum\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   203
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   204
\ \ {\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
   205
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   206
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   207
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   208
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   209
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   210
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   211
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   212
\ pat{\isacharunderscore}completeness\ auto%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   213
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   214
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   215
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   216
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   217
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   218
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   219
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   220
\begin{isamarkuptext}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   221
\noindent The \isa{lexicographic{\isacharunderscore}order} method fails on this example, because none of the
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   222
  arguments decreases in the recursive call.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   223
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   224
  A more general method for termination proofs is to supply a wellfounded
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   225
  relation on the argument type, and to show that the argument
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   226
  decreases in every recursive call. 
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   227
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   228
  The termination argument for \isa{sum} is based on the fact that
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   229
  the \emph{difference} between \isa{i} and \isa{N} gets
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   230
  smaller in every step, and that the recursion stops when \isa{i}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   231
  is greater then \isa{n}. Phrased differently, the expression 
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   232
  \isa{N\ {\isacharplus}\ {\isadigit{1}}\ {\isacharminus}\ i} decreases in every recursive call.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   233
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   234
  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
   235
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   236
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   237
\isacommand{termination}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   238
\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   239
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   240
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   241
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   242
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   243
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   244
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   245
\isacommand{by}\isamarkupfalse%
21346
c8aa120fa05d updated
krauss
parents: 21212
diff changeset
   246
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}i{\isacharcomma}N{\isacharparenright}{\isachardot}\ N\ {\isacharplus}\ {\isadigit{1}}\ {\isacharminus}\ i{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\ auto%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   247
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   248
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   249
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   250
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   251
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   252
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   253
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   254
\begin{isamarkuptext}%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   255
The \isa{relation} method takes a relation of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   256
  type \isa{{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ set}, where \isa{{\isacharprime}a} is the argument type of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   257
  the function. If the function has multiple curried arguments, then
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   258
  these are packed together into a tuple, as it happened in the above
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   259
  example.
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   260
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   261
  The predefined function \isa{measure{\isasymColon}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ set} is a very common way of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   262
  specifying termination relations in terms of a mapping into the
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   263
  natural numbers.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   264
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   265
  After the invocation of \isa{relation}, we must prove that (a)
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   266
  the relation we supplied is wellfounded, and (b) that the arguments
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   267
  of recursive calls indeed decrease with respect to the
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   268
  relation. These goals are all solved by the subsequent call to
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   269
  \isa{auto}.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   270
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   271
  Let us complicate the function a little, by adding some more
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   272
  recursive calls:%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   273
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   274
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   275
\isacommand{function}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   276
\ foo\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   277
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   278
\ \ {\isachardoublequoteopen}foo\ i\ N\ {\isacharequal}\ {\isacharparenleft}if\ i\ {\isachargreater}\ N\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   279
\ \ \ \ \ \ \ \ \ \ \ \ \ \ 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
   280
\ \ \ \ \ \ \ \ \ \ \ \ \ \ else\ i\ {\isacharplus}\ foo\ {\isacharparenleft}Suc\ i{\isacharparenright}\ N{\isacharparenright}{\isachardoublequoteclose}\isanewline
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
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   287
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   288
\ pat{\isacharunderscore}completeness\ auto%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   289
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   290
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   291
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   292
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   293
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   294
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   295
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   296
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   297
When \isa{i} has reached \isa{N}, it starts at zero again
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   298
  and \isa{N} is decremented.
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   299
  This corresponds to a nested
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   300
  loop where one index counts up and the other down. Termination can
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   301
  be proved using a lexicographic combination of two measures, namely
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   302
  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
   303
  list of measure functions.%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   304
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   305
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   306
\isacommand{termination}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   307
\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   308
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   309
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   310
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   311
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   312
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   313
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   314
\isacommand{by}\isamarkupfalse%
21346
c8aa120fa05d updated
krauss
parents: 21212
diff changeset
   315
\ {\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
   316
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   317
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   318
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   319
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   320
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   321
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   322
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   323
\isamarkupsection{Mutual Recursion%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   324
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   325
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   326
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   327
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   328
If two or more functions call one another mutually, they have to be defined
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   329
  in one step. The simplest example are probably \isa{even} and \isa{odd}:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   330
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   331
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   332
\isacommand{function}\isamarkupfalse%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   333
\ even\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   334
\ \ \ \ \isakeyword{and}\ odd\ \ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   335
\isakeyword{where}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   336
\ \ {\isachardoublequoteopen}even\ {\isadigit{0}}\ {\isacharequal}\ True{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   337
{\isacharbar}\ {\isachardoublequoteopen}odd\ {\isadigit{0}}\ {\isacharequal}\ False{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   338
{\isacharbar}\ {\isachardoublequoteopen}even\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ odd\ n{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   339
{\isacharbar}\ {\isachardoublequoteopen}odd\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ even\ n{\isachardoublequoteclose}\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   340
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   341
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   342
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   343
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   344
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   345
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   346
\isacommand{by}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   347
\ pat{\isacharunderscore}completeness\ auto%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   348
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   349
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   350
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   351
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   352
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   353
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   354
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   355
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   356
To solve the problem of mutual dependencies, Isabelle internally
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   357
  creates a single function operating on the sum
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   358
  type. Then the original functions are defined as
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   359
  projections. Consequently, termination has to be proved
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   360
  simultaneously for both functions, by specifying a measure on the
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   361
  sum type:%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   362
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   363
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   364
\isacommand{termination}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   365
\ \isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   366
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   367
\isadelimproof
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   368
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   369
\endisadelimproof
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
\isatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   372
\isacommand{by}\isamarkupfalse%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   373
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ case\ x\ of\ Inl\ n\ {\isasymRightarrow}\ n\ {\isacharbar}\ Inr\ n\ {\isasymRightarrow}\ n{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\ \isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   374
\ \ \ auto%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   375
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   376
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   377
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   378
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   379
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   380
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   381
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   382
\isamarkupsubsection{Induction for mutual recursion%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   383
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   384
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   385
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   386
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   387
When functions are mutually recursive, proving properties about them
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   388
  generally requires simultaneous induction. The induction rules
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   389
  generated from the definitions reflect this.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   390
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   391
  Let us prove something about \isa{even} and \isa{odd}:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   392
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   393
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   394
\isacommand{lemma}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   395
\ \isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   396
\ \ {\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
   397
\ \ {\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
   398
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   399
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   400
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   401
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   402
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   403
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   404
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   405
We apply simultaneous induction, specifying the induction variable
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   406
  for both goals, separated by \cmd{and}:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   407
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   408
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   409
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   410
\ {\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
   411
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   412
We get four subgoals, which correspond to the clauses in the
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   413
  definition of \isa{even} and \isa{odd}:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   414
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   415
\ {\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
   416
\ {\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
   417
\ {\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
   418
\ {\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
   419
\end{isabelle}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   420
  Simplification solves the first two goals, leaving us with two
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   421
  statements about the \isa{mod} operation to prove:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   422
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   423
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   424
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   425
\ simp{\isacharunderscore}all%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   426
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   427
\begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   428
\ {\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
   429
\ {\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
   430
\end{isabelle} 
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   431
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   432
  \noindent These can be handeled by the descision procedure for
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   433
  presburger arithmethic.%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   434
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   435
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   436
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   437
\ presburger\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   438
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   439
\ presburger\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   440
\isacommand{done}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   441
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   442
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   443
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   444
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   445
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   446
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   447
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   448
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   449
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   450
Even if we were just interested in one of the statements proved by
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   451
  simultaneous induction, the other ones may be necessary to
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   452
  strengthen the induction hypothesis. If we had left out the statement
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   453
  about \isa{odd} (by substituting it with \isa{True}, our
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   454
  proof would have failed:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   455
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   456
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   457
\isacommand{lemma}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   458
\ \isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   459
\ \ {\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
   460
\ \ {\isachardoublequoteopen}True{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   461
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   462
\isadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   463
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   464
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   465
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   466
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   467
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   468
\ {\isacharparenleft}induct\ n\ rule{\isacharcolon}\ even{\isacharunderscore}odd{\isachardot}induct{\isacharparenright}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   469
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   470
\noindent Now the third subgoal is a dead end, since we have no
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   471
  useful induction hypothesis:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   472
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   473
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   474
\ {\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
   475
\ {\isadigit{2}}{\isachardot}\ True\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   476
\ {\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
   477
\ {\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
   478
\end{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   479
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   480
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   481
\isacommand{oops}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   482
%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   483
\endisatagproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   484
{\isafoldproof}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   485
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   486
\isadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   487
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   488
\endisadelimproof
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   489
%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   490
\isamarkupsection{More general patterns%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   491
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   492
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   493
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   494
\isamarkupsubsection{Avoiding pattern splitting%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   495
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   496
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   497
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   498
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   499
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
   500
  patterns were always disjoint and complete, and if they weren't,
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   501
  they were made disjoint automatically like in the definition of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   502
  \isa{sep} in \S\ref{patmatch}.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   503
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   504
  This splitting can significantly increase the number of equations
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   505
  involved, and is not always necessary. The following simple example
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   506
  shows the problem:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   507
  
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   508
  Suppose we are modelling incomplete knowledge about the world by a
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   509
  three-valued datatype, which has values for \isa{T}, \isa{F}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   510
  and \isa{X} for true, false and uncertain propositions.%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   511
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   512
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   513
\isacommand{datatype}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   514
\ P{\isadigit{3}}\ {\isacharequal}\ T\ {\isacharbar}\ F\ {\isacharbar}\ X%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   515
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   516
Then the conjunction of such values can be defined as follows:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   517
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   518
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   519
\isacommand{fun}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   520
\ 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
   521
\isakeyword{where}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   522
\ \ {\isachardoublequoteopen}And\ T\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   523
\ \ {\isachardoublequoteopen}And\ p\ T\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   524
\ \ {\isachardoublequoteopen}And\ p\ F\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   525
\ \ {\isachardoublequoteopen}And\ F\ p\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   526
\ \ {\isachardoublequoteopen}And\ X\ X\ {\isacharequal}\ X{\isachardoublequoteclose}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   527
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   528
This definition is useful, because the equations can directly be used
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   529
  as rules to simplify expressions. But the patterns overlap, e.g.~the
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   530
  expression \isa{And\ T\ T} is matched by the first two
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   531
  equations. By default, Isabelle makes the patterns disjoint by
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   532
  splitting them up, producing instances:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   533
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   534
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   535
\isacommand{thm}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   536
\ And{\isachardot}simps%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   537
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   538
\isa{And\ T\ {\isacharquery}p\ {\isacharequal}\ {\isacharquery}p\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   539
And\ F\ T\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   540
And\ X\ T\ {\isacharequal}\ X\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   541
And\ F\ F\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   542
And\ X\ F\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   543
And\ F\ X\ {\isacharequal}\ F\isasep\isanewline%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   544
And\ X\ X\ {\isacharequal}\ X}
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
  \vspace*{1em}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   547
  \noindent There are several problems with this approach:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   548
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   549
  \begin{enumerate}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   550
  \item When datatypes have many constructors, there can be an
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   551
  explosion of equations. For \isa{And}, we get seven instead of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   552
  five equation, which can be tolerated, but this is just a small
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   553
  example.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   554
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   555
  \item Since splitting makes the equations "more special", they
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   556
  do not always match in rewriting. While the term \isa{And\ x\ F}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   557
  can be simplified to \isa{F} by the original specification, a
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   558
  (manual) case split on \isa{x} is now necessary.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   559
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   560
  \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
   561
  means that our induction proofs will have more cases.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   562
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   563
  \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
   564
  back which we put in.
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   565
  \end{enumerate}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   566
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   567
  On the other hand, a definition needs to be consistent and defining
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   568
  both \isa{f\ x\ {\isacharequal}\ True} and \isa{f\ x\ {\isacharequal}\ False} is a bad
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   569
  idea. So if we don't want Isabelle to mangle our definitions, we
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   570
  will have to prove that this is not necessary. By using the full
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   571
  definition form withour the \cmd{sequential} option, we get this
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   572
  behaviour:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   573
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   574
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   575
\isacommand{function}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   576
\ 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
   577
\isakeyword{where}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   578
\ \ {\isachardoublequoteopen}And{\isadigit{2}}\ T\ p\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   579
\ \ {\isachardoublequoteopen}And{\isadigit{2}}\ p\ T\ {\isacharequal}\ p{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   580
\ \ {\isachardoublequoteopen}And{\isadigit{2}}\ p\ F\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   581
\ \ {\isachardoublequoteopen}And{\isadigit{2}}\ F\ p\ {\isacharequal}\ F{\isachardoublequoteclose}\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   582
\ \ {\isachardoublequoteopen}And{\isadigit{2}}\ X\ X\ {\isacharequal}\ X{\isachardoublequoteclose}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   583
\isadelimproof
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
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   586
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   587
\isatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   588
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   589
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   590
Now it is also time to look at the subgoals generated by a
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   591
  function definition. In this case, they are:
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   592
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   593
  \begin{isabelle}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   594
\ {\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
   595
\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
   596
\isaindent{\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}P\ x{\isachardot}\ }{\isasymLongrightarrow}\ P\isanewline
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   597
\ {\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
   598
\ {\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
   599
\ {\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
   600
\ {\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
   601
\ {\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
   602
\ {\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
   603
\ {\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
   604
\ {\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
   605
\ {\isadigit{1}}{\isadigit{0}}{\isachardot}\ {\isasymAnd}p{\isachardot}\ {\isacharparenleft}p{\isacharcomma}\ T{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}X{\isacharcomma}\ X{\isacharparenright}\ {\isasymLongrightarrow}\ p\ {\isacharequal}\ X%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   606
\end{isabelle} 
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
  The first subgoal expresses the completeness of the patterns. It has
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   609
  the form of an elimination rule and states that every \isa{x} of
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   610
  the function's input type must match one of the patterns. It could
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   611
  be equivalently stated as a disjunction of existential statements: 
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   612
\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
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   613
  datatypes, we can solve it with the \isa{pat{\isacharunderscore}completeness} method:%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   614
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   615
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   616
\isacommand{apply}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   617
\ pat{\isacharunderscore}completeness%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   618
\begin{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   619
The remaining subgoals express \emph{pattern compatibility}. We do
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   620
  allow that a value is matched by more than one patterns, but in this
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   621
  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
   622
  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
   623
  subgoal. Usually this needs injectivity of the constructors, which
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   624
  is used automatically by \isa{auto}.%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   625
\end{isamarkuptxt}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   626
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   627
\isacommand{by}\isamarkupfalse%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   628
\ auto%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   629
\endisatagproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   630
{\isafoldproof}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   631
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   632
\isadelimproof
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
\endisadelimproof
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   635
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   636
\isamarkupsubsection{Non-constructor patterns%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   637
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   638
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   639
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   640
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   641
FIXME%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   642
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   643
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   644
%
22065
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   645
\isamarkupsubsection{Non-constructor patterns%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   646
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   647
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   648
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   649
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   650
FIXME%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   651
\end{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   652
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   653
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   654
\isamarkupsection{Partiality%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   655
}
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   656
\isamarkuptrue%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   657
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   658
\begin{isamarkuptext}%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   659
In HOL, all functions are total. A function \isa{f} applied to
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   660
  \isa{x} always has a value \isa{f\ x}, and there is no notion
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   661
  of undefinedness. 
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   662
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   663
  FIXME%
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
%
cdd077905eee added sections on mutual induction and patterns
krauss
parents: 21346
diff changeset
   667
\isamarkupsection{Nested recursion%
21212
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   668
}
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   669
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   670
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   671
\begin{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   672
FIXME%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   673
\end{isamarkuptext}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   674
\isamarkuptrue%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   675
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   676
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   677
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   678
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   679
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   680
\isatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   681
\isacommand{end}\isamarkupfalse%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   682
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   683
\endisatagtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   684
{\isafoldtheory}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   685
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   686
\isadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   687
%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   688
\endisadelimtheory
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   689
\isanewline
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   690
\end{isabellebody}%
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   691
%%% Local Variables:
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   692
%%% mode: latex
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   693
%%% TeX-master: "root"
547224bf9348 Added a (stub of a) function tutorial
krauss
parents:
diff changeset
   694
%%% End: