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