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