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