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