5375
|
1 |
\chapter{Functional Programming in HOL}
|
|
2 |
|
|
3 |
Although on the surface this chapter is mainly concerned with how to write
|
|
4 |
functional programs in HOL and how to verify them, most of the
|
|
5 |
constructs and proof procedures introduced are general purpose and recur in
|
|
6 |
any specification or verification task.
|
|
7 |
|
9255
|
8 |
The dedicated functional programmer should be warned: HOL offers only {\em
|
|
9 |
total functional programming} --- all functions in HOL must be total; lazy
|
|
10 |
data structures are not directly available. On the positive side, functions
|
|
11 |
in HOL need not be computable: HOL is a specification language that goes well
|
|
12 |
beyond what can be expressed as a program. However, for the time being we
|
|
13 |
concentrate on the computable.
|
5375
|
14 |
|
|
15 |
\section{An introductory theory}
|
|
16 |
\label{sec:intro-theory}
|
|
17 |
|
|
18 |
Functional programming needs datatypes and functions. Both of them can be
|
|
19 |
defined in a theory with a syntax reminiscent of languages like ML or
|
|
20 |
Haskell. As an example consider the theory in Fig.~\ref{fig:ToyList}.
|
|
21 |
|
|
22 |
\begin{figure}[htbp]
|
|
23 |
\begin{ttbox}\makeatother
|
|
24 |
\input{ToyList/ToyList.thy}\end{ttbox}
|
|
25 |
\caption{A theory of lists}
|
|
26 |
\label{fig:ToyList}
|
|
27 |
\end{figure}
|
|
28 |
|
|
29 |
HOL already has a predefined theory of lists called \texttt{List} ---
|
|
30 |
\texttt{ToyList} is merely a small fragment of it chosen as an example. In
|
|
31 |
contrast to what is recommended in \S\ref{sec:Basic:Theories},
|
|
32 |
\texttt{ToyList} is not based on \texttt{Main} but on \texttt{Datatype}, a
|
|
33 |
theory that contains everything required for datatype definitions but does
|
|
34 |
not have \texttt{List} as a parent, thus avoiding ambiguities caused by
|
|
35 |
defining lists twice.
|
|
36 |
|
|
37 |
The \ttindexbold{datatype} \texttt{list} introduces two constructors
|
|
38 |
\texttt{Nil} and \texttt{Cons}, the empty list and the operator that adds an
|
|
39 |
element to the front of a list. For example, the term \texttt{Cons True (Cons
|
|
40 |
False Nil)} is a value of type \texttt{bool~list}, namely the list with the
|
|
41 |
elements \texttt{True} and \texttt{False}. Because this notation becomes
|
|
42 |
unwieldy very quickly, the datatype declaration is annotated with an
|
|
43 |
alternative syntax: instead of \texttt{Nil} and \texttt{Cons}~$x$~$xs$ we can
|
|
44 |
write \index{#@{\tt[]}|bold}\texttt{[]} and
|
|
45 |
\texttt{$x$~\#~$xs$}\index{#@{\tt\#}|bold}. In fact, this alternative syntax
|
|
46 |
is the standard syntax. Thus the list \texttt{Cons True (Cons False Nil)}
|
|
47 |
becomes \texttt{True \# False \# []}. The annotation \ttindexbold{infixr}
|
|
48 |
means that \texttt{\#} associates to the right, i.e.\ the term \texttt{$x$ \#
|
|
49 |
$y$ \# $z$} is read as \texttt{$x$ \# ($y$ \# $z$)} and not as \texttt{($x$
|
|
50 |
\# $y$) \# $z$}.
|
|
51 |
|
|
52 |
\begin{warn}
|
|
53 |
Syntax annotations are a powerful but completely optional feature. You
|
|
54 |
could drop them from theory \texttt{ToyList} and go back to the identifiers
|
|
55 |
\texttt{Nil} and \texttt{Cons}. However, lists are such a central datatype
|
|
56 |
that their syntax is highly customized. We recommend that novices should
|
|
57 |
not use syntax annotations in their own theories.
|
|
58 |
\end{warn}
|
|
59 |
|
|
60 |
Next, the functions \texttt{app} and \texttt{rev} are declared. In contrast
|
|
61 |
to ML, Isabelle insists on explicit declarations of all functions (keyword
|
|
62 |
\ttindexbold{consts}). (Apart from the declaration-before-use restriction,
|
|
63 |
the order of items in a theory file is unconstrained.) Function \texttt{app}
|
|
64 |
is annotated with concrete syntax too. Instead of the prefix syntax
|
|
65 |
\texttt{app}~$xs$~$ys$ the infix $xs$~\texttt{\at}~$ys$ becomes the preferred
|
|
66 |
form.
|
|
67 |
|
|
68 |
Both functions are defined recursively. The equations for \texttt{app} and
|
|
69 |
\texttt{rev} hardly need comments: \texttt{app} appends two lists and
|
5850
|
70 |
\texttt{rev} reverses a list. The keyword \ttindex{primrec} indicates that
|
5375
|
71 |
the recursion is of a particularly primitive kind where each recursive call
|
|
72 |
peels off a datatype constructor from one of the arguments (see
|
|
73 |
\S\ref{sec:datatype}). Thus the recursion always terminates, i.e.\ the
|
|
74 |
function is \bfindex{total}.
|
|
75 |
|
|
76 |
The termination requirement is absolutely essential in HOL, a logic of total
|
|
77 |
functions. If we were to drop it, inconsistencies could quickly arise: the
|
|
78 |
``definition'' $f(n) = f(n)+1$ immediately leads to $0 = 1$ by subtracting
|
|
79 |
$f(n)$ on both sides.
|
|
80 |
% However, this is a subtle issue that we cannot discuss here further.
|
|
81 |
|
|
82 |
\begin{warn}
|
|
83 |
As we have indicated, the desire for total functions is not a gratuitously
|
|
84 |
imposed restriction but an essential characteristic of HOL. It is only
|
|
85 |
because of totality that reasoning in HOL is comparatively easy. More
|
|
86 |
generally, the philosophy in HOL is not to allow arbitrary axioms (such as
|
|
87 |
function definitions whose totality has not been proved) because they
|
|
88 |
quickly lead to inconsistencies. Instead, fixed constructs for introducing
|
|
89 |
types and functions are offered (such as \texttt{datatype} and
|
|
90 |
\texttt{primrec}) which are guaranteed to preserve consistency.
|
|
91 |
\end{warn}
|
|
92 |
|
|
93 |
A remark about syntax. The textual definition of a theory follows a fixed
|
|
94 |
syntax with keywords like \texttt{datatype} and \texttt{end} (see
|
|
95 |
Fig.~\ref{fig:keywords} in Appendix~\ref{sec:Appendix} for a full list).
|
|
96 |
Embedded in this syntax are the types and formulae of HOL, whose syntax is
|
|
97 |
extensible, e.g.\ by new user-defined infix operators
|
|
98 |
(see~\ref{sec:infix-syntax}). To distinguish the two levels, everything
|
|
99 |
HOL-specific should be enclosed in \texttt{"}\dots\texttt{"}. The same holds
|
|
100 |
for identifiers that happen to be keywords, as in
|
|
101 |
\begin{ttbox}
|
|
102 |
consts "end" :: 'a list => 'a
|
|
103 |
\end{ttbox}
|
|
104 |
To lessen this burden, quotation marks around types can be dropped,
|
|
105 |
provided their syntax does not go beyond what is described in
|
|
106 |
\S\ref{sec:TypesTermsForms}. Types containing further operators, e.g.\
|
5850
|
107 |
\label{startype} \texttt{*} for Cartesian products, need quotation marks.
|
5375
|
108 |
|
|
109 |
When Isabelle prints a syntax error message, it refers to the HOL syntax as
|
|
110 |
the \bfindex{inner syntax}.
|
|
111 |
|
|
112 |
\section{An introductory proof}
|
|
113 |
\label{sec:intro-proof}
|
|
114 |
|
|
115 |
Having defined \texttt{ToyList}, we load it with the ML command
|
|
116 |
\begin{ttbox}
|
|
117 |
use_thy "ToyList";
|
|
118 |
\end{ttbox}
|
|
119 |
and are ready to prove a few simple theorems. This will illustrate not just
|
|
120 |
the basic proof commands but also the typical proof process.
|
|
121 |
|
|
122 |
\subsubsection*{Main goal: \texttt{rev(rev xs) = xs}}
|
|
123 |
|
|
124 |
Our goal is to show that reversing a list twice produces the original
|
|
125 |
list. Typing
|
|
126 |
\begin{ttbox}
|
|
127 |
\input{ToyList/thm}\end{ttbox}
|
|
128 |
establishes a new goal to be proved in the context of the current theory,
|
|
129 |
which is the one we just loaded. Isabelle's response is to print the current proof state:
|
|
130 |
\begin{ttbox}
|
9255
|
131 |
\Out{Level 0}
|
|
132 |
\Out{rev (rev xs) = xs}
|
|
133 |
\Out{ 1. rev (rev xs) = xs}
|
5375
|
134 |
\end{ttbox}
|
|
135 |
Until we have finished a proof, the proof state always looks like this:
|
|
136 |
\begin{ttbox}
|
9255
|
137 |
\Out{Level \(i\)}
|
|
138 |
\Out{\(G\)}
|
|
139 |
\Out{ 1. \(G@1\)}
|
|
140 |
\Out{ \(\vdots\)}
|
|
141 |
\Out{ \(n\). \(G@n\)}
|
5375
|
142 |
\end{ttbox}
|
|
143 |
where \texttt{Level}~$i$ indicates that we are $i$ steps into the proof, $G$
|
|
144 |
is the overall goal that we are trying to prove, and the numbered lines
|
|
145 |
contain the subgoals $G@1$, \dots, $G@n$ that we need to prove to establish
|
|
146 |
$G$. At \texttt{Level 0} there is only one subgoal, which is identical with
|
|
147 |
the overall goal. Normally $G$ is constant and only serves as a reminder.
|
|
148 |
Hence we rarely show it in this tutorial.
|
|
149 |
|
|
150 |
Let us now get back to \texttt{rev(rev xs) = xs}. Properties of recursively
|
|
151 |
defined functions are best established by induction. In this case there is
|
|
152 |
not much choice except to induct on \texttt{xs}:
|
|
153 |
\begin{ttbox}
|
|
154 |
\input{ToyList/inductxs}\end{ttbox}
|
|
155 |
This tells Isabelle to perform induction on variable \texttt{xs} in subgoal
|
|
156 |
1. The new proof state contains two subgoals, namely the base case
|
|
157 |
(\texttt{Nil}) and the induction step (\texttt{Cons}):
|
|
158 |
\begin{ttbox}
|
9255
|
159 |
\Out{1. rev (rev []) = []}
|
|
160 |
\Out{2. !!a list. rev (rev list) = list ==> rev (rev (a # list)) = a # list}
|
5375
|
161 |
\end{ttbox}
|
|
162 |
The induction step is an example of the general format of a subgoal:
|
|
163 |
\begin{ttbox}
|
9255
|
164 |
\Out{\(i\). !!\(x@1 \dots x@n\). {\it assumptions} ==> {\it conclusion}}
|
5375
|
165 |
\end{ttbox}\index{==>@{\tt==>}|bold}
|
|
166 |
The prefix of bound variables \texttt{!!\(x@1 \dots x@n\)} can be ignored
|
|
167 |
most of the time, or simply treated as a list of variables local to this
|
|
168 |
subgoal. Their deeper significance is explained in \S\ref{sec:PCproofs}. The
|
|
169 |
{\it assumptions} are the local assumptions for this subgoal and {\it
|
|
170 |
conclusion} is the actual proposition to be proved. Typical proof steps
|
|
171 |
that add new assumptions are induction or case distinction. In our example
|
|
172 |
the only assumption is the induction hypothesis \texttt{rev (rev list) =
|
|
173 |
list}, where \texttt{list} is a variable name chosen by Isabelle. If there
|
|
174 |
are multiple assumptions, they are enclosed in the bracket pair
|
|
175 |
\texttt{[|}\index{==>@\ttlbr|bold} and \texttt{|]}\index{==>@\ttrbr|bold}
|
|
176 |
and separated by semicolons.
|
|
177 |
|
|
178 |
Let us try to solve both goals automatically:
|
|
179 |
\begin{ttbox}
|
|
180 |
\input{ToyList/autotac}\end{ttbox}
|
|
181 |
This command tells Isabelle to apply a proof strategy called
|
|
182 |
\texttt{Auto_tac} to all subgoals. Essentially, \texttt{Auto_tac} tries to
|
|
183 |
`simplify' the subgoals. In our case, subgoal~1 is solved completely (thanks
|
|
184 |
to the equation \texttt{rev [] = []}) and disappears; the simplified version
|
|
185 |
of subgoal~2 becomes the new subgoal~1:
|
|
186 |
\begin{ttbox}\makeatother
|
9255
|
187 |
\Out{1. !!a list. rev(rev list) = list ==> rev(rev list @ a # []) = a # list}
|
5375
|
188 |
\end{ttbox}
|
|
189 |
In order to simplify this subgoal further, a lemma suggests itself.
|
|
190 |
|
|
191 |
\subsubsection*{First lemma: \texttt{rev(xs \at~ys) = (rev ys) \at~(rev xs)}}
|
|
192 |
|
|
193 |
We start the proof as usual:
|
|
194 |
\begin{ttbox}\makeatother
|
|
195 |
\input{ToyList/lemma1}\end{ttbox}
|
|
196 |
There are two variables that we could induct on: \texttt{xs} and
|
|
197 |
\texttt{ys}. Because \texttt{\at} is defined by recursion on
|
|
198 |
the first argument, \texttt{xs} is the correct one:
|
|
199 |
\begin{ttbox}
|
|
200 |
\input{ToyList/inductxs}\end{ttbox}
|
|
201 |
This time not even the base case is solved automatically:
|
|
202 |
\begin{ttbox}\makeatother
|
|
203 |
by(Auto_tac);
|
9255
|
204 |
\Out{ 1. rev ys = rev ys @ []}
|
|
205 |
\Out{ 2. \dots}
|
5375
|
206 |
\end{ttbox}
|
|
207 |
We need another lemma.
|
|
208 |
|
|
209 |
\subsubsection*{Second lemma: \texttt{xs \at~[] = xs}}
|
|
210 |
|
|
211 |
This time the canonical proof procedure
|
|
212 |
\begin{ttbox}\makeatother
|
|
213 |
\input{ToyList/lemma2}\input{ToyList/inductxs}\input{ToyList/autotac}\end{ttbox}
|
|
214 |
leads to the desired message \texttt{No subgoals!}:
|
|
215 |
\begin{ttbox}\makeatother
|
9255
|
216 |
\Out{xs @ [] = xs}
|
|
217 |
\Out{No subgoals!}
|
5375
|
218 |
\end{ttbox}
|
|
219 |
Now we can give the lemma just proved a suitable name
|
|
220 |
\begin{ttbox}
|
6691
|
221 |
\input{ToyList/qed2}\end{ttbox}\index{*qed}
|
5375
|
222 |
and tell Isabelle to use this lemma in all future proofs by simplification:
|
|
223 |
\begin{ttbox}
|
|
224 |
\input{ToyList/addsimps2}\end{ttbox}
|
|
225 |
Note that in the theorem \texttt{app_Nil2} the free variable \texttt{xs} has
|
|
226 |
been replaced by the unknown \texttt{?xs}, just as explained in
|
|
227 |
\S\ref{sec:variables}.
|
|
228 |
|
|
229 |
Going back to the proof of the first lemma
|
|
230 |
\begin{ttbox}\makeatother
|
|
231 |
\input{ToyList/lemma1}\input{ToyList/inductxs}\input{ToyList/autotac}\end{ttbox}
|
|
232 |
we find that this time \texttt{Auto_tac} solves the base case, but the
|
|
233 |
induction step merely simplifies to
|
|
234 |
\begin{ttbox}\makeatother
|
9255
|
235 |
\Out{1. !!a list.}
|
|
236 |
\Out{ rev (list @ ys) = rev ys @ rev list}
|
|
237 |
\Out{ ==> (rev ys @ rev list) @ a # [] = rev ys @ rev list @ a # []}
|
5375
|
238 |
\end{ttbox}
|
|
239 |
Now we need to remember that \texttt{\at} associates to the right, and that
|
|
240 |
\texttt{\#} and \texttt{\at} have the same priority (namely the \texttt{65}
|
|
241 |
in the definition of \texttt{ToyList}). Thus the conclusion really is
|
|
242 |
\begin{ttbox}\makeatother
|
9255
|
243 |
\Out{ ==> (rev ys @ rev list) @ (a # []) = rev ys @ (rev list @ (a # []))}
|
5375
|
244 |
\end{ttbox}
|
|
245 |
and the missing lemma is associativity of \texttt{\at}.
|
|
246 |
|
|
247 |
\subsubsection*{Third lemma: \texttt{(xs \at~ys) \at~zs = xs \at~(ys \at~zs)}}
|
|
248 |
|
|
249 |
This time the canonical proof procedure
|
|
250 |
\begin{ttbox}\makeatother
|
|
251 |
\input{ToyList/lemma3}\end{ttbox}
|
|
252 |
succeeds without further ado. Again we name the lemma and add it to
|
|
253 |
the set of lemmas used during simplification:
|
|
254 |
\begin{ttbox}
|
|
255 |
\input{ToyList/qed3}\end{ttbox}
|
|
256 |
Now we can go back and prove the first lemma
|
|
257 |
\begin{ttbox}\makeatother
|
|
258 |
\input{ToyList/lemma1}\input{ToyList/inductxs}\input{ToyList/autotac}\end{ttbox}
|
|
259 |
add it to the simplification lemmas
|
|
260 |
\begin{ttbox}
|
|
261 |
\input{ToyList/qed1}\end{ttbox}
|
|
262 |
and then solve our main theorem:
|
|
263 |
\begin{ttbox}\makeatother
|
|
264 |
\input{ToyList/thm}\input{ToyList/inductxs}\input{ToyList/autotac}\end{ttbox}
|
|
265 |
|
|
266 |
\subsubsection*{Review}
|
|
267 |
|
|
268 |
This is the end of our toy proof. It should have familiarized you with
|
|
269 |
\begin{itemize}
|
|
270 |
\item the standard theorem proving procedure:
|
|
271 |
state a goal; proceed with proof until a new lemma is required; prove that
|
|
272 |
lemma; come back to the original goal.
|
|
273 |
\item a specific procedure that works well for functional programs:
|
|
274 |
induction followed by all-out simplification via \texttt{Auto_tac}.
|
|
275 |
\item a basic repertoire of proof commands.
|
|
276 |
\end{itemize}
|
|
277 |
|
|
278 |
|
|
279 |
\section{Some helpful commands}
|
|
280 |
\label{sec:commands-and-hints}
|
|
281 |
|
|
282 |
This section discusses a few basic commands for manipulating the proof state
|
|
283 |
and can be skipped by casual readers.
|
|
284 |
|
|
285 |
There are two kinds of commands used during a proof: the actual proof
|
|
286 |
commands and auxiliary commands for examining the proof state and controlling
|
|
287 |
the display. Proof commands are always of the form
|
|
288 |
\texttt{by(\textit{tactic});}\indexbold{tactic} where \textbf{tactic} is a
|
|
289 |
synonym for ``theorem proving function''. Typical examples are
|
|
290 |
\texttt{induct_tac} and \texttt{Auto_tac} --- the suffix \texttt{_tac} is
|
|
291 |
merely a mnemonic. Further tactics are introduced throughout the tutorial.
|
|
292 |
|
|
293 |
%Tactics can also be modified. For example,
|
|
294 |
%\begin{ttbox}
|
|
295 |
%by(ALLGOALS Asm_simp_tac);
|
|
296 |
%\end{ttbox}
|
|
297 |
%tells Isabelle to apply \texttt{Asm_simp_tac} to all subgoals. For more on
|
|
298 |
%tactics and how to combine them see~\S\ref{sec:Tactics}.
|
|
299 |
|
|
300 |
The most useful auxiliary commands are:
|
|
301 |
\begin{description}
|
|
302 |
\item[Printing the current state]
|
|
303 |
Type \texttt{pr();} to redisplay the current proof state, for example when it
|
|
304 |
has disappeared off the screen.
|
|
305 |
\item[Limiting the number of subgoals]
|
|
306 |
Typing \texttt{prlim $k$;} tells Isabelle to print only the first $k$
|
|
307 |
subgoals from now on and redisplays the current proof state. This is helpful
|
|
308 |
when there are many subgoals.
|
|
309 |
\item[Undoing] Typing \texttt{undo();} undoes the effect of the last
|
|
310 |
tactic.
|
|
311 |
\item[Context switch] Every proof happens in the context of a
|
|
312 |
\bfindex{current theory}. By default, this is the last theory loaded. If
|
|
313 |
you want to prove a theorem in the context of a different theory
|
|
314 |
\texttt{T}, you need to type \texttt{context T.thy;}\index{*context|bold}
|
|
315 |
first. Of course you need to change the context again if you want to go
|
|
316 |
back to your original theory.
|
|
317 |
\item[Displaying types] We have already mentioned the flag
|
|
318 |
\ttindex{show_types} above. It can also be useful for detecting typos in
|
|
319 |
formulae early on. For example, if \texttt{show_types} is set and the goal
|
|
320 |
\texttt{rev(rev xs) = xs} is started, Isabelle prints the additional output
|
|
321 |
\begin{ttbox}
|
9255
|
322 |
\Out{Variables:}
|
|
323 |
\Out{ xs :: 'a list}
|
5375
|
324 |
\end{ttbox}
|
|
325 |
which tells us that Isabelle has correctly inferred that
|
|
326 |
\texttt{xs} is a variable of list type. On the other hand, had we
|
|
327 |
made a typo as in \texttt{rev(re xs) = xs}, the response
|
|
328 |
\begin{ttbox}
|
9255
|
329 |
\Out{Variables:}
|
|
330 |
\Out{ re :: 'a list => 'a list}
|
|
331 |
\Out{ xs :: 'a list}
|
5375
|
332 |
\end{ttbox}
|
|
333 |
would have alerted us because of the unexpected variable \texttt{re}.
|
|
334 |
\item[(Re)loading theories]\index{loading theories}\index{reloading theories}
|
|
335 |
Initially you load theory \texttt{T} by typing \ttindex{use_thy}~\texttt{"T";},
|
|
336 |
which loads all parent theories of \texttt{T} automatically, if they are not
|
|
337 |
loaded already. If you modify \texttt{T.thy} or \texttt{T.ML}, you can
|
|
338 |
reload it by typing \texttt{use_thy~"T";} again. This time, however, only
|
|
339 |
\texttt{T} is reloaded. If some of \texttt{T}'s parents have changed as well,
|
6577
|
340 |
type \ttindexbold{update_thy}~\texttt{"T";} to reload \texttt{T} and all of
|
|
341 |
its parents that have changed (or have changed parents).
|
5375
|
342 |
\end{description}
|
|
343 |
Further commands are found in the Reference Manual.
|
|
344 |
|
|
345 |
|
|
346 |
\section{Datatypes}
|
|
347 |
\label{sec:datatype}
|
|
348 |
|
|
349 |
Inductive datatypes are part of almost every non-trivial application of HOL.
|
|
350 |
First we take another look at a very important example, the datatype of
|
|
351 |
lists, before we turn to datatypes in general. The section closes with a
|
|
352 |
case study.
|
|
353 |
|
|
354 |
|
|
355 |
\subsection{Lists}
|
|
356 |
|
6148
|
357 |
Lists are one of the essential datatypes in computing. Readers of this
|
|
358 |
tutorial and users of HOL need to be familiar with their basic operations.
|
|
359 |
Theory \texttt{ToyList} is only a small fragment of HOL's predefined theory
|
6628
|
360 |
\texttt{List}\footnote{\url{http://isabelle.in.tum.de/library/HOL/List.html}}.
|
5375
|
361 |
The latter contains many further operations. For example, the functions
|
|
362 |
\ttindexbold{hd} (`head') and \ttindexbold{tl} (`tail') return the first
|
|
363 |
element and the remainder of a list. (However, pattern-matching is usually
|
6148
|
364 |
preferable to \texttt{hd} and \texttt{tl}.) Theory \texttt{List} also
|
|
365 |
contains more syntactic sugar:
|
5375
|
366 |
\texttt{[}$x@1$\texttt{,}\dots\texttt{,}$x@n$\texttt{]} abbreviates
|
6148
|
367 |
$x@1$\texttt{\#}\dots\texttt{\#}$x@n$\texttt{\#[]}. In the rest of the
|
|
368 |
tutorial we always use HOL's predefined lists.
|
5375
|
369 |
|
|
370 |
|
|
371 |
\subsection{The general format}
|
|
372 |
\label{sec:general-datatype}
|
|
373 |
|
|
374 |
The general HOL \texttt{datatype} definition is of the form
|
|
375 |
\[
|
|
376 |
\mathtt{datatype}~(\alpha@1, \dots, \alpha@n) \, t ~=~
|
|
377 |
C@1~\tau@{11}~\dots~\tau@{1k@1} ~\mid~ \dots ~\mid~
|
|
378 |
C@m~\tau@{m1}~\dots~\tau@{mk@m}
|
|
379 |
\]
|
|
380 |
where $\alpha@i$ are type variables (the parameters), $C@i$ are distinct
|
|
381 |
constructor names and $\tau@{ij}$ are types; it is customary to capitalize
|
|
382 |
the first letter in constructor names. There are a number of
|
|
383 |
restrictions (such as the type should not be empty) detailed
|
6606
|
384 |
elsewhere~\cite{isabelle-HOL}. Isabelle notifies you if you violate them.
|
5375
|
385 |
|
|
386 |
Laws about datatypes, such as \verb$[] ~= x#xs$ and \texttt{(x\#xs = y\#ys) =
|
|
387 |
(x=y \& xs=ys)}, are used automatically during proofs by simplification.
|
|
388 |
The same is true for the equations in primitive recursive function
|
|
389 |
definitions.
|
|
390 |
|
|
391 |
\subsection{Primitive recursion}
|
|
392 |
|
|
393 |
Functions on datatypes are usually defined by recursion. In fact, most of the
|
|
394 |
time they are defined by what is called \bfindex{primitive recursion}.
|
5850
|
395 |
The keyword \ttindexbold{primrec} is followed by a list of equations
|
5375
|
396 |
\[ f \, x@1 \, \dots \, (C \, y@1 \, \dots \, y@k)\, \dots \, x@n = r \]
|
|
397 |
such that $C$ is a constructor of the datatype $t$ and all recursive calls of
|
|
398 |
$f$ in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. Thus
|
|
399 |
Isabelle immediately sees that $f$ terminates because one (fixed!) argument
|
|
400 |
becomes smaller with every recursive call. There must be exactly one equation
|
|
401 |
for each constructor. Their order is immaterial.
|
|
402 |
A more general method for defining total recursive functions is explained in
|
|
403 |
\S\ref{sec:recdef}.
|
|
404 |
|
|
405 |
\begin{exercise}
|
|
406 |
Given the datatype of binary trees
|
|
407 |
\begin{ttbox}
|
|
408 |
\input{Misc/tree}\end{ttbox}
|
|
409 |
define a function \texttt{mirror} that mirrors the structure of a binary tree
|
|
410 |
by swapping subtrees (recursively). Prove \texttt{mirror(mirror(t)) = t}.
|
|
411 |
\end{exercise}
|
|
412 |
|
|
413 |
\subsection{\texttt{case}-expressions}
|
|
414 |
\label{sec:case-expressions}
|
|
415 |
|
|
416 |
HOL also features \ttindexbold{case}-expressions for analyzing elements of a
|
|
417 |
datatype. For example,
|
|
418 |
\begin{ttbox}
|
|
419 |
case xs of [] => 0 | y#ys => y
|
|
420 |
\end{ttbox}
|
|
421 |
evaluates to \texttt{0} if \texttt{xs} is \texttt{[]} and to \texttt{y} if
|
|
422 |
\texttt{xs} is \texttt{y\#ys}. (Since the result in both branches must be of
|
|
423 |
the same type, it follows that \texttt{y::nat} and hence
|
|
424 |
\texttt{xs::(nat)list}.)
|
|
425 |
|
|
426 |
In general, if $e$ is a term of the datatype $t$ defined in
|
|
427 |
\S\ref{sec:general-datatype} above, the corresponding
|
|
428 |
\texttt{case}-expression analyzing $e$ is
|
|
429 |
\[
|
|
430 |
\begin{array}{rrcl}
|
|
431 |
\mbox{\tt case}~e~\mbox{\tt of} & C@1~x@{11}~\dots~x@{1k@1} & \To & e@1 \\
|
|
432 |
\vdots \\
|
|
433 |
\mid & C@m~x@{m1}~\dots~x@{mk@m} & \To & e@m
|
|
434 |
\end{array}
|
|
435 |
\]
|
|
436 |
|
|
437 |
\begin{warn}
|
|
438 |
{\em All} constructors must be present, their order is fixed, and nested
|
|
439 |
patterns are not supported. Violating these restrictions results in strange
|
|
440 |
error messages.
|
|
441 |
\end{warn}
|
|
442 |
\noindent
|
|
443 |
Nested patterns can be simulated by nested \texttt{case}-expressions: instead
|
|
444 |
of
|
|
445 |
\begin{ttbox}
|
|
446 |
case xs of [] => 0 | [x] => x | x#(y#zs) => y
|
|
447 |
\end{ttbox}
|
|
448 |
write
|
|
449 |
\begin{ttbox}
|
|
450 |
case xs of [] => 0 | x#ys => (case ys of [] => x | y#zs => y)
|
|
451 |
\end{ttbox}
|
|
452 |
Note that \texttt{case}-expressions should be enclosed in parentheses to
|
|
453 |
indicate their scope.
|
|
454 |
|
|
455 |
\subsection{Structural induction}
|
|
456 |
|
|
457 |
Almost all the basic laws about a datatype are applied automatically during
|
|
458 |
simplification. Only induction is invoked by hand via \texttt{induct_tac},
|
|
459 |
which works for any datatype. In some cases, induction is overkill and a case
|
|
460 |
distinction over all constructors of the datatype suffices. This is performed
|
9255
|
461 |
by \ttindexbold{case_tac}. A trivial example:
|
5375
|
462 |
\begin{ttbox}
|
9255
|
463 |
\input{Misc/exhaust.ML}\Out{ 1. xs = [] ==> (case xs of [] => [] | y # ys => xs) = xs}
|
|
464 |
\Out{ 2. !!a list. xs = a # list ==> (case xs of [] => [] | y # ys => xs) = xs}
|
5375
|
465 |
\input{Misc/autotac.ML}\end{ttbox}
|
|
466 |
Note that this particular case distinction could have been automated
|
|
467 |
completely. See~\S\ref{sec:SimpFeatures}.
|
|
468 |
|
|
469 |
\begin{warn}
|
|
470 |
Induction is only allowed on a free variable that should not occur among
|
|
471 |
the assumptions of the subgoal. Exhaustion works for arbitrary terms.
|
|
472 |
\end{warn}
|
|
473 |
|
|
474 |
\subsection{Case study: boolean expressions}
|
|
475 |
\label{sec:boolex}
|
|
476 |
|
|
477 |
The aim of this case study is twofold: it shows how to model boolean
|
|
478 |
expressions and some algorithms for manipulating them, and it demonstrates
|
|
479 |
the constructs introduced above.
|
|
480 |
|
|
481 |
\subsubsection{How can we model boolean expressions?}
|
|
482 |
|
|
483 |
We want to represent boolean expressions built up from variables and
|
|
484 |
constants by negation and conjunction. The following datatype serves exactly
|
|
485 |
that purpose:
|
|
486 |
\begin{ttbox}
|
|
487 |
\input{Ifexpr/boolex}\end{ttbox}
|
|
488 |
The two constants are represented by the terms \texttt{Const~True} and
|
|
489 |
\texttt{Const~False}. Variables are represented by terms of the form
|
|
490 |
\texttt{Var}~$n$, where $n$ is a natural number (type \texttt{nat}).
|
|
491 |
For example, the formula $P@0 \land \neg P@1$ is represented by the term
|
|
492 |
\texttt{And~(Var~0)~(Neg(Var~1))}.
|
|
493 |
|
|
494 |
\subsubsection{What is the value of boolean expressions?}
|
|
495 |
|
|
496 |
The value of a boolean expressions depends on the value of its variables.
|
|
497 |
Hence the function \texttt{value} takes an additional parameter, an {\em
|
|
498 |
environment} of type \texttt{nat~=>~bool}, which maps variables to their
|
|
499 |
values:
|
|
500 |
\begin{ttbox}
|
|
501 |
\input{Ifexpr/value}\end{ttbox}
|
|
502 |
|
|
503 |
\subsubsection{If-expressions}
|
|
504 |
|
|
505 |
An alternative and often more efficient (because in a certain sense
|
|
506 |
canonical) representation are so-called \textit{If-expressions\/} built up
|
|
507 |
from constants (\texttt{CIF}), variables (\texttt{VIF}) and conditionals
|
|
508 |
(\texttt{IF}):
|
|
509 |
\begin{ttbox}
|
|
510 |
\input{Ifexpr/ifex}\end{ttbox}
|
|
511 |
The evaluation if If-expressions proceeds as for \texttt{boolex}:
|
|
512 |
\begin{ttbox}
|
|
513 |
\input{Ifexpr/valif}\end{ttbox}
|
|
514 |
|
|
515 |
\subsubsection{Transformation into and of If-expressions}
|
|
516 |
|
|
517 |
The type \texttt{boolex} is close to the customary representation of logical
|
|
518 |
formulae, whereas \texttt{ifex} is designed for efficiency. Thus we need to
|
|
519 |
translate from \texttt{boolex} into \texttt{ifex}:
|
|
520 |
\begin{ttbox}
|
|
521 |
\input{Ifexpr/bool2if}\end{ttbox}
|
|
522 |
At last, we have something we can verify: that \texttt{bool2if} preserves the
|
|
523 |
value of its argument.
|
|
524 |
\begin{ttbox}
|
|
525 |
\input{Ifexpr/bool2if.ML}\end{ttbox}
|
|
526 |
The proof is canonical:
|
|
527 |
\begin{ttbox}
|
|
528 |
\input{Ifexpr/proof.ML}\end{ttbox}
|
|
529 |
In fact, all proofs in this case study look exactly like this. Hence we do
|
|
530 |
not show them below.
|
|
531 |
|
|
532 |
More interesting is the transformation of If-expressions into a normal form
|
|
533 |
where the first argument of \texttt{IF} cannot be another \texttt{IF} but
|
|
534 |
must be a constant or variable. Such a normal form can be computed by
|
|
535 |
repeatedly replacing a subterm of the form \texttt{IF~(IF~b~x~y)~z~u} by
|
|
536 |
\texttt{IF b (IF x z u) (IF y z u)}, which has the same value. The following
|
|
537 |
primitive recursive functions perform this task:
|
|
538 |
\begin{ttbox}
|
|
539 |
\input{Ifexpr/normif}
|
|
540 |
\input{Ifexpr/norm}\end{ttbox}
|
|
541 |
Their interplay is a bit tricky, and we leave it to the reader to develop an
|
|
542 |
intuitive understanding. Fortunately, Isabelle can help us to verify that the
|
|
543 |
transformation preserves the value of the expression:
|
|
544 |
\begin{ttbox}
|
|
545 |
\input{Ifexpr/norm.ML}\end{ttbox}
|
|
546 |
The proof is canonical, provided we first show the following lemma (which
|
|
547 |
also helps to understand what \texttt{normif} does) and make it available
|
|
548 |
for simplification via \texttt{Addsimps}:
|
|
549 |
\begin{ttbox}
|
|
550 |
\input{Ifexpr/normif.ML}\end{ttbox}
|
|
551 |
|
|
552 |
But how can we be sure that \texttt{norm} really produces a normal form in
|
|
553 |
the above sense? We have to prove
|
|
554 |
\begin{ttbox}
|
|
555 |
\input{Ifexpr/normal_norm.ML}\end{ttbox}
|
|
556 |
where \texttt{normal} expresses that an If-expression is in normal form:
|
|
557 |
\begin{ttbox}
|
|
558 |
\input{Ifexpr/normal}\end{ttbox}
|
|
559 |
Of course, this requires a lemma about normality of \texttt{normif}
|
|
560 |
\begin{ttbox}
|
|
561 |
\input{Ifexpr/normal_normif.ML}\end{ttbox}
|
|
562 |
that has to be made available for simplification via \texttt{Addsimps}.
|
|
563 |
|
|
564 |
How does one come up with the required lemmas? Try to prove the main theorems
|
|
565 |
without them and study carefully what \texttt{Auto_tac} leaves unproved. This
|
|
566 |
has to provide the clue.
|
|
567 |
The necessity of universal quantification (\texttt{!t e}) in the two lemmas
|
|
568 |
is explained in \S\ref{sec:InductionHeuristics}
|
|
569 |
|
|
570 |
\begin{exercise}
|
7569
|
571 |
We strengthen the definition of a \texttt{normal} If-expression as follows:
|
5375
|
572 |
the first argument of all \texttt{IF}s must be a variable. Adapt the above
|
|
573 |
development to this changed requirement. (Hint: you may need to formulate
|
|
574 |
some of the goals as implications (\texttt{-->}) rather than equalities
|
|
575 |
(\texttt{=}).)
|
|
576 |
\end{exercise}
|
|
577 |
|
|
578 |
\section{Some basic types}
|
|
579 |
|
6577
|
580 |
|
5375
|
581 |
\subsection{Natural numbers}
|
6577
|
582 |
\index{arithmetic|(}
|
5375
|
583 |
|
|
584 |
The type \ttindexbold{nat} of natural numbers is predefined and behaves like
|
|
585 |
\begin{ttbox}
|
|
586 |
datatype nat = 0 | Suc nat
|
|
587 |
\end{ttbox}
|
|
588 |
In particular, there are \texttt{case}-expressions, for example
|
|
589 |
\begin{ttbox}
|
|
590 |
case n of 0 => 0 | Suc m => m
|
|
591 |
\end{ttbox}
|
|
592 |
primitive recursion, for example
|
|
593 |
\begin{ttbox}
|
|
594 |
\input{Misc/natsum}\end{ttbox}
|
|
595 |
and induction, for example
|
|
596 |
\begin{ttbox}
|
|
597 |
\input{Misc/NatSum.ML}\ttbreak
|
9255
|
598 |
\Out{ sum n + sum n = n * Suc n}
|
|
599 |
\Out{ No subgoals!}
|
5375
|
600 |
\end{ttbox}
|
|
601 |
|
|
602 |
The usual arithmetic operations \ttindexbold{+}, \ttindexbold{-},
|
6577
|
603 |
\ttindexbold{*}, \ttindexbold{div}, \ttindexbold{mod}, \ttindexbold{min} and
|
|
604 |
\ttindexbold{max} are predefined, as are the relations \ttindexbold{<=} and
|
|
605 |
\ttindexbold{<}. There is even a least number operation \ttindexbold{LEAST}.
|
|
606 |
For example, \texttt{(LEAST n.$\,$1 < n) = 2} (HOL does not prove this
|
|
607 |
completely automatically).
|
5375
|
608 |
|
|
609 |
\begin{warn}
|
6577
|
610 |
The operations \ttindexbold{+}, \ttindexbold{-}, \ttindexbold{*},
|
|
611 |
\ttindexbold{min}, \ttindexbold{max}, \ttindexbold{<=} and \ttindexbold{<}
|
|
612 |
are overloaded, i.e.\ they are available not just for natural numbers but
|
|
613 |
at other types as well (see \S\ref{sec:TypeClasses}). For example, given
|
5375
|
614 |
the goal \texttt{x+y = y+x}, there is nothing to indicate that you are
|
6577
|
615 |
talking about natural numbers. Hence Isabelle can only infer that
|
5375
|
616 |
\texttt{x} and \texttt{y} are of some arbitrary type where \texttt{+} is
|
|
617 |
declared. As a consequence, you will be unable to prove the goal (although
|
|
618 |
it may take you some time to realize what has happened if
|
|
619 |
\texttt{show_types} is not set). In this particular example, you need to
|
|
620 |
include an explicit type constraint, for example \texttt{x+y = y+(x::nat)}.
|
|
621 |
If there is enough contextual information this may not be necessary:
|
|
622 |
\texttt{x+0 = x} automatically implies \texttt{x::nat}.
|
|
623 |
\end{warn}
|
|
624 |
|
6577
|
625 |
Simple arithmetic goals are proved automatically by both \texttt{Auto_tac}
|
|
626 |
and the simplification tactics introduced in \S\ref{sec:Simplification}. For
|
|
627 |
example, the goal
|
|
628 |
\begin{ttbox}
|
|
629 |
\input{Misc/arith1.ML}\end{ttbox}
|
|
630 |
is proved automatically. The main restriction is that only addition is taken
|
|
631 |
into account; other arithmetic operations and quantified formulae are ignored.
|
|
632 |
|
|
633 |
For more complex goals, there is the special tactic \ttindexbold{arith_tac}. It
|
|
634 |
proves arithmetic goals involving the usual logical connectives (\verb$~$,
|
|
635 |
\verb$&$, \verb$|$, \verb$-->$), the relations \texttt{<=} and \texttt{<},
|
|
636 |
and the operations \ttindexbold{+}, \ttindexbold{-}, \ttindexbold{min} and
|
|
637 |
\ttindexbold{max}. For example, it can prove
|
|
638 |
\begin{ttbox}
|
|
639 |
\input{Misc/arith2.ML}\end{ttbox}
|
|
640 |
because \texttt{k*k} can be treated as atomic.
|
|
641 |
In contrast, $n*n = n \Longrightarrow n=0 \lor n=1$ is not
|
|
642 |
even proved by \texttt{arith_tac} because the proof relies essentially on
|
|
643 |
properties of multiplication.
|
|
644 |
|
|
645 |
\begin{warn}
|
|
646 |
The running time of \texttt{arith_tac} is exponential in the number of
|
|
647 |
occurrences of \ttindexbold{-}, \ttindexbold{min} and \ttindexbold{max}
|
|
648 |
because they are first eliminated by case distinctions.
|
|
649 |
|
|
650 |
\texttt{arith_tac} is incomplete even for the restricted class of formulae
|
|
651 |
described above (known as ``linear arithmetic''). If divisibility plays a
|
|
652 |
role, it may fail to prove a valid formula, for example $m+m \neq n+n+1$.
|
|
653 |
Fortunately, such examples are rare in practice.
|
|
654 |
\end{warn}
|
|
655 |
|
|
656 |
\index{arithmetic|)}
|
|
657 |
|
5375
|
658 |
|
|
659 |
\subsection{Products}
|
|
660 |
|
|
661 |
HOL also has pairs: \texttt{($a@1$,$a@2$)} is of type \texttt{$\tau@1$ *
|
|
662 |
$\tau@2$} provided each $a@i$ is of type $\tau@i$. The components of a pair
|
|
663 |
are extracted by \texttt{fst} and \texttt{snd}:
|
|
664 |
\texttt{fst($x$,$y$) = $x$} and \texttt{snd($x$,$y$) = $y$}. Tuples
|
|
665 |
are simulated by pairs nested to the right:
|
|
666 |
\texttt{($a@1$,$a@2$,$a@3$)} and \texttt{$\tau@1$ * $\tau@2$ * $\tau@3$}
|
|
667 |
stand for \texttt{($a@1$,($a@2$,$a@3$))} and \texttt{$\tau@1$ * ($\tau@2$ *
|
|
668 |
$\tau@3$)}. Therefore \texttt{fst(snd($a@1$,$a@2$,$a@3$)) = $a@2$}.
|
|
669 |
|
|
670 |
It is possible to use (nested) tuples as patterns in abstractions, for
|
|
671 |
example \texttt{\%(x,y,z).x+y+z} and \texttt{\%((x,y),z).x+y+z}.
|
|
672 |
|
|
673 |
In addition to explicit $\lambda$-abstractions, tuple patterns can be used in
|
|
674 |
most variable binding constructs. Typical examples are
|
|
675 |
\begin{ttbox}
|
|
676 |
let (x,y) = f z in (y,x)
|
|
677 |
|
|
678 |
case xs of [] => 0 | (x,y)\#zs => x+y
|
|
679 |
\end{ttbox}
|
|
680 |
Further important examples are quantifiers and sets.
|
|
681 |
|
|
682 |
\begin{warn}
|
|
683 |
Abstraction over pairs and tuples is merely a convenient shorthand for a more
|
|
684 |
complex internal representation. Thus the internal and external form of a
|
|
685 |
term may differ, which can affect proofs. If you want to avoid this
|
|
686 |
complication, use \texttt{fst} and \texttt{snd}, i.e.\ write
|
|
687 |
\texttt{\%p.~fst p + snd p} instead of \texttt{\%(x,y).~x + y}.
|
|
688 |
See~\S\ref{} for theorem proving with tuple patterns.
|
|
689 |
\end{warn}
|
|
690 |
|
|
691 |
|
|
692 |
\section{Definitions}
|
|
693 |
\label{sec:Definitions}
|
|
694 |
|
|
695 |
A definition is simply an abbreviation, i.e.\ a new name for an existing
|
|
696 |
construction. In particular, definitions cannot be recursive. Isabelle offers
|
|
697 |
definitions on the level of types and terms. Those on the type level are
|
|
698 |
called type synonyms, those on the term level are called (constant)
|
|
699 |
definitions.
|
|
700 |
|
|
701 |
|
|
702 |
\subsection{Type synonyms}
|
|
703 |
\indexbold{type synonyms}
|
|
704 |
|
|
705 |
Type synonyms are similar to those found in ML. Their syntax is fairly self
|
|
706 |
explanatory:
|
|
707 |
\begin{ttbox}
|
|
708 |
\input{Misc/types}\end{ttbox}\indexbold{*types}
|
|
709 |
The synonym \texttt{alist} shows that in general the type on the right-hand
|
|
710 |
side needs to be enclosed in double quotation marks
|
|
711 |
(see the end of~\S\ref{sec:intro-theory}).
|
|
712 |
|
|
713 |
Internally all synonyms are fully expanded. As a consequence Isabelle's
|
|
714 |
output never contains synonyms. Their main purpose is to improve the
|
|
715 |
readability of theory definitions. Synonyms can be used just like any other
|
|
716 |
type:
|
|
717 |
\begin{ttbox}
|
|
718 |
\input{Misc/consts}\end{ttbox}
|
|
719 |
|
|
720 |
\subsection{Constant definitions}
|
|
721 |
\label{sec:ConstDefinitions}
|
|
722 |
|
|
723 |
The above constants \texttt{nand} and \texttt{exor} are non-recursive and can
|
|
724 |
therefore be defined directly by
|
|
725 |
\begin{ttbox}
|
|
726 |
\input{Misc/defs}\end{ttbox}\indexbold{*defs}
|
|
727 |
where \texttt{defs} is a keyword and \texttt{nand_def} and \texttt{exor_def}
|
|
728 |
are arbitrary user-supplied names.
|
|
729 |
The symbol \texttt{==}\index{==>@{\tt==}|bold} is a special form of equality
|
|
730 |
that should only be used in constant definitions.
|
|
731 |
Declarations and definitions can also be merged
|
|
732 |
\begin{ttbox}
|
|
733 |
\input{Misc/constdefs}\end{ttbox}\indexbold{*constdefs}
|
|
734 |
in which case the default name of each definition is $f$\texttt{_def}, where
|
|
735 |
$f$ is the name of the defined constant.
|
|
736 |
|
|
737 |
Note that pattern-matching is not allowed, i.e.\ each definition must be of
|
|
738 |
the form $f\,x@1\,\dots\,x@n$\texttt{~==~}$t$.
|
|
739 |
|
|
740 |
Section~\S\ref{sec:Simplification} explains how definitions are used
|
|
741 |
in proofs.
|
|
742 |
|
|
743 |
\begin{warn}
|
|
744 |
A common mistake when writing definitions is to introduce extra free variables
|
|
745 |
on the right-hand side as in the following fictitious definition:
|
|
746 |
\begin{ttbox}
|
|
747 |
defs prime_def "prime(p) == (m divides p) --> (m=1 | m=p)"
|
|
748 |
\end{ttbox}
|
|
749 |
Isabelle rejects this `definition' because of the extra {\tt m} on the
|
|
750 |
right-hand side, which would introduce an inconsistency. What you should have
|
|
751 |
written is
|
|
752 |
\begin{ttbox}
|
|
753 |
defs prime_def "prime(p) == !m. (m divides p) --> (m=1 | m=p)"
|
|
754 |
\end{ttbox}
|
|
755 |
\end{warn}
|
|
756 |
|
|
757 |
|
|
758 |
|
|
759 |
|
|
760 |
\chapter{More Functional Programming}
|
|
761 |
|
|
762 |
The purpose of this chapter is to deepen the reader's understanding of the
|
|
763 |
concepts encountered so far and to introduce an advanced method for defining
|
|
764 |
recursive functions. The first two sections give a structured presentation of
|
|
765 |
theorem proving by simplification (\S\ref{sec:Simplification}) and
|
|
766 |
discuss important heuristics for induction (\S\ref{sec:InductionHeuristics}). They
|
|
767 |
can be skipped by readers less interested in proofs. They are followed by a
|
|
768 |
case study, a compiler for expressions (\S\ref{sec:ExprCompiler}).
|
|
769 |
Finally we present a very general method for defining recursive functions
|
|
770 |
that goes well beyond what \texttt{primrec} allows (\S\ref{sec:recdef}).
|
|
771 |
|
|
772 |
|
|
773 |
\section{Simplification}
|
|
774 |
\label{sec:Simplification}
|
|
775 |
|
|
776 |
So far we have proved our theorems by \texttt{Auto_tac}, which
|
|
777 |
`simplifies' all subgoals. In fact, \texttt{Auto_tac} can do much more than
|
|
778 |
that, except that it did not need to so far. However, when you go beyond toy
|
|
779 |
examples, you need to understand the ingredients of \texttt{Auto_tac}.
|
|
780 |
This section covers the tactic that \texttt{Auto_tac} always applies first,
|
|
781 |
namely simplification.
|
|
782 |
|
|
783 |
Simplification is one of the central theorem proving tools in Isabelle and
|
|
784 |
many other systems. The tool itself is called the \bfindex{simplifier}. The
|
|
785 |
purpose of this section is twofold: to introduce the many features of the
|
|
786 |
simplifier (\S\ref{sec:SimpFeatures}) and to explain a little bit how the
|
|
787 |
simplifier works (\S\ref{sec:SimpHow}). Anybody intending to use HOL should
|
|
788 |
read \S\ref{sec:SimpFeatures}, and the serious student should read
|
|
789 |
\S\ref{sec:SimpHow} as well in order to understand what happened in case
|
|
790 |
things do not simplify as expected.
|
|
791 |
|
|
792 |
|
|
793 |
\subsection{Using the simplifier}
|
|
794 |
\label{sec:SimpFeatures}
|
|
795 |
|
|
796 |
In its most basic form, simplification means repeated application of
|
|
797 |
equations from left to right. For example, taking the rules for \texttt{\at}
|
|
798 |
and applying them to the term \texttt{[0,1] \at\ []} results in a sequence of
|
|
799 |
simplification steps:
|
|
800 |
\begin{ttbox}\makeatother
|
|
801 |
(0#1#[]) @ [] \(\leadsto\) 0#((1#[]) @ []) \(\leadsto\) 0#(1#([] @ [])) \(\leadsto\) 0#1#[]
|
|
802 |
\end{ttbox}
|
|
803 |
This is also known as {\em term rewriting} and the equations are referred
|
|
804 |
to as {\em rewrite rules}. This is more honest than `simplification' because
|
|
805 |
the terms do not necessarily become simpler in the process.
|
|
806 |
|
|
807 |
\subsubsection{Simpsets}
|
|
808 |
|
|
809 |
To facilitate simplification, each theory has an associated set of
|
|
810 |
simplification rules, known as a \bfindex{simpset}. Within a theory,
|
|
811 |
proofs by simplification refer to the associated simpset by default.
|
|
812 |
The simpset of a theory is built up as follows: starting with the union of
|
|
813 |
the simpsets of the parent theories, each occurrence of a \texttt{datatype}
|
|
814 |
or \texttt{primrec} construct augments the simpset. Explicit definitions are
|
|
815 |
not added automatically. Users can add new theorems via \texttt{Addsimps} and
|
|
816 |
delete them again later by \texttt{Delsimps}.
|
|
817 |
|
|
818 |
You may augment a simpset not just by equations but by pretty much any
|
|
819 |
theorem. The simplifier will try to make sense of it. For example, a theorem
|
|
820 |
\verb$~$$P$ is automatically turned into \texttt{$P$ = False}. The details are
|
|
821 |
explained in \S\ref{sec:SimpHow}.
|
|
822 |
|
|
823 |
As a rule of thumb, rewrite rules that really simplify a term (like
|
|
824 |
\texttt{xs \at\ [] = xs} and \texttt{rev(rev xs) = xs}) should be added to the
|
|
825 |
current simpset right after they have been proved. Those of a more specific
|
|
826 |
nature (e.g.\ distributivity laws, which alter the structure of terms
|
|
827 |
considerably) should only be added for specific proofs and deleted again
|
|
828 |
afterwards. Conversely, it may also happen that a generally useful rule
|
|
829 |
needs to be removed for a certain proof and is added again afterwards. The
|
|
830 |
need of frequent temporary additions or deletions may indicate a badly
|
|
831 |
designed simpset.
|
|
832 |
\begin{warn}
|
|
833 |
Simplification may not terminate, for example if both $f(x) = g(x)$ and
|
|
834 |
$g(x) = f(x)$ are in the simpset. It is the user's responsibility not to
|
|
835 |
include rules that can lead to nontermination, either on their own or in
|
|
836 |
combination with other rules.
|
|
837 |
\end{warn}
|
|
838 |
|
|
839 |
\subsubsection{Simplification tactics}
|
|
840 |
|
|
841 |
There are four main simplification tactics:
|
|
842 |
\begin{ttdescription}
|
|
843 |
\item[\ttindexbold{Simp_tac} $i$] simplifies the conclusion of subgoal~$i$
|
|
844 |
using the theory's simpset. It may solve the subgoal completely if it has
|
|
845 |
become trivial. For example:
|
|
846 |
\begin{ttbox}\makeatother
|
9255
|
847 |
\Out{ 1. [] @ [] = []}
|
5375
|
848 |
by(Simp_tac 1);
|
9255
|
849 |
\Out{ No subgoals!}
|
5375
|
850 |
\end{ttbox}
|
|
851 |
|
|
852 |
\item[\ttindexbold{Asm_simp_tac}]
|
|
853 |
is like \verb$Simp_tac$, but extracts additional rewrite rules from
|
|
854 |
the assumptions of the subgoal. For example, it solves
|
|
855 |
\begin{ttbox}\makeatother
|
9255
|
856 |
\Out{1. xs = [] ==> xs @ ys = ys @ xs}
|
5375
|
857 |
\end{ttbox}
|
|
858 |
which \texttt{Simp_tac} does not do.
|
|
859 |
|
|
860 |
\item[\ttindexbold{Full_simp_tac}] is like \verb$Simp_tac$, but also
|
|
861 |
simplifies the assumptions (without using the assumptions to
|
|
862 |
simplify each other or the actual goal).
|
|
863 |
|
|
864 |
\item[\ttindexbold{Asm_full_simp_tac}] is like \verb$Asm_simp_tac$,
|
|
865 |
but also simplifies the assumptions. In particular, assumptions can
|
|
866 |
simplify each other. For example:
|
|
867 |
\begin{ttbox}\makeatother
|
9255
|
868 |
\Out{ 1. [| xs @ zs = ys @ xs; [] @ xs = [] @ [] |] ==> ys = zs}
|
5375
|
869 |
by(Asm_full_simp_tac 1);
|
9255
|
870 |
\Out{ No subgoals!}
|
5375
|
871 |
\end{ttbox}
|
|
872 |
The second assumption simplifies to \texttt{xs = []}, which in turn
|
|
873 |
simplifies the first assumption to \texttt{zs = ys}, thus reducing the
|
|
874 |
conclusion to \texttt{ys = ys} and hence to \texttt{True}.
|
|
875 |
(See also the paragraph on tracing below.)
|
|
876 |
\end{ttdescription}
|
|
877 |
\texttt{Asm_full_simp_tac} is the most powerful of this quartet of
|
|
878 |
tactics. In fact, \texttt{Auto_tac} starts by applying
|
|
879 |
\texttt{Asm_full_simp_tac} to all subgoals. The only reason for the existence
|
|
880 |
of the other three tactics is that sometimes one wants to limit the amount of
|
|
881 |
simplification, for example to avoid nontermination:
|
|
882 |
\begin{ttbox}\makeatother
|
9255
|
883 |
\Out{1. ! x. f x = g (f (g x)) ==> f [] = f [] @ []}
|
5375
|
884 |
\end{ttbox}
|
|
885 |
is solved by \texttt{Simp_tac}, but \texttt{Asm_simp_tac} and
|
|
886 |
\texttt{Asm_full_simp_tac} loop because the rewrite rule \texttt{f x = g(f(g
|
|
887 |
x))} extracted from the assumption does not terminate. Isabelle notices
|
|
888 |
certain simple forms of nontermination, but not this one.
|
|
889 |
|
|
890 |
\subsubsection{Modifying simpsets locally}
|
|
891 |
|
|
892 |
If a certain theorem is merely needed in one proof by simplification, the
|
|
893 |
pattern
|
|
894 |
\begin{ttbox}
|
|
895 |
Addsimps [\(rare_theorem\)];
|
|
896 |
by(Simp_tac 1);
|
|
897 |
Delsimps [\(rare_theorem\)];
|
|
898 |
\end{ttbox}
|
|
899 |
is awkward. Therefore there are lower-case versions of the simplification
|
|
900 |
tactics (\ttindexbold{simp_tac}, \ttindexbold{asm_simp_tac},
|
|
901 |
\ttindexbold{full_simp_tac}, \ttindexbold{asm_full_simp_tac}) and of the
|
|
902 |
simpset modifiers (\ttindexbold{addsimps}, \ttindexbold{delsimps})
|
|
903 |
that do not access or modify the implicit simpset but explicitly take a
|
|
904 |
simpset as an argument. For example, the above three lines become
|
|
905 |
\begin{ttbox}
|
|
906 |
by(simp_tac (simpset() addsimps [\(rare_theorem\)]) 1);
|
|
907 |
\end{ttbox}
|
|
908 |
where the result of the function call \texttt{simpset()} is the simpset of
|
|
909 |
the current theory and \texttt{addsimps} is an infix function. The implicit
|
|
910 |
simpset is read once but not modified.
|
|
911 |
This is far preferable to pairs of \texttt{Addsimps} and \texttt{Delsimps}.
|
|
912 |
Local modifications can be stacked as in
|
|
913 |
\begin{ttbox}
|
|
914 |
by(simp_tac (simpset() addsimps [\(rare_theorem\)] delsimps [\(some_thm\)]) 1);
|
|
915 |
\end{ttbox}
|
|
916 |
|
|
917 |
\subsubsection{Rewriting with definitions}
|
|
918 |
|
|
919 |
Constant definitions (\S\ref{sec:ConstDefinitions}) are not automatically
|
|
920 |
included in the simpset of a theory. Hence such definitions are not expanded
|
|
921 |
automatically either, just as it should be: definitions are introduced for
|
|
922 |
the purpose of abbreviating complex concepts. Of course we need to expand the
|
|
923 |
definitions initially to derive enough lemmas that characterize the concept
|
|
924 |
sufficiently for us to forget the original definition completely. For
|
|
925 |
example, given the theory
|
|
926 |
\begin{ttbox}
|
|
927 |
\input{Misc/Exor.thy}\end{ttbox}
|
|
928 |
we may want to prove \verb$exor A (~A)$. Instead of \texttt{Goal} we use
|
|
929 |
\begin{ttbox}
|
|
930 |
\input{Misc/exorgoal.ML}\end{ttbox}
|
|
931 |
which tells Isabelle to expand the definition of \texttt{exor}---the first
|
|
932 |
argument of \texttt{Goalw} can be a list of definitions---in the initial goal:
|
|
933 |
\begin{ttbox}
|
9255
|
934 |
\Out{exor A (~ A)}
|
|
935 |
\Out{ 1. A & ~ ~ A | ~ A & ~ A}
|
5375
|
936 |
\end{ttbox}
|
|
937 |
In this simple example, the goal is proved by \texttt{Simp_tac}.
|
|
938 |
Of course the resulting theorem is insufficient to characterize \texttt{exor}
|
|
939 |
completely.
|
|
940 |
|
|
941 |
In case we want to expand a definition in the middle of a proof, we can
|
|
942 |
simply add the definition locally to the simpset:
|
|
943 |
\begin{ttbox}
|
|
944 |
\input{Misc/exorproof.ML}\end{ttbox}
|
|
945 |
You should normally not add the definition permanently to the simpset
|
|
946 |
using \texttt{Addsimps} because this defeats the whole purpose of an
|
|
947 |
abbreviation.
|
|
948 |
|
|
949 |
\begin{warn}
|
|
950 |
If you have defined $f\,x\,y$\texttt{~==~}$t$ then you can only expand
|
|
951 |
occurrences of $f$ with at least two arguments. Thus it is safer to define
|
|
952 |
$f$\texttt{~==~\%$x\,y$.}$\;t$.
|
|
953 |
\end{warn}
|
|
954 |
|
|
955 |
\subsubsection{Simplifying \texttt{let}-expressions}
|
|
956 |
|
|
957 |
Proving a goal containing \ttindex{let}-expressions invariably requires the
|
|
958 |
\texttt{let}-constructs to be expanded at some point. Since
|
|
959 |
\texttt{let}-\texttt{in} is just syntactic sugar for a defined constant
|
|
960 |
(called \texttt{Let}), expanding \texttt{let}-constructs means rewriting with
|
|
961 |
\texttt{Let_def}:
|
|
962 |
%context List.thy;
|
|
963 |
%Goal "(let xs = [] in xs@xs) = ys";
|
|
964 |
\begin{ttbox}\makeatother
|
9255
|
965 |
\Out{ 1. (let xs = [] in xs @ xs) = ys}
|
5375
|
966 |
by(simp_tac (simpset() addsimps [Let_def]) 1);
|
9255
|
967 |
\Out{ 1. [] = ys}
|
5375
|
968 |
\end{ttbox}
|
|
969 |
If, in a particular context, there is no danger of a combinatorial explosion
|
|
970 |
of nested \texttt{let}s one could even add \texttt{Let_def} permanently via
|
|
971 |
\texttt{Addsimps}.
|
|
972 |
|
|
973 |
\subsubsection{Conditional equations}
|
|
974 |
|
|
975 |
So far all examples of rewrite rules were equations. The simplifier also
|
|
976 |
accepts {\em conditional\/} equations, for example
|
|
977 |
\begin{ttbox}
|
|
978 |
xs ~= [] ==> hd xs # tl xs = xs \hfill \((*)\)
|
|
979 |
\end{ttbox}
|
9255
|
980 |
(which is proved by \texttt{case_tac} on \texttt{xs} followed by
|
5375
|
981 |
\texttt{Asm_full_simp_tac} twice). Assuming that this theorem together with
|
|
982 |
%\begin{ttbox}\makeatother
|
|
983 |
\texttt{(rev xs = []) = (xs = [])}
|
|
984 |
%\end{ttbox}
|
|
985 |
are part of the simpset, the subgoal
|
|
986 |
\begin{ttbox}\makeatother
|
9255
|
987 |
\Out{1. xs ~= [] ==> hd(rev xs) # tl(rev xs) = rev xs}
|
5375
|
988 |
\end{ttbox}
|
|
989 |
is proved by simplification:
|
|
990 |
the conditional equation $(*)$ above
|
|
991 |
can simplify \texttt{hd(rev~xs)~\#~tl(rev~xs)} to \texttt{rev xs}
|
|
992 |
because the corresponding precondition \verb$rev xs ~= []$ simplifies to
|
|
993 |
\verb$xs ~= []$, which is exactly the local assumption of the subgoal.
|
|
994 |
|
|
995 |
|
|
996 |
\subsubsection{Automatic case splits}
|
|
997 |
|
|
998 |
Goals containing \ttindex{if}-expressions are usually proved by case
|
|
999 |
distinction on the condition of the \texttt{if}. For example the goal
|
|
1000 |
\begin{ttbox}
|
9255
|
1001 |
\Out{1. ! xs. if xs = [] then rev xs = [] else rev xs ~= []}
|
5375
|
1002 |
\end{ttbox}
|
|
1003 |
can be split into
|
|
1004 |
\begin{ttbox}
|
9255
|
1005 |
\Out{1. ! xs. (xs = [] --> rev xs = []) \& (xs ~= [] --> rev xs ~= [])}
|
5375
|
1006 |
\end{ttbox}
|
|
1007 |
by typing
|
|
1008 |
\begin{ttbox}
|
|
1009 |
\input{Misc/splitif.ML}\end{ttbox}
|
|
1010 |
Because this is almost always the right proof strategy, the simplifier
|
|
1011 |
performs case-splitting on \texttt{if}s automatically. Try \texttt{Simp_tac}
|
|
1012 |
on the initial goal above.
|
|
1013 |
|
|
1014 |
This splitting idea generalizes from \texttt{if} to \ttindex{case}:
|
|
1015 |
\begin{ttbox}\makeatother
|
9255
|
1016 |
\Out{1. (case xs of [] => zs | y#ys => y#(ys@zs)) = xs@zs}
|
5375
|
1017 |
\end{ttbox}
|
|
1018 |
becomes
|
|
1019 |
\begin{ttbox}\makeatother
|
9255
|
1020 |
\Out{1. (xs = [] --> zs = xs @ zs) &}
|
|
1021 |
\Out{ (! a list. xs = a # list --> a # list @ zs = xs @ zs)}
|
5375
|
1022 |
\end{ttbox}
|
|
1023 |
by typing
|
|
1024 |
\begin{ttbox}
|
|
1025 |
\input{Misc/splitlist.ML}\end{ttbox}
|
|
1026 |
In contrast to \texttt{if}-expressions, the simplifier does not split
|
|
1027 |
\texttt{case}-expressions by default because this can lead to nontermination
|
|
1028 |
in case of recursive datatypes.
|
|
1029 |
Nevertheless the simplifier can be instructed to perform \texttt{case}-splits
|
|
1030 |
by adding the appropriate rule to the simpset:
|
|
1031 |
\begin{ttbox}
|
9721
|
1032 |
by(simp_tac (simpset() addsplits [list.split]) 1);
|
5375
|
1033 |
\end{ttbox}\indexbold{*addsplits}
|
|
1034 |
solves the initial goal outright, which \texttt{Simp_tac} alone will not do.
|
|
1035 |
|
|
1036 |
In general, every datatype $t$ comes with a rule
|
|
1037 |
\texttt{$t$.split} that can be added to the simpset either
|
|
1038 |
locally via \texttt{addsplits} (see above), or permanently via
|
|
1039 |
\begin{ttbox}
|
|
1040 |
Addsplits [\(t\).split];
|
|
1041 |
\end{ttbox}\indexbold{*Addsplits}
|
|
1042 |
Split-rules can be removed globally via \ttindexbold{Delsplits} and locally
|
|
1043 |
via \ttindexbold{delsplits} as, for example, in
|
|
1044 |
\begin{ttbox}
|
|
1045 |
by(simp_tac (simpset() addsimps [\(\dots\)] delsplits [split_if]) 1);
|
|
1046 |
\end{ttbox}
|
|
1047 |
|
|
1048 |
|
6577
|
1049 |
\subsubsection{Arithmetic}
|
|
1050 |
\index{arithmetic}
|
|
1051 |
|
|
1052 |
The simplifier routinely solves a small class of linear arithmetic formulae
|
|
1053 |
(over types \texttt{nat} and \texttt{int}): it only takes into account
|
|
1054 |
assumptions and conclusions that are (possibly negated) (in)equalities
|
|
1055 |
(\texttt{=}, \texttt{<=}, \texttt{<}) and it only knows about addition. Thus
|
|
1056 |
\begin{ttbox}
|
|
1057 |
\input{Misc/arith1.ML}\end{ttbox}
|
|
1058 |
is proved by simplification, whereas the only slightly more complex
|
|
1059 |
\begin{ttbox}
|
|
1060 |
\input{Misc/arith3.ML}\end{ttbox}
|
|
1061 |
is not proved by simplification and requires \texttt{arith_tac}.
|
|
1062 |
|
5375
|
1063 |
\subsubsection{Permutative rewrite rules}
|
|
1064 |
|
|
1065 |
A rewrite rule is {\bf permutative} if the left-hand side and right-hand side
|
|
1066 |
are the same up to renaming of variables. The most common permutative rule
|
|
1067 |
is commutativity: $x+y = y+x$. Another example is $(x-y)-z = (x-z)-y$. Such
|
|
1068 |
rules are problematic because once they apply, they can be used forever.
|
|
1069 |
The simplifier is aware of this danger and treats permutative rules
|
6606
|
1070 |
separately. For details see~\cite{isabelle-ref}.
|
5375
|
1071 |
|
|
1072 |
\subsubsection{Tracing}
|
|
1073 |
\indexbold{tracing the simplifier}
|
|
1074 |
|
|
1075 |
Using the simplifier effectively may take a bit of experimentation. Set the
|
|
1076 |
\verb$trace_simp$ flag to get a better idea of what is going on:
|
|
1077 |
\begin{ttbox}\makeatother
|
9255
|
1078 |
\Out{ 1. rev [x] = []}
|
5375
|
1079 |
\ttbreak
|
|
1080 |
set trace_simp;
|
|
1081 |
by(Simp_tac 1);
|
9676
|
1082 |
\ttbreak
|
9255
|
1083 |
\Out{ Applying instance of rewrite rule:}
|
|
1084 |
\Out{ rev (?x # ?xs) == rev ?xs @ [?x]}
|
|
1085 |
\Out{ Rewriting:}
|
|
1086 |
\Out{ rev [x] == rev [] @ [x]}
|
5375
|
1087 |
\ttbreak
|
9255
|
1088 |
\Out{ Applying instance of rewrite rule:}
|
|
1089 |
\Out{ rev [] == []}
|
|
1090 |
\Out{ Rewriting:}
|
|
1091 |
\Out{ rev [] == []}
|
9676
|
1092 |
\ttbreak
|
9255
|
1093 |
\Out{ Applying instance of rewrite rule:}
|
|
1094 |
\Out{ [] @ ?y == ?y}
|
|
1095 |
\Out{ Rewriting:}
|
|
1096 |
\Out{ [] @ [x] == [x]}
|
5375
|
1097 |
\ttbreak
|
9255
|
1098 |
\Out{ Applying instance of rewrite rule:}
|
|
1099 |
\Out{ ?x # ?t = ?t == False}
|
|
1100 |
\Out{ Rewriting:}
|
|
1101 |
\Out{ [x] = [] == False}
|
5375
|
1102 |
\ttbreak
|
9255
|
1103 |
\Out{ Level 1}
|
|
1104 |
\Out{ rev [x] = []}
|
|
1105 |
\Out{ 1. False}
|
5375
|
1106 |
\end{ttbox}
|
|
1107 |
In more complicated cases, the trace can be enormous, especially since
|
|
1108 |
invocations of the simplifier are often nested (e.g.\ when solving conditions
|
|
1109 |
of rewrite rules).
|
|
1110 |
|
|
1111 |
\subsection{How it works}
|
|
1112 |
\label{sec:SimpHow}
|
|
1113 |
|
|
1114 |
\subsubsection{Higher-order patterns}
|
|
1115 |
|
|
1116 |
\subsubsection{Local assumptions}
|
|
1117 |
|
|
1118 |
\subsubsection{The preprocessor}
|
|
1119 |
|
|
1120 |
\section{Induction heuristics}
|
|
1121 |
\label{sec:InductionHeuristics}
|
|
1122 |
|
|
1123 |
The purpose of this section is to illustrate some simple heuristics for
|
|
1124 |
inductive proofs. The first one we have already mentioned in our initial
|
|
1125 |
example:
|
|
1126 |
\begin{quote}
|
|
1127 |
{\em 1. Theorems about recursive functions are proved by induction.}
|
|
1128 |
\end{quote}
|
|
1129 |
In case the function has more than one argument
|
|
1130 |
\begin{quote}
|
|
1131 |
{\em 2. Do induction on argument number $i$ if the function is defined by
|
|
1132 |
recursion in argument number $i$.}
|
|
1133 |
\end{quote}
|
|
1134 |
When we look at the proof of
|
|
1135 |
\begin{ttbox}\makeatother
|
|
1136 |
(xs @ ys) @ zs = xs @ (ys @ zs)
|
|
1137 |
\end{ttbox}
|
|
1138 |
in \S\ref{sec:intro-proof} we find (a) \texttt{\at} is recursive in the first
|
|
1139 |
argument, (b) \texttt{xs} occurs only as the first argument of \texttt{\at},
|
|
1140 |
and (c) both \texttt{ys} and \texttt{zs} occur at least once as the second
|
|
1141 |
argument of \texttt{\at}. Hence it is natural to perform induction on
|
|
1142 |
\texttt{xs}.
|
|
1143 |
|
|
1144 |
The key heuristic, and the main point of this section, is to
|
|
1145 |
generalize the goal before induction. The reason is simple: if the goal is
|
|
1146 |
too specific, the induction hypothesis is too weak to allow the induction
|
|
1147 |
step to go through. Let us now illustrate the idea with an example.
|
|
1148 |
|
|
1149 |
We define a tail-recursive version of list-reversal,
|
|
1150 |
i.e.\ one that can be compiled into a loop:
|
|
1151 |
\begin{ttbox}
|
|
1152 |
\input{Misc/Itrev.thy}\end{ttbox}
|
|
1153 |
The behaviour of \texttt{itrev} is simple: it reverses its first argument by
|
|
1154 |
stacking its elements onto the second argument, and returning that second
|
|
1155 |
argument when the first one becomes empty.
|
|
1156 |
We need to show that \texttt{itrev} does indeed reverse its first argument
|
|
1157 |
provided the second one is empty:
|
|
1158 |
\begin{ttbox}
|
|
1159 |
\input{Misc/itrev1.ML}\end{ttbox}
|
|
1160 |
There is no choice as to the induction variable, and we immediately simplify:
|
9676
|
1161 |
\begin{ttbox}\makeatother
|
|
1162 |
\input{Misc/induct_auto.ML}\ttbreak
|
9255
|
1163 |
\Out{ 1. !!a list. itrev list [] = rev list\(\;\)==> itrev list [a] = rev list @ [a]}
|
5375
|
1164 |
\end{ttbox}
|
|
1165 |
Just as predicted above, the overall goal, and hence the induction
|
|
1166 |
hypothesis, is too weak to solve the induction step because of the fixed
|
|
1167 |
\texttt{[]}. The corresponding heuristic:
|
|
1168 |
\begin{quote}
|
|
1169 |
{\em 3. Generalize goals for induction by replacing constants by variables.}
|
|
1170 |
\end{quote}
|
|
1171 |
Of course one cannot do this na\"{\i}vely: \texttt{itrev xs ys = rev xs} is
|
|
1172 |
just not true --- the correct generalization is
|
|
1173 |
\begin{ttbox}\makeatother
|
|
1174 |
\input{Misc/itrev2.ML}\end{ttbox}
|
|
1175 |
If \texttt{ys} is replaced by \texttt{[]}, the right-hand side simplifies to
|
|
1176 |
\texttt{rev xs}, just as required.
|
|
1177 |
|
|
1178 |
In this particular instance it is easy to guess the right generalization,
|
|
1179 |
but in more complex situations a good deal of creativity is needed. This is
|
|
1180 |
the main source of complications in inductive proofs.
|
|
1181 |
|
|
1182 |
Although we now have two variables, only \texttt{xs} is suitable for
|
|
1183 |
induction, and we repeat our above proof attempt. Unfortunately, we are still
|
|
1184 |
not there:
|
|
1185 |
\begin{ttbox}\makeatother
|
9255
|
1186 |
\Out{1. !!a list.}
|
|
1187 |
\Out{ itrev list ys = rev list @ ys}
|
|
1188 |
\Out{ ==> itrev list (a # ys) = rev list @ a # ys}
|
5375
|
1189 |
\end{ttbox}
|
|
1190 |
The induction hypothesis is still too weak, but this time it takes no
|
|
1191 |
intuition to generalize: the problem is that \texttt{ys} is fixed throughout
|
|
1192 |
the subgoal, but the induction hypothesis needs to be applied with
|
|
1193 |
\texttt{a \# ys} instead of \texttt{ys}. Hence we prove the theorem
|
|
1194 |
for all \texttt{ys} instead of a fixed one:
|
|
1195 |
\begin{ttbox}\makeatother
|
|
1196 |
\input{Misc/itrev3.ML}\end{ttbox}
|
|
1197 |
This time induction on \texttt{xs} followed by simplification succeeds. This
|
|
1198 |
leads to another heuristic for generalization:
|
|
1199 |
\begin{quote}
|
|
1200 |
{\em 4. Generalize goals for induction by universally quantifying all free
|
|
1201 |
variables {\em(except the induction variable itself!)}.}
|
|
1202 |
\end{quote}
|
|
1203 |
This prevents trivial failures like the above and does not change the
|
|
1204 |
provability of the goal. Because it is not always required, and may even
|
|
1205 |
complicate matters in some cases, this heuristic is often not
|
|
1206 |
applied blindly.
|
|
1207 |
|
|
1208 |
A final point worth mentioning is the orientation of the equation we just
|
|
1209 |
proved: the more complex notion (\texttt{itrev}) is on the left-hand
|
|
1210 |
side, the simpler \texttt{rev} on the right-hand side. This constitutes
|
|
1211 |
another, albeit weak heuristic that is not restricted to induction:
|
|
1212 |
\begin{quote}
|
|
1213 |
{\em 5. The right-hand side of an equation should (in some sense) be
|
|
1214 |
simpler than the left-hand side.}
|
|
1215 |
\end{quote}
|
|
1216 |
The heuristic is tricky to apply because it is not obvious that
|
|
1217 |
\texttt{rev xs \at\ ys} is simpler than \texttt{itrev xs ys}. But see what
|
|
1218 |
happens if you try to prove the symmetric equation!
|
|
1219 |
|
|
1220 |
|
|
1221 |
\section{Case study: compiling expressions}
|
|
1222 |
\label{sec:ExprCompiler}
|
|
1223 |
|
|
1224 |
The task is to develop a compiler from a generic type of expressions (built
|
|
1225 |
up from variables, constants and binary operations) to a stack machine. This
|
|
1226 |
generic type of expressions is a generalization of the boolean expressions in
|
|
1227 |
\S\ref{sec:boolex}. This time we do not commit ourselves to a particular
|
|
1228 |
type of variables or values but make them type parameters. Neither is there
|
|
1229 |
a fixed set of binary operations: instead the expression contains the
|
|
1230 |
appropriate function itself.
|
|
1231 |
\begin{ttbox}
|
|
1232 |
\input{CodeGen/expr}\end{ttbox}
|
|
1233 |
The three constructors represent constants, variables and the combination of
|
|
1234 |
two subexpressions with a binary operation.
|
|
1235 |
|
|
1236 |
The value of an expression w.r.t.\ an environment that maps variables to
|
|
1237 |
values is easily defined:
|
|
1238 |
\begin{ttbox}
|
|
1239 |
\input{CodeGen/value}\end{ttbox}
|
|
1240 |
|
|
1241 |
The stack machine has three instructions: load a constant value onto the
|
|
1242 |
stack, load the contents of a certain address onto the stack, and apply a
|
|
1243 |
binary operation to the two topmost elements of the stack, replacing them by
|
|
1244 |
the result. As for \texttt{expr}, addresses and values are type parameters:
|
|
1245 |
\begin{ttbox}
|
|
1246 |
\input{CodeGen/instr}\end{ttbox}
|
|
1247 |
|
|
1248 |
The execution of the stack machine is modelled by a function \texttt{exec}
|
|
1249 |
that takes a store (modelled as a function from addresses to values, just
|
|
1250 |
like the environment for evaluating expressions), a stack (modelled as a
|
|
1251 |
list) of values and a list of instructions and returns the stack at the end
|
|
1252 |
of the execution --- the store remains unchanged:
|
|
1253 |
\begin{ttbox}
|
|
1254 |
\input{CodeGen/exec}\end{ttbox}
|
|
1255 |
Recall that \texttt{hd} and \texttt{tl}
|
|
1256 |
return the first element and the remainder of a list.
|
|
1257 |
|
|
1258 |
Because all functions are total, \texttt{hd} is defined even for the empty
|
|
1259 |
list, although we do not know what the result is. Thus our model of the
|
|
1260 |
machine always terminates properly, although the above definition does not
|
|
1261 |
tell us much about the result in situations where \texttt{Apply} was executed
|
|
1262 |
with fewer than two elements on the stack.
|
|
1263 |
|
|
1264 |
The compiler is a function from expressions to a list of instructions. Its
|
|
1265 |
definition is pretty much obvious:
|
|
1266 |
\begin{ttbox}\makeatother
|
|
1267 |
\input{CodeGen/comp}\end{ttbox}
|
|
1268 |
|
|
1269 |
Now we have to prove the correctness of the compiler, i.e.\ that the
|
|
1270 |
execution of a compiled expression results in the value of the expression:
|
|
1271 |
\begin{ttbox}
|
|
1272 |
exec s [] (comp e) = [value s e]
|
|
1273 |
\end{ttbox}
|
|
1274 |
This is generalized to
|
|
1275 |
\begin{ttbox}
|
|
1276 |
\input{CodeGen/goal2.ML}\end{ttbox}
|
|
1277 |
and proved by induction on \texttt{e} followed by simplification, once we
|
|
1278 |
have the following lemma about executing the concatenation of two instruction
|
|
1279 |
sequences:
|
|
1280 |
\begin{ttbox}\makeatother
|
6099
|
1281 |
\input{CodeGen/goal1.ML}\end{ttbox}
|
5375
|
1282 |
This requires induction on \texttt{xs} and ordinary simplification for the
|
|
1283 |
base cases. In the induction step, simplification leaves us with a formula
|
|
1284 |
that contains two \texttt{case}-expressions over instructions. Thus we add
|
|
1285 |
automatic case splitting as well, which finishes the proof:
|
|
1286 |
\begin{ttbox}
|
|
1287 |
\input{CodeGen/simpsplit.ML}\end{ttbox}
|
|
1288 |
|
|
1289 |
We could now go back and prove \texttt{exec s [] (comp e) = [value s e]}
|
|
1290 |
merely by simplification with the generalized version we just proved.
|
|
1291 |
However, this is unnecessary because the generalized version fully subsumes
|
|
1292 |
its instance.
|
|
1293 |
|
5850
|
1294 |
|
|
1295 |
\section{Advanced datatypes}
|
|
1296 |
\index{*datatype|(}
|
|
1297 |
\index{*primrec|(}
|
|
1298 |
|
6099
|
1299 |
This section presents advanced forms of \texttt{datatype}s and (in the near
|
|
1300 |
future!) records.
|
|
1301 |
|
5850
|
1302 |
\subsection{Mutual recursion}
|
9255
|
1303 |
\label{sec:datatype-mut-rec}
|
5850
|
1304 |
|
|
1305 |
Sometimes it is necessary to define two datatypes that depend on each
|
|
1306 |
other. This is called \textbf{mutual recursion}. As an example consider a
|
|
1307 |
language of arithmetic and boolean expressions where
|
|
1308 |
\begin{itemize}
|
|
1309 |
\item arithmetic expressions contain boolean expressions because there are
|
|
1310 |
conditional arithmetic expressions like ``if $m<n$ then $n-m$ else $m-n$'',
|
|
1311 |
and
|
|
1312 |
\item boolean expressions contain arithmetic expressions because of
|
|
1313 |
comparisons like ``$m<n$''.
|
|
1314 |
\end{itemize}
|
|
1315 |
In Isabelle this becomes
|
|
1316 |
\begin{ttbox}
|
|
1317 |
\input{Datatype/abdata}\end{ttbox}\indexbold{*and}
|
|
1318 |
Type \texttt{aexp} is similar to \texttt{expr} in \S\ref{sec:ExprCompiler},
|
|
1319 |
except that we have fixed the values to be of type \texttt{nat} and that we
|
|
1320 |
have fixed the two binary operations \texttt{Sum} and \texttt{Diff}. Boolean
|
|
1321 |
expressions can be arithmetic comparisons, conjunctions and negations.
|
|
1322 |
The semantics is fixed via two evaluation functions
|
|
1323 |
\begin{ttbox}
|
|
1324 |
\input{Datatype/abconstseval}\end{ttbox}
|
|
1325 |
that take an environment (a mapping from variables \texttt{'a} to values
|
|
1326 |
\texttt{nat}) and an expression and return its arithmetic/boolean
|
|
1327 |
value. Since the datatypes are mutually recursive, so are functions that
|
|
1328 |
operate on them. Hence they need to be defined in a single \texttt{primrec}
|
|
1329 |
section:
|
|
1330 |
\begin{ttbox}
|
|
1331 |
\input{Datatype/abevala}
|
|
1332 |
\input{Datatype/abevalb}\end{ttbox}
|
|
1333 |
|
|
1334 |
%In general, given $n$ mutually recursive datatypes, whenever you define a
|
|
1335 |
%\texttt{primrec} function on one of them, Isabelle expects you to define $n$
|
|
1336 |
%(possibly mutually recursive) functions simultaneously.
|
|
1337 |
|
|
1338 |
In the same fashion we also define two functions that perform substitution:
|
|
1339 |
\begin{ttbox}
|
|
1340 |
\input{Datatype/abconstssubst}\end{ttbox}
|
|
1341 |
The first argument is a function mapping variables to expressions, the
|
|
1342 |
substitution. It is applied to all variables in the second argument. As a
|
|
1343 |
result, the type of variables in the expression may change from \texttt{'a}
|
|
1344 |
to \texttt{'b}. Note that there are only arithmetic and no boolean variables.
|
|
1345 |
\begin{ttbox}
|
|
1346 |
\input{Datatype/absubsta}
|
|
1347 |
\input{Datatype/absubstb}\end{ttbox}
|
|
1348 |
|
|
1349 |
Now we can prove a fundamental theorem about the interaction between
|
|
1350 |
evaluation and substitution: applying a substitution $s$ to an expression $a$
|
|
1351 |
and evaluating the result in an environment $env$ yields the same result as
|
|
1352 |
evaluation $a$ in the environment that maps every variable $x$ to the value
|
|
1353 |
of $s(x)$ under $env$. If you try to prove this separately for arithmetic or
|
|
1354 |
boolean expressions (by induction), you find that you always need the other
|
|
1355 |
theorem in the induction step. Therefore you need to state and prove both
|
|
1356 |
theorems simultaneously:
|
|
1357 |
\begin{quote}\small
|
|
1358 |
\verbatiminput{Datatype/abgoalind.ML}
|
7848
|
1359 |
\end{quote}
|
5850
|
1360 |
The resulting 8 goals (one for each constructor) are proved in one fell swoop
|
|
1361 |
\texttt{by Auto_tac;}.
|
|
1362 |
|
|
1363 |
In general, given $n$ mutually recursive datatypes $\tau@1$, \dots, $\tau@n$,
|
|
1364 |
Isabelle expects an inductive proof to start with a goal of the form
|
|
1365 |
\[ P@1(x@1)\ \texttt{\&}\ \dots\ \texttt{\&}\ P@n(x@n) \]
|
|
1366 |
where each variable $x@i$ is of type $\tau@i$. Induction is started by
|
|
1367 |
\begin{ttbox}
|
7848
|
1368 |
by(induct_tac "\(x@1\) \(\dots\) \(x@n\)" \(k\));
|
5850
|
1369 |
\end{ttbox}
|
|
1370 |
|
|
1371 |
\begin{exercise}
|
|
1372 |
Define a function \texttt{norma} of type \texttt{'a aexp => 'a aexp} that
|
|
1373 |
replaces \texttt{IF}s with complex boolean conditions by nested
|
|
1374 |
\texttt{IF}s where each condition is a \texttt{Less} --- \texttt{And} and
|
|
1375 |
\texttt{Neg} should be eliminated completely. Prove that \texttt{norma}
|
|
1376 |
preserves the value of an expression and that the result of \texttt{norma}
|
|
1377 |
is really normal, i.e.\ no more \texttt{And}s and \texttt{Neg}s occur in
|
|
1378 |
it. ({\em Hint:} proceed as in \S\ref{sec:boolex}).
|
|
1379 |
\end{exercise}
|
|
1380 |
|
|
1381 |
\subsection{Nested recursion}
|
|
1382 |
|
|
1383 |
So far, all datatypes had the property that on the right-hand side of their
|
|
1384 |
definition they occurred only at the top-level, i.e.\ directly below a
|
|
1385 |
constructor. This is not the case any longer for the following model of terms
|
|
1386 |
where function symbols can be applied to a list of arguments:
|
|
1387 |
\begin{ttbox}
|
|
1388 |
\input{Datatype/tdata}\end{ttbox}
|
|
1389 |
Parameter \texttt{'a} is the type of variables and \texttt{'b} the type of
|
|
1390 |
function symbols.
|
|
1391 |
A mathematical term like $f(x,g(y))$ becomes \texttt{App f [Var x, App g
|
|
1392 |
[Var y]]}, where \texttt{f}, \texttt{g}, \texttt{x}, \texttt{y} are
|
|
1393 |
suitable values, e.g.\ numbers or strings.
|
|
1394 |
|
|
1395 |
What complicates the definition of \texttt{term} is the nested occurrence of
|
|
1396 |
\texttt{term} inside \texttt{list} on the right-hand side. In principle,
|
|
1397 |
nested recursion can be eliminated in favour of mutual recursion by unfolding
|
|
1398 |
the offending datatypes, here \texttt{list}. The result for \texttt{term}
|
|
1399 |
would be something like
|
|
1400 |
\begin{ttbox}
|
|
1401 |
\input{Datatype/tunfoldeddata}\end{ttbox}
|
7569
|
1402 |
Although we do not recommend this unfolding to the user, it shows how to
|
|
1403 |
simulate nested recursion by mutual recursion.
|
|
1404 |
Now we return to the initial definition of \texttt{term} using
|
5850
|
1405 |
nested recursion.
|
|
1406 |
|
|
1407 |
Let us define a substitution function on terms. Because terms involve term
|
|
1408 |
lists, we need to define two substitution functions simultaneously:
|
|
1409 |
\begin{ttbox}
|
|
1410 |
\input{Datatype/tconstssubst}
|
|
1411 |
\input{Datatype/tsubst}
|
|
1412 |
\input{Datatype/tsubsts}\end{ttbox}
|
|
1413 |
Similarly, when proving a statement about terms inductively, we need
|
|
1414 |
to prove a related statement about term lists simultaneously. For example,
|
|
1415 |
the fact that the identity substitution does not change a term needs to be
|
|
1416 |
strengthened and proved as follows:
|
|
1417 |
\begin{quote}\small
|
|
1418 |
\verbatiminput{Datatype/tidproof.ML}
|
|
1419 |
\end{quote}
|
|
1420 |
Note that \texttt{Var} is the identity substitution because by definition it
|
|
1421 |
leaves variables unchanged: \texttt{subst Var (Var $x$) = Var $x$}. Note also
|
|
1422 |
that the type annotations are necessary because otherwise there is nothing in
|
6099
|
1423 |
the goal to enforce that both halves of the goal talk about the same type
|
5850
|
1424 |
parameters \texttt{('a,'b)}. As a result, induction would fail
|
6099
|
1425 |
because the two halves of the goal would be unrelated.
|
5850
|
1426 |
|
|
1427 |
\begin{exercise}
|
6099
|
1428 |
The fact that substitution distributes over composition can be expressed
|
|
1429 |
roughly as follows:
|
5850
|
1430 |
\begin{ttbox}
|
|
1431 |
subst (f o g) t = subst f (subst g t)
|
|
1432 |
\end{ttbox}
|
|
1433 |
Correct this statement (you will find that it does not type-check),
|
|
1434 |
strengthen it and prove it. (Note: \texttt{o} is function composition; its
|
|
1435 |
definition is found in the theorem \texttt{o_def}).
|
|
1436 |
\end{exercise}
|
|
1437 |
|
|
1438 |
|
9255
|
1439 |
Of course, you may also combine mutual and nested recursion. For example,
|
|
1440 |
constructor \texttt{Sum} in \S\ref{sec:datatype-mut-rec} could take a list of
|
|
1441 |
expressions as its argument: \texttt{Sum ('a aexp list)}.
|
5850
|
1442 |
|
|
1443 |
|
|
1444 |
\subsection{The limits of nested recursion}
|
|
1445 |
|
|
1446 |
How far can we push nested recursion? By the unfolding argument above, we can
|
|
1447 |
reduce nested to mutual recursion provided the nested recursion only involves
|
7569
|
1448 |
previously defined datatypes. The \texttt{data} example above involves
|
|
1449 |
products, which is fine because products are also datatypes.
|
5850
|
1450 |
However, functions are most emphatically not allowed:
|
|
1451 |
\begin{ttbox}
|
|
1452 |
datatype t = C (t => bool)
|
|
1453 |
\end{ttbox}
|
|
1454 |
is a real can of worms: in HOL it must be ruled out because it requires a
|
|
1455 |
type \texttt{t} such that \texttt{t} and its power set \texttt{t => bool}
|
|
1456 |
have the same cardinality---an impossibility.
|
|
1457 |
In theory, we could allow limited forms of recursion involving function
|
|
1458 |
spaces. For example
|
|
1459 |
\begin{ttbox}
|
|
1460 |
datatype t = C (nat => t) | D
|
|
1461 |
\end{ttbox}
|
|
1462 |
is unproblematic. However, Isabelle does not support recursion involving
|
|
1463 |
\texttt{=>} at all at the moment.
|
|
1464 |
|
|
1465 |
For a theoretical analysis of what kinds of datatypes are feasible in HOL
|
|
1466 |
see, for example,~\cite{Gunter-HOL92}. There are alternatives to pure HOL:
|
6606
|
1467 |
LCF~\cite{paulson87} is a logic where types like
|
5850
|
1468 |
\begin{ttbox}
|
|
1469 |
datatype t = C (t -> t)
|
|
1470 |
\end{ttbox}
|
6099
|
1471 |
do indeed make sense (note the intentionally different arrow \texttt{->}!).
|
5850
|
1472 |
There is even a version of LCF on top of HOL, called
|
6606
|
1473 |
HOLCF~\cite{MuellerNvOS99}.
|
5850
|
1474 |
|
|
1475 |
\index{*primrec|)}
|
|
1476 |
\index{*datatype|)}
|
|
1477 |
|
|
1478 |
\subsection{Case study: Tries}
|
|
1479 |
|
|
1480 |
Tries are a classic search tree data structure~\cite{Knuth3-75} for fast
|
|
1481 |
indexing with strings. Figure~\ref{fig:trie} gives a graphical example of a
|
|
1482 |
trie containing the words ``all'', ``an'', ``ape'', ``can'', ``car'' and
|
|
1483 |
``cat''. When searching a string in a trie, the letters of the string are
|
|
1484 |
examined sequentially. Each letter determines which subtrie to search next.
|
|
1485 |
In this case study we model tries as a datatype, define a lookup and an
|
|
1486 |
update function, and prove that they behave as expected.
|
|
1487 |
|
|
1488 |
\begin{figure}[htbp]
|
|
1489 |
\begin{center}
|
|
1490 |
\unitlength1mm
|
|
1491 |
\begin{picture}(60,30)
|
|
1492 |
\put( 5, 0){\makebox(0,0)[b]{l}}
|
|
1493 |
\put(25, 0){\makebox(0,0)[b]{e}}
|
|
1494 |
\put(35, 0){\makebox(0,0)[b]{n}}
|
|
1495 |
\put(45, 0){\makebox(0,0)[b]{r}}
|
|
1496 |
\put(55, 0){\makebox(0,0)[b]{t}}
|
|
1497 |
%
|
|
1498 |
\put( 5, 9){\line(0,-1){5}}
|
|
1499 |
\put(25, 9){\line(0,-1){5}}
|
|
1500 |
\put(44, 9){\line(-3,-2){9}}
|
|
1501 |
\put(45, 9){\line(0,-1){5}}
|
|
1502 |
\put(46, 9){\line(3,-2){9}}
|
|
1503 |
%
|
|
1504 |
\put( 5,10){\makebox(0,0)[b]{l}}
|
|
1505 |
\put(15,10){\makebox(0,0)[b]{n}}
|
|
1506 |
\put(25,10){\makebox(0,0)[b]{p}}
|
|
1507 |
\put(45,10){\makebox(0,0)[b]{a}}
|
|
1508 |
%
|
|
1509 |
\put(14,19){\line(-3,-2){9}}
|
|
1510 |
\put(15,19){\line(0,-1){5}}
|
|
1511 |
\put(16,19){\line(3,-2){9}}
|
|
1512 |
\put(45,19){\line(0,-1){5}}
|
|
1513 |
%
|
|
1514 |
\put(15,20){\makebox(0,0)[b]{a}}
|
|
1515 |
\put(45,20){\makebox(0,0)[b]{c}}
|
|
1516 |
%
|
|
1517 |
\put(30,30){\line(-3,-2){13}}
|
|
1518 |
\put(30,30){\line(3,-2){13}}
|
|
1519 |
\end{picture}
|
|
1520 |
\end{center}
|
|
1521 |
\caption{A sample trie}
|
|
1522 |
\label{fig:trie}
|
|
1523 |
\end{figure}
|
|
1524 |
|
6099
|
1525 |
Proper tries associate some value with each string. Since the
|
5850
|
1526 |
information is stored only in the final node associated with the string, many
|
|
1527 |
nodes do not carry any value. This distinction is captured by the
|
9255
|
1528 |
following predefined datatype (from theory \texttt{Option}, which is a parent
|
|
1529 |
of \texttt{Main}):
|
5850
|
1530 |
\begin{ttbox}
|
|
1531 |
datatype 'a option = None | Some 'a
|
6099
|
1532 |
\end{ttbox}\indexbold{*option}\indexbold{*None}\indexbold{*Some}
|
5850
|
1533 |
|
|
1534 |
To minimize running time, each node of a trie should contain an array that maps
|
6099
|
1535 |
letters to subtries. We have chosen a more space efficient representation
|
5850
|
1536 |
where the subtries are held in an association list, i.e.\ a list of
|
|
1537 |
(letter,trie) pairs. Abstracting over the alphabet \texttt{'a} and the
|
|
1538 |
values \texttt{'v} we define a trie as follows:
|
|
1539 |
\begin{ttbox}
|
|
1540 |
\input{Datatype/trie}\end{ttbox}
|
|
1541 |
The first component is the optional value, the second component the
|
|
1542 |
association list of subtries. We define two corresponding selector functions:
|
|
1543 |
\begin{ttbox}
|
|
1544 |
\input{Datatype/triesels}\end{ttbox}
|
|
1545 |
Association lists come with a generic lookup function:
|
|
1546 |
\begin{ttbox}
|
|
1547 |
\input{Datatype/assoc}\end{ttbox}
|
|
1548 |
|
|
1549 |
Now we can define the lookup function for tries. It descends into the trie
|
|
1550 |
examining the letters of the search string one by one. As
|
|
1551 |
recursion on lists is simpler than on tries, let us express this as primitive
|
|
1552 |
recursion on the search string argument:
|
|
1553 |
\begin{ttbox}
|
|
1554 |
\input{Datatype/lookup}\end{ttbox}
|
|
1555 |
As a first simple property we prove that looking up a string in the empty
|
|
1556 |
trie \texttt{Trie~None~[]} always returns \texttt{None}. The proof merely
|
|
1557 |
distinguishes the two cases whether the search string is empty or not:
|
|
1558 |
\begin{ttbox}
|
|
1559 |
\input{Datatype/lookupempty.ML}\end{ttbox}
|
|
1560 |
This lemma is added to the simpset as usual.
|
|
1561 |
|
|
1562 |
Things begin to get interesting with the definition of an update function
|
|
1563 |
that adds a new (string,value) pair to a trie, overwriting the old value
|
|
1564 |
associated with that string:
|
|
1565 |
\begin{ttbox}
|
|
1566 |
\input{Datatype/update}\end{ttbox}
|
|
1567 |
The base case is obvious. In the recursive case the subtrie
|
|
1568 |
\texttt{tt} associated with the first letter \texttt{a} is extracted,
|
|
1569 |
recursively updated, and then placed in front of the association list.
|
|
1570 |
The old subtrie associated with \texttt{a} is still in the association list
|
|
1571 |
but no longer accessible via \texttt{assoc}. Clearly, there is room here for
|
|
1572 |
optimizations!
|
|
1573 |
|
|
1574 |
Our main goal is to prove the correct interaction of \texttt{update} and
|
|
1575 |
\texttt{lookup}:
|
|
1576 |
\begin{quote}\small
|
|
1577 |
\verbatiminput{Datatype/triemain.ML}
|
|
1578 |
\end{quote}
|
|
1579 |
Our plan will be to induct on \texttt{as}; hence the remaining variables are
|
|
1580 |
quantified. From the definitions it is clear that induction on either
|
|
1581 |
\texttt{as} or \texttt{bs} is required. The choice of \texttt{as} is merely
|
|
1582 |
guided by the intuition that simplification of \texttt{lookup} might be easier
|
|
1583 |
if \texttt{update} has already been simplified, which can only happen if
|
|
1584 |
\texttt{as} is instantiated.
|
|
1585 |
The start of the proof is completely conventional:
|
|
1586 |
\begin{ttbox}
|
|
1587 |
\input{Datatype/trieinduct.ML}\end{ttbox}
|
|
1588 |
Unfortunately, this time we are left with three intimidating looking subgoals:
|
|
1589 |
\begin{ttbox}
|
9255
|
1590 |
\Out{1. ... ==> ... lookup (...) bs = lookup t bs}
|
|
1591 |
\Out{2. ... ==> ... lookup (...) bs = lookup t bs}
|
|
1592 |
\Out{3. ... ==> ... lookup (...) bs = lookup t bs}
|
5850
|
1593 |
\end{ttbox}
|
|
1594 |
Clearly, if we want to make headway we have to instantiate \texttt{bs} as
|
|
1595 |
well now. It turns out that instead of induction, case distinction
|
|
1596 |
suffices:
|
|
1597 |
\begin{ttbox}
|
|
1598 |
\input{Datatype/trieexhaust.ML}\end{ttbox}
|
|
1599 |
The {\em tactical} \texttt{ALLGOALS} merely applies the tactic following it
|
|
1600 |
to all subgoals, which results in a total of six subgoals; \texttt{Auto_tac}
|
|
1601 |
solves them all.
|
|
1602 |
|
|
1603 |
This proof may look surprisingly straightforward. The reason is that we
|
|
1604 |
have told the simplifier (without telling the reader) to expand all
|
|
1605 |
\texttt{let}s and to split all \texttt{case}-constructs over options before
|
|
1606 |
we started on the main goal:
|
|
1607 |
\begin{ttbox}
|
|
1608 |
\input{Datatype/trieAdds.ML}\end{ttbox}
|
|
1609 |
|
|
1610 |
\begin{exercise}
|
|
1611 |
Write an improved version of \texttt{update} that does not suffer from the
|
|
1612 |
space leak in the version above. Prove the main theorem for your improved
|
|
1613 |
\texttt{update}.
|
|
1614 |
\end{exercise}
|
|
1615 |
|
|
1616 |
\begin{exercise}
|
|
1617 |
Modify \texttt{update} so that it can also {\em delete} entries from a
|
|
1618 |
trie. It is up to you if you want to shrink tries if possible. Prove (a
|
|
1619 |
modified version of) the main theorem above.
|
|
1620 |
\end{exercise}
|
|
1621 |
|
5375
|
1622 |
\section{Total recursive functions}
|
|
1623 |
\label{sec:recdef}
|
|
1624 |
\index{*recdef|(}
|
|
1625 |
|
|
1626 |
|
|
1627 |
Although many total functions have a natural primitive recursive definition,
|
|
1628 |
this is not always the case. Arbitrary total recursive functions can be
|
|
1629 |
defined by means of \texttt{recdef}: you can use full pattern-matching,
|
|
1630 |
recursion need not involve datatypes, and termination is proved by showing
|
7569
|
1631 |
that the arguments of all recursive calls are smaller in a suitable (user
|
5375
|
1632 |
supplied) sense.
|
|
1633 |
|
|
1634 |
\subsection{Defining recursive functions}
|
|
1635 |
|
|
1636 |
Here is a simple example, the Fibonacci function:
|
|
1637 |
\begin{ttbox}
|
6099
|
1638 |
\input{Recdef/fib}\end{ttbox}
|
5375
|
1639 |
The definition of \texttt{fib} is accompanied by a \bfindex{measure function}
|
|
1640 |
\texttt{\%n.$\;$n} that maps the argument of \texttt{fib} to a natural
|
|
1641 |
number. The requirement is that in each equation the measure of the argument
|
|
1642 |
on the left-hand side is strictly greater than the measure of the argument of
|
|
1643 |
each recursive call. In the case of \texttt{fib} this is obviously true
|
|
1644 |
because the measure function is the identity and \texttt{Suc(Suc~x)} is
|
|
1645 |
strictly greater than both \texttt{x} and \texttt{Suc~x}.
|
|
1646 |
|
|
1647 |
Slightly more interesting is the insertion of a fixed element
|
|
1648 |
between any two elements of a list:
|
|
1649 |
\begin{ttbox}
|
7587
|
1650 |
\input{Recdef/sep}\end{ttbox}
|
5375
|
1651 |
This time the measure is the length of the list, which decreases with the
|
|
1652 |
recursive call; the first component of the argument tuple is irrelevant.
|
|
1653 |
|
|
1654 |
Pattern matching need not be exhaustive:
|
|
1655 |
\begin{ttbox}
|
6099
|
1656 |
\input{Recdef/last}\end{ttbox}
|
5375
|
1657 |
|
|
1658 |
Overlapping patterns are disambiguated by taking the order of equations into
|
|
1659 |
account, just as in functional programming:
|
6099
|
1660 |
\begin{ttbox}
|
|
1661 |
\input{Recdef/sep1}\end{ttbox}
|
5375
|
1662 |
This defines exactly the same function \texttt{sep} as further above.
|
|
1663 |
|
|
1664 |
\begin{warn}
|
6099
|
1665 |
Currently \texttt{recdef} only takes the first argument of a (curried)
|
|
1666 |
recursive function into account. This means both the termination measure
|
|
1667 |
and pattern matching can only use that first argument. In general, you will
|
|
1668 |
therefore have to combine several arguments into a tuple. In case only one
|
|
1669 |
argument is relevant for termination, you can also rearrange the order of
|
|
1670 |
arguments as in
|
|
1671 |
\begin{ttbox}
|
|
1672 |
\input{Recdef/sep2}\end{ttbox}
|
5375
|
1673 |
\end{warn}
|
|
1674 |
|
|
1675 |
When loading a theory containing a \texttt{recdef} of a function $f$,
|
|
1676 |
Isabelle proves the recursion equations and stores the result as a list of
|
|
1677 |
theorems $f$.\texttt{rules}. It can be viewed by typing
|
|
1678 |
\begin{ttbox}
|
|
1679 |
prths \(f\).rules;
|
|
1680 |
\end{ttbox}
|
|
1681 |
All of the above examples are simple enough that Isabelle can determine
|
|
1682 |
automatically that the measure of the argument goes down in each recursive
|
|
1683 |
call. In that case $f$.\texttt{rules} contains precisely the defining
|
|
1684 |
equations.
|
|
1685 |
|
|
1686 |
In general, Isabelle may not be able to prove all termination conditions
|
|
1687 |
automatically. For example, termination of
|
|
1688 |
\begin{ttbox}
|
9255
|
1689 |
\input{Recdef/constsgcd}recdef gcd "measure (\%(m,n).n)"
|
6099
|
1690 |
"gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"
|
5375
|
1691 |
\end{ttbox}
|
|
1692 |
relies on the lemma \texttt{mod_less_divisor}
|
|
1693 |
\begin{ttbox}
|
|
1694 |
0 < n ==> m mod n < n
|
|
1695 |
\end{ttbox}
|
|
1696 |
that is not part of the default simpset. As a result, Isabelle prints a
|
|
1697 |
warning and \texttt{gcd.rules} contains a precondition:
|
|
1698 |
\begin{ttbox}
|
|
1699 |
(! m n. 0 < n --> m mod n < n) ==> gcd (m, n) = (if n=0 \dots)
|
|
1700 |
\end{ttbox}
|
|
1701 |
We need to instruct \texttt{recdef} to use an extended simpset to prove the
|
|
1702 |
termination condition:
|
|
1703 |
\begin{ttbox}
|
6099
|
1704 |
\input{Recdef/gcd}\end{ttbox}
|
5375
|
1705 |
This time everything works fine and \texttt{gcd.rules} contains precisely the
|
|
1706 |
stated recursion equation for \texttt{gcd}.
|
|
1707 |
|
|
1708 |
When defining some nontrivial total recursive function, the first attempt
|
|
1709 |
will usually generate a number of termination conditions, some of which may
|
|
1710 |
require new lemmas to be proved in some of the parent theories. Those lemmas
|
|
1711 |
can then be added to the simpset used by \texttt{recdef} for its
|
|
1712 |
proofs, as shown for \texttt{gcd}.
|
|
1713 |
|
|
1714 |
Although all the above examples employ measure functions, \texttt{recdef}
|
|
1715 |
allows arbitrary wellfounded relations. For example, termination of
|
|
1716 |
Ackermann's function requires the lexicographic product \texttt{**}:
|
|
1717 |
\begin{ttbox}
|
6099
|
1718 |
\input{Recdef/ack}\end{ttbox}
|
6606
|
1719 |
For details see the manual~\cite{isabelle-HOL} and the examples in the
|
6099
|
1720 |
library.
|
5375
|
1721 |
|
|
1722 |
|
|
1723 |
\subsection{Deriving simplification rules}
|
|
1724 |
|
6099
|
1725 |
Once we have succeeded in proving all termination conditions, we can start to
|
5375
|
1726 |
derive some theorems. In contrast to \texttt{primrec} definitions, which are
|
|
1727 |
automatically added to the simpset, \texttt{recdef} rules must be included
|
|
1728 |
explicitly, for example as in
|
|
1729 |
\begin{ttbox}
|
|
1730 |
Addsimps fib.rules;
|
|
1731 |
\end{ttbox}
|
|
1732 |
However, some care is necessary now, in contrast to \texttt{primrec}.
|
|
1733 |
Although \texttt{gcd} is a total function, its defining equation leads to
|
|
1734 |
nontermination of the simplifier, because the subterm \texttt{gcd(n, m mod
|
|
1735 |
n)} on the right-hand side can again be simplified by the same equation,
|
|
1736 |
and so on. The reason: the simplifier rewrites the \texttt{then} and
|
|
1737 |
\texttt{else} branches of a conditional if the condition simplifies to
|
|
1738 |
neither \texttt{True} nor \texttt{False}. Therefore it is recommended to
|
|
1739 |
derive an alternative formulation that replaces case distinctions on the
|
|
1740 |
right-hand side by conditional equations. For \texttt{gcd} it means we have
|
|
1741 |
to prove
|
|
1742 |
\begin{ttbox}
|
|
1743 |
gcd (m, 0) = m
|
|
1744 |
n ~= 0 ==> gcd (m, n) = gcd(n, m mod n)
|
|
1745 |
\end{ttbox}
|
|
1746 |
To avoid nontermination during those proofs, we have to resort to some low
|
|
1747 |
level tactics:
|
|
1748 |
\begin{ttbox}
|
|
1749 |
Goal "gcd(m,0) = m";
|
|
1750 |
by(resolve_tac [trans] 1);
|
|
1751 |
by(resolve_tac gcd.rules 1);
|
|
1752 |
by(Simp_tac 1);
|
|
1753 |
\end{ttbox}
|
|
1754 |
At this point it is not necessary to understand what exactly
|
|
1755 |
\texttt{resolve_tac} is doing. The main point is that the above proof works
|
|
1756 |
not just for this one example but in general (except that we have to use
|
|
1757 |
\texttt{Asm_simp_tac} and $f$\texttt{.rules} in general). Try the second
|
|
1758 |
\texttt{gcd}-equation above!
|
|
1759 |
|
9255
|
1760 |
|
5375
|
1761 |
\subsection{Induction}
|
|
1762 |
|
|
1763 |
Assuming we have added the recursion equations (or some suitable derived
|
|
1764 |
equations) to the simpset, we might like to prove something about our
|
|
1765 |
function. Since the function is recursive, the natural proof principle is
|
|
1766 |
again induction. But this time the structural form of induction that comes
|
|
1767 |
with datatypes is unlikely to work well---otherwise we could have defined the
|
|
1768 |
function by \texttt{primrec}. Therefore \texttt{recdef} automatically proves
|
|
1769 |
a suitable induction rule $f$\texttt{.induct} that follows the recursion
|
|
1770 |
pattern of the particular function $f$. Roughly speaking, it requires you to
|
|
1771 |
prove for each \texttt{recdef} equation that the property you are trying to
|
|
1772 |
establish holds for the left-hand side provided it holds for all recursive
|
|
1773 |
calls on the right-hand side. Applying $f$\texttt{.induct} requires its
|
|
1774 |
explicit instantiation. See \S\ref{sec:explicit-inst} for details.
|
|
1775 |
|
|
1776 |
\index{*recdef|)}
|