9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
9924
|
3 |
\def\isabellecontext{ToyList}%
|
15136
|
4 |
\isacommand{theory}\ ToyList\isanewline
|
15141
|
5 |
\isakeyword{imports}\ PreList\isanewline
|
15136
|
6 |
\isakeyword{begin}\isamarkupfalse%
|
11866
|
7 |
%
|
8749
|
8 |
\begin{isamarkuptext}%
|
|
9 |
\noindent
|
8771
|
10 |
HOL already has a predefined theory of lists called \isa{List} ---
|
|
11 |
\isa{ToyList} is merely a small fragment of it chosen as an example. In
|
8749
|
12 |
contrast to what is recommended in \S\ref{sec:Basic:Theories},
|
8771
|
13 |
\isa{ToyList} is not based on \isa{Main} but on \isa{PreList}, a
|
8749
|
14 |
theory that contains pretty much everything but lists, thus avoiding
|
|
15 |
ambiguities caused by defining lists twice.%
|
|
16 |
\end{isamarkuptext}%
|
11866
|
17 |
\isamarkuptrue%
|
9674
|
18 |
\isacommand{datatype}\ {\isacharprime}a\ list\ {\isacharequal}\ Nil\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharparenleft}{\isachardoublequote}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}{\isacharparenright}\isanewline
|
11866
|
19 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Cons\ {\isacharprime}a\ {\isachardoublequote}{\isacharprime}a\ list{\isachardoublequote}\ \ \ \ \ \ \ \ \ \ \ \ {\isacharparenleft}\isakeyword{infixr}\ {\isachardoublequote}{\isacharhash}{\isachardoublequote}\ {\isadigit{6}}{\isadigit{5}}{\isacharparenright}\isamarkupfalse%
|
|
20 |
%
|
8749
|
21 |
\begin{isamarkuptext}%
|
|
22 |
\noindent
|
12327
|
23 |
The datatype\index{datatype@\isacommand {datatype} (command)}
|
|
24 |
\tydx{list} introduces two
|
11428
|
25 |
constructors \cdx{Nil} and \cdx{Cons}, the
|
9541
|
26 |
empty~list and the operator that adds an element to the front of a list. For
|
9792
|
27 |
example, the term \isa{Cons True (Cons False Nil)} is a value of
|
|
28 |
type \isa{bool\ list}, namely the list with the elements \isa{True} and
|
11450
|
29 |
\isa{False}. Because this notation quickly becomes unwieldy, the
|
8749
|
30 |
datatype declaration is annotated with an alternative syntax: instead of
|
9541
|
31 |
\isa{Nil} and \isa{Cons x xs} we can write
|
15364
|
32 |
\isa{{\isacharbrackleft}{\isacharbrackright}}\index{$HOL2list@\isa{[]}|bold} and
|
|
33 |
\isa{x\ {\isacharhash}\ xs}\index{$HOL2list@\isa{\#}|bold}. In fact, this
|
11450
|
34 |
alternative syntax is the familiar one. Thus the list \isa{Cons True
|
9674
|
35 |
(Cons False Nil)} becomes \isa{True\ {\isacharhash}\ False\ {\isacharhash}\ {\isacharbrackleft}{\isacharbrackright}}. The annotation
|
11428
|
36 |
\isacommand{infixr}\index{infixr@\isacommand{infixr} (annotation)}
|
|
37 |
means that \isa{{\isacharhash}} associates to
|
11450
|
38 |
the right: the term \isa{x\ {\isacharhash}\ y\ {\isacharhash}\ z} is read as \isa{x\ {\isacharhash}\ {\isacharparenleft}y\ {\isacharhash}\ z{\isacharparenright}}
|
9792
|
39 |
and not as \isa{{\isacharparenleft}x\ {\isacharhash}\ y{\isacharparenright}\ {\isacharhash}\ z}.
|
10971
|
40 |
The \isa{{\isadigit{6}}{\isadigit{5}}} is the priority of the infix \isa{{\isacharhash}}.
|
8749
|
41 |
|
|
42 |
\begin{warn}
|
13191
|
43 |
Syntax annotations can be powerful, but they are difficult to master and
|
11456
|
44 |
are never necessary. You
|
8771
|
45 |
could drop them from theory \isa{ToyList} and go back to the identifiers
|
10795
|
46 |
\isa{Nil} and \isa{Cons}.
|
11456
|
47 |
Novices should avoid using
|
10795
|
48 |
syntax annotations in their own theories.
|
8749
|
49 |
\end{warn}
|
11428
|
50 |
Next, two functions \isa{app} and \cdx{rev} are declared:%
|
8749
|
51 |
\end{isamarkuptext}%
|
11866
|
52 |
\isamarkuptrue%
|
10187
|
53 |
\isacommand{consts}\ app\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\ \ \ {\isacharparenleft}\isakeyword{infixr}\ {\isachardoublequote}{\isacharat}{\isachardoublequote}\ {\isadigit{6}}{\isadigit{5}}{\isacharparenright}\isanewline
|
11866
|
54 |
\ \ \ \ \ \ \ rev\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isamarkupfalse%
|
|
55 |
%
|
8749
|
56 |
\begin{isamarkuptext}%
|
|
57 |
\noindent
|
10971
|
58 |
In contrast to many functional programming languages,
|
|
59 |
Isabelle insists on explicit declarations of all functions
|
11456
|
60 |
(keyword \commdx{consts}). Apart from the declaration-before-use
|
10971
|
61 |
restriction, the order of items in a theory file is unconstrained. Function
|
10790
|
62 |
\isa{app} is annotated with concrete syntax too. Instead of the
|
|
63 |
prefix syntax \isa{app\ xs\ ys} the infix
|
15364
|
64 |
\isa{xs\ {\isacharat}\ ys}\index{$HOL2list@\isa{\at}|bold} becomes the preferred
|
8749
|
65 |
form. Both functions are defined recursively:%
|
|
66 |
\end{isamarkuptext}%
|
11866
|
67 |
\isamarkuptrue%
|
8749
|
68 |
\isacommand{primrec}\isanewline
|
9674
|
69 |
{\isachardoublequote}{\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ ys\ \ \ \ \ \ \ {\isacharequal}\ ys{\isachardoublequote}\isanewline
|
|
70 |
{\isachardoublequote}{\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ {\isacharat}\ ys\ {\isacharequal}\ x\ {\isacharhash}\ {\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}{\isachardoublequote}\isanewline
|
8749
|
71 |
\isanewline
|
11866
|
72 |
\isamarkupfalse%
|
8749
|
73 |
\isacommand{primrec}\isanewline
|
9674
|
74 |
{\isachardoublequote}rev\ {\isacharbrackleft}{\isacharbrackright}\ \ \ \ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}\isanewline
|
11866
|
75 |
{\isachardoublequote}rev\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ \ {\isacharequal}\ {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharat}\ {\isacharparenleft}x\ {\isacharhash}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
76 |
%
|
8749
|
77 |
\begin{isamarkuptext}%
|
11456
|
78 |
\noindent\index{*rev (constant)|(}\index{append function|(}
|
10790
|
79 |
The equations for \isa{app} and \isa{rev} hardly need comments:
|
|
80 |
\isa{app} appends two lists and \isa{rev} reverses a list. The
|
11428
|
81 |
keyword \commdx{primrec} indicates that the recursion is
|
10790
|
82 |
of a particularly primitive kind where each recursive call peels off a datatype
|
8771
|
83 |
constructor from one of the arguments. Thus the
|
10654
|
84 |
recursion always terminates, i.e.\ the function is \textbf{total}.
|
11428
|
85 |
\index{functions!total}
|
8749
|
86 |
|
|
87 |
The termination requirement is absolutely essential in HOL, a logic of total
|
|
88 |
functions. If we were to drop it, inconsistencies would quickly arise: the
|
|
89 |
``definition'' $f(n) = f(n)+1$ immediately leads to $0 = 1$ by subtracting
|
|
90 |
$f(n)$ on both sides.
|
|
91 |
% However, this is a subtle issue that we cannot discuss here further.
|
|
92 |
|
|
93 |
\begin{warn}
|
11456
|
94 |
As we have indicated, the requirement for total functions is an essential characteristic of HOL\@. It is only
|
8749
|
95 |
because of totality that reasoning in HOL is comparatively easy. More
|
11456
|
96 |
generally, the philosophy in HOL is to refrain from asserting arbitrary axioms (such as
|
8749
|
97 |
function definitions whose totality has not been proved) because they
|
|
98 |
quickly lead to inconsistencies. Instead, fixed constructs for introducing
|
|
99 |
types and functions are offered (such as \isacommand{datatype} and
|
|
100 |
\isacommand{primrec}) which are guaranteed to preserve consistency.
|
|
101 |
\end{warn}
|
|
102 |
|
11456
|
103 |
\index{syntax}%
|
8749
|
104 |
A remark about syntax. The textual definition of a theory follows a fixed
|
10971
|
105 |
syntax with keywords like \isacommand{datatype} and \isacommand{end}.
|
|
106 |
% (see Fig.~\ref{fig:keywords} in Appendix~\ref{sec:Appendix} for a full list).
|
8749
|
107 |
Embedded in this syntax are the types and formulae of HOL, whose syntax is
|
12627
|
108 |
extensible (see \S\ref{sec:concrete-syntax}), e.g.\ by new user-defined infix operators.
|
10971
|
109 |
To distinguish the two levels, everything
|
8749
|
110 |
HOL-specific (terms and types) should be enclosed in
|
|
111 |
\texttt{"}\dots\texttt{"}.
|
|
112 |
To lessen this burden, quotation marks around a single identifier can be
|
|
113 |
dropped, unless the identifier happens to be a keyword, as in%
|
|
114 |
\end{isamarkuptext}%
|
11866
|
115 |
\isamarkuptrue%
|
|
116 |
\isacommand{consts}\ {\isachardoublequote}end{\isachardoublequote}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequote}\isamarkupfalse%
|
|
117 |
%
|
8749
|
118 |
\begin{isamarkuptext}%
|
|
119 |
\noindent
|
|
120 |
When Isabelle prints a syntax error message, it refers to the HOL syntax as
|
11456
|
121 |
the \textbf{inner syntax} and the enclosing theory language as the \textbf{outer syntax}.
|
8749
|
122 |
|
|
123 |
|
10878
|
124 |
\section{An Introductory Proof}
|
8749
|
125 |
\label{sec:intro-proof}
|
|
126 |
|
|
127 |
Assuming you have input the declarations and definitions of \texttt{ToyList}
|
|
128 |
presented so far, we are ready to prove a few simple theorems. This will
|
|
129 |
illustrate not just the basic proof commands but also the typical proof
|
|
130 |
process.
|
|
131 |
|
11457
|
132 |
\subsubsection*{Main Goal.}
|
8749
|
133 |
|
|
134 |
Our goal is to show that reversing a list twice produces the original
|
11456
|
135 |
list.%
|
8749
|
136 |
\end{isamarkuptext}%
|
11866
|
137 |
\isamarkuptrue%
|
|
138 |
\isacommand{theorem}\ rev{\isacharunderscore}rev\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}rev{\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isachardoublequote}\isamarkupfalse%
|
|
139 |
\isamarkuptrue%
|
15481
|
140 |
\isamarkupfalse%
|
11866
|
141 |
\isamarkuptrue%
|
15481
|
142 |
\isamarkupfalse%
|
11866
|
143 |
\isamarkuptrue%
|
15614
|
144 |
\isanewline
|
11866
|
145 |
\isamarkupfalse%
|
8749
|
146 |
%
|
11428
|
147 |
\isamarkupsubsubsection{First Lemma%
|
10395
|
148 |
}
|
11866
|
149 |
\isamarkuptrue%
|
9723
|
150 |
%
|
8749
|
151 |
\begin{isamarkuptext}%
|
11428
|
152 |
\indexbold{abandoning a proof}\indexbold{proofs!abandoning}
|
|
153 |
After abandoning the above proof attempt (at the shell level type
|
|
154 |
\commdx{oops}) we start a new proof:%
|
8749
|
155 |
\end{isamarkuptext}%
|
11866
|
156 |
\isamarkuptrue%
|
|
157 |
\isacommand{lemma}\ rev{\isacharunderscore}app\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}rev{\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}rev\ ys{\isacharparenright}\ {\isacharat}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
158 |
\isamarkuptrue%
|
15481
|
159 |
\isamarkupfalse%
|
11866
|
160 |
\isamarkuptrue%
|
15481
|
161 |
\isamarkupfalse%
|
11866
|
162 |
\isamarkuptrue%
|
15614
|
163 |
\isanewline
|
11866
|
164 |
\isamarkupfalse%
|
8749
|
165 |
%
|
11428
|
166 |
\isamarkupsubsubsection{Second Lemma%
|
10395
|
167 |
}
|
11866
|
168 |
\isamarkuptrue%
|
9723
|
169 |
%
|
8749
|
170 |
\begin{isamarkuptext}%
|
11456
|
171 |
We again try the canonical proof procedure:%
|
8749
|
172 |
\end{isamarkuptext}%
|
11866
|
173 |
\isamarkuptrue%
|
10187
|
174 |
\isacommand{lemma}\ app{\isacharunderscore}Nil{\isadigit{2}}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}xs\ {\isacharat}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequote}\isanewline
|
11866
|
175 |
\isamarkupfalse%
|
15481
|
176 |
\isamarkupfalse%
|
11866
|
177 |
\isamarkupfalse%
|
|
178 |
\isamarkuptrue%
|
15481
|
179 |
\isamarkupfalse%
|
11866
|
180 |
%
|
8749
|
181 |
\begin{isamarkuptext}%
|
11428
|
182 |
\noindent
|
|
183 |
As a result of that final \commdx{done}, Isabelle associates the lemma just proved
|
10171
|
184 |
with its name. In this tutorial, we sometimes omit to show that final \isacommand{done}
|
|
185 |
if it is obvious from the context that the proof is finished.
|
|
186 |
|
|
187 |
% Instead of \isacommand{apply} followed by a dot, you can simply write
|
|
188 |
% \isacommand{by}\indexbold{by}, which we do most of the time.
|
10971
|
189 |
Notice that in lemma \isa{app{\isacharunderscore}Nil{\isadigit{2}}},
|
|
190 |
as printed out after the final \isacommand{done}, the free variable \isa{xs} has been
|
9792
|
191 |
replaced by the unknown \isa{{\isacharquery}xs}, just as explained in
|
|
192 |
\S\ref{sec:variables}.
|
8749
|
193 |
|
|
194 |
Going back to the proof of the first lemma%
|
|
195 |
\end{isamarkuptext}%
|
11866
|
196 |
\isamarkuptrue%
|
9674
|
197 |
\isacommand{lemma}\ rev{\isacharunderscore}app\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}rev{\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}rev\ ys{\isacharparenright}\ {\isacharat}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isachardoublequote}\isanewline
|
11866
|
198 |
\isamarkupfalse%
|
|
199 |
\isamarkupfalse%
|
15481
|
200 |
\isamarkupfalse%
|
11866
|
201 |
\isamarkuptrue%
|
|
202 |
\isamarkupfalse%
|
8749
|
203 |
%
|
11456
|
204 |
\isamarkupsubsubsection{Third Lemma%
|
10395
|
205 |
}
|
11866
|
206 |
\isamarkuptrue%
|
9723
|
207 |
%
|
|
208 |
\begin{isamarkuptext}%
|
11456
|
209 |
Abandoning the previous attempt, the canonical proof procedure
|
|
210 |
succeeds without further ado.%
|
9723
|
211 |
\end{isamarkuptext}%
|
11866
|
212 |
\isamarkuptrue%
|
9674
|
213 |
\isacommand{lemma}\ app{\isacharunderscore}assoc\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ {\isacharat}\ zs\ {\isacharequal}\ xs\ {\isacharat}\ {\isacharparenleft}ys\ {\isacharat}\ zs{\isacharparenright}{\isachardoublequote}\isanewline
|
11866
|
214 |
\isamarkupfalse%
|
15481
|
215 |
\isamarkupfalse%
|
11866
|
216 |
\isamarkupfalse%
|
|
217 |
\isamarkupfalse%
|
|
218 |
%
|
8749
|
219 |
\begin{isamarkuptext}%
|
|
220 |
\noindent
|
11456
|
221 |
Now we can prove the first lemma:%
|
8749
|
222 |
\end{isamarkuptext}%
|
11866
|
223 |
\isamarkuptrue%
|
9674
|
224 |
\isacommand{lemma}\ rev{\isacharunderscore}app\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}rev{\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}rev\ ys{\isacharparenright}\ {\isacharat}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isachardoublequote}\isanewline
|
11866
|
225 |
\isamarkupfalse%
|
15481
|
226 |
\isamarkupfalse%
|
11866
|
227 |
\isamarkupfalse%
|
|
228 |
\isamarkupfalse%
|
|
229 |
%
|
8749
|
230 |
\begin{isamarkuptext}%
|
|
231 |
\noindent
|
11456
|
232 |
Finally, we prove our main theorem:%
|
8749
|
233 |
\end{isamarkuptext}%
|
11866
|
234 |
\isamarkuptrue%
|
9674
|
235 |
\isacommand{theorem}\ rev{\isacharunderscore}rev\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}rev{\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isachardoublequote}\isanewline
|
11866
|
236 |
\isamarkupfalse%
|
15481
|
237 |
\isamarkupfalse%
|
11866
|
238 |
\isamarkupfalse%
|
|
239 |
\isamarkupfalse%
|
|
240 |
%
|
8749
|
241 |
\begin{isamarkuptext}%
|
|
242 |
\noindent
|
11456
|
243 |
The final \commdx{end} tells Isabelle to close the current theory because
|
8749
|
244 |
we are finished with its development:%
|
11456
|
245 |
\index{*rev (constant)|)}\index{append function|)}%
|
8749
|
246 |
\end{isamarkuptext}%
|
11866
|
247 |
\isamarkuptrue%
|
8749
|
248 |
\isacommand{end}\isanewline
|
11866
|
249 |
\isamarkupfalse%
|
9722
|
250 |
\end{isabellebody}%
|
9145
|
251 |
%%% Local Variables:
|
|
252 |
%%% mode: latex
|
|
253 |
%%% TeX-master: "root"
|
|
254 |
%%% End:
|