8743
|
1 |
\chapter{Basic Concepts}
|
|
2 |
|
|
3 |
\section{Introduction}
|
|
4 |
|
11405
|
5 |
This book is a tutorial on how to use the theorem prover Isabelle/HOL as a
|
|
6 |
specification and verification system. Isabelle is a generic system for
|
|
7 |
implementing logical formalisms, and Isabelle/HOL is the specialization
|
|
8 |
of Isabelle for HOL, which abbreviates Higher-Order Logic. We introduce
|
|
9 |
HOL step by step following the equation
|
8743
|
10 |
\[ \mbox{HOL} = \mbox{Functional Programming} + \mbox{Logic}. \]
|
11456
|
11 |
We do not assume that you are familiar with mathematical logic.
|
|
12 |
However, we do assume that
|
|
13 |
you are used to logical and set theoretic notation, as covered
|
|
14 |
in a good discrete mathematics course~\cite{Rosen-DMA}, and
|
11450
|
15 |
that you are familiar with the basic concepts of functional
|
11209
|
16 |
programming~\cite{Bird-Haskell,Hudak-Haskell,paulson-ml2,Thompson-Haskell}.
|
|
17 |
Although this tutorial initially concentrates on functional programming, do
|
|
18 |
not be misled: HOL can express most mathematical concepts, and functional
|
|
19 |
programming is just one particularly simple and ubiquitous instance.
|
8743
|
20 |
|
11205
|
21 |
Isabelle~\cite{paulson-isa-book} is implemented in ML~\cite{SML}. This has
|
|
22 |
influenced some of Isabelle/HOL's concrete syntax but is otherwise irrelevant
|
11450
|
23 |
for us: this tutorial is based on
|
11213
|
24 |
Isabelle/Isar~\cite{isabelle-isar-ref}, an extension of Isabelle which hides
|
|
25 |
the implementation language almost completely. Thus the full name of the
|
|
26 |
system should be Isabelle/Isar/HOL, but that is a bit of a mouthful.
|
|
27 |
|
|
28 |
There are other implementations of HOL, in particular the one by Mike Gordon
|
11450
|
29 |
\index{Gordon, Mike}%
|
11213
|
30 |
\emph{et al.}, which is usually referred to as ``the HOL system''
|
|
31 |
\cite{mgordon-hol}. For us, HOL refers to the logical system, and sometimes
|
11450
|
32 |
its incarnation Isabelle/HOL\@.
|
8743
|
33 |
|
|
34 |
A tutorial is by definition incomplete. Currently the tutorial only
|
|
35 |
introduces the rudiments of Isar's proof language. To fully exploit the power
|
11213
|
36 |
of Isar, in particular the ability to write readable and structured proofs,
|
|
37 |
you need to consult the Isabelle/Isar Reference
|
8743
|
38 |
Manual~\cite{isabelle-isar-ref}. If you want to use Isabelle's ML level
|
|
39 |
directly (for example for writing your own proof procedures) see the Isabelle
|
|
40 |
Reference Manual~\cite{isabelle-ref}; for details relating to HOL see the
|
|
41 |
Isabelle/HOL manual~\cite{isabelle-HOL}. All manuals have a comprehensive
|
|
42 |
index.
|
|
43 |
|
|
44 |
\section{Theories}
|
|
45 |
\label{sec:Basic:Theories}
|
|
46 |
|
11428
|
47 |
\index{theories|(}%
|
8743
|
48 |
Working with Isabelle means creating theories. Roughly speaking, a
|
11428
|
49 |
\textbf{theory} is a named collection of types, functions, and theorems,
|
8743
|
50 |
much like a module in a programming language or a specification in a
|
|
51 |
specification language. In fact, theories in HOL can be either. The general
|
|
52 |
format of a theory \texttt{T} is
|
|
53 |
\begin{ttbox}
|
|
54 |
theory T = B\(@1\) + \(\cdots\) + B\(@n\):
|
11450
|
55 |
{\rmfamily\textit{declarations, definitions, and proofs}}
|
8743
|
56 |
end
|
|
57 |
\end{ttbox}
|
|
58 |
where \texttt{B}$@1$, \dots, \texttt{B}$@n$ are the names of existing
|
11450
|
59 |
theories that \texttt{T} is based on and \textit{declarations,
|
|
60 |
definitions, and proofs} represents the newly introduced concepts
|
8771
|
61 |
(types, functions etc.) and proofs about them. The \texttt{B}$@i$ are the
|
11450
|
62 |
direct \textbf{parent theories}\indexbold{parent theories} of~\texttt{T}\@.
|
|
63 |
Everything defined in the parent theories (and their parents, recursively) is
|
8743
|
64 |
automatically visible. To avoid name clashes, identifiers can be
|
11450
|
65 |
\textbf{qualified}\indexbold{identifiers!qualified}
|
|
66 |
by theory names as in \texttt{T.f} and~\texttt{B.f}.
|
|
67 |
Each theory \texttt{T} must
|
11428
|
68 |
reside in a \textbf{theory file}\index{theory files} named \texttt{T.thy}.
|
8743
|
69 |
|
|
70 |
This tutorial is concerned with introducing you to the different linguistic
|
11450
|
71 |
constructs that can fill the \textit{declarations, definitions, and
|
|
72 |
proofs} above. A complete grammar of the basic
|
8743
|
73 |
constructs is found in the Isabelle/Isar Reference Manual.
|
|
74 |
|
10885
|
75 |
HOL's theory collection is available online at
|
8743
|
76 |
\begin{center}\small
|
10978
|
77 |
\url{http://isabelle.in.tum.de/library/HOL/}
|
8743
|
78 |
\end{center}
|
10885
|
79 |
and is recommended browsing. Note that most of the theories
|
9541
|
80 |
are based on classical Isabelle without the Isar extension. This means that
|
|
81 |
they look slightly different than the theories in this tutorial, and that all
|
|
82 |
proofs are in separate ML files.
|
|
83 |
|
8743
|
84 |
\begin{warn}
|
11428
|
85 |
HOL contains a theory \thydx{Main}, the union of all the basic
|
10885
|
86 |
predefined theories like arithmetic, lists, sets, etc.
|
|
87 |
Unless you know what you are doing, always include \isa{Main}
|
10971
|
88 |
as a direct or indirect parent of all your theories.
|
11428
|
89 |
\end{warn}%
|
|
90 |
\index{theories|)}
|
8743
|
91 |
|
|
92 |
|
10885
|
93 |
\section{Types, Terms and Formulae}
|
8743
|
94 |
\label{sec:TypesTermsForms}
|
|
95 |
|
10795
|
96 |
Embedded in a theory are the types, terms and formulae of HOL\@. HOL is a typed
|
8771
|
97 |
logic whose type system resembles that of functional programming languages
|
11450
|
98 |
like ML or Haskell. Thus there are
|
|
99 |
\index{types|(}
|
8743
|
100 |
\begin{description}
|
11450
|
101 |
\item[base types,]
|
|
102 |
in particular \tydx{bool}, the type of truth values,
|
11428
|
103 |
and \tydx{nat}, the type of natural numbers.
|
11450
|
104 |
\item[type constructors,]\index{type constructors}
|
|
105 |
in particular \tydx{list}, the type of
|
11428
|
106 |
lists, and \tydx{set}, the type of sets. Type constructors are written
|
8771
|
107 |
postfix, e.g.\ \isa{(nat)list} is the type of lists whose elements are
|
8743
|
108 |
natural numbers. Parentheses around single arguments can be dropped (as in
|
8771
|
109 |
\isa{nat list}), multiple arguments are separated by commas (as in
|
|
110 |
\isa{(bool,nat)ty}).
|
11450
|
111 |
\item[function types,]\index{function types}
|
|
112 |
denoted by \isasymFun\indexbold{$IsaFun@\isasymFun}.
|
8771
|
113 |
In HOL \isasymFun\ represents \emph{total} functions only. As is customary,
|
|
114 |
\isa{$\tau@1$ \isasymFun~$\tau@2$ \isasymFun~$\tau@3$} means
|
|
115 |
\isa{$\tau@1$ \isasymFun~($\tau@2$ \isasymFun~$\tau@3$)}. Isabelle also
|
|
116 |
supports the notation \isa{[$\tau@1,\dots,\tau@n$] \isasymFun~$\tau$}
|
|
117 |
which abbreviates \isa{$\tau@1$ \isasymFun~$\cdots$ \isasymFun~$\tau@n$
|
8743
|
118 |
\isasymFun~$\tau$}.
|
11450
|
119 |
\item[type variables,]\index{type variables}\index{variables!type}
|
10795
|
120 |
denoted by \ttindexboldpos{'a}{$Isatype}, \isa{'b} etc., just like in ML\@. They give rise
|
8771
|
121 |
to polymorphic types like \isa{'a \isasymFun~'a}, the type of the identity
|
|
122 |
function.
|
8743
|
123 |
\end{description}
|
|
124 |
\begin{warn}
|
|
125 |
Types are extremely important because they prevent us from writing
|
|
126 |
nonsense. Isabelle insists that all terms and formulae must be well-typed
|
|
127 |
and will print an error message if a type mismatch is encountered. To
|
|
128 |
reduce the amount of explicit type information that needs to be provided by
|
|
129 |
the user, Isabelle infers the type of all variables automatically (this is
|
|
130 |
called \bfindex{type inference}) and keeps quiet about it. Occasionally
|
|
131 |
this may lead to misunderstandings between you and the system. If anything
|
11428
|
132 |
strange happens, we recommend that you set the flag\index{flags}
|
|
133 |
\isa{show_types}\index{*show_types (flag)}.
|
|
134 |
Isabelle will then display type information
|
11450
|
135 |
that is usually suppressed. Simply type
|
8743
|
136 |
\begin{ttbox}
|
|
137 |
ML "set show_types"
|
|
138 |
\end{ttbox}
|
|
139 |
|
|
140 |
\noindent
|
10971
|
141 |
This can be reversed by \texttt{ML "reset show_types"}. Various other flags,
|
11428
|
142 |
which we introduce as we go along, can be set and reset in the same manner.%
|
|
143 |
\index{flags!setting and resetting}
|
11450
|
144 |
\end{warn}%
|
|
145 |
\index{types|)}
|
8743
|
146 |
|
|
147 |
|
11450
|
148 |
\index{terms|(}
|
|
149 |
\textbf{Terms} are formed as in functional programming by
|
8771
|
150 |
applying functions to arguments. If \isa{f} is a function of type
|
|
151 |
\isa{$\tau@1$ \isasymFun~$\tau@2$} and \isa{t} is a term of type
|
|
152 |
$\tau@1$ then \isa{f~t} is a term of type $\tau@2$. HOL also supports
|
|
153 |
infix functions like \isa{+} and some basic constructs from functional
|
11428
|
154 |
programming, such as conditional expressions:
|
8743
|
155 |
\begin{description}
|
11450
|
156 |
\item[\isa{if $b$ then $t@1$ else $t@2$}]\index{*if expressions}
|
11428
|
157 |
Here $b$ is of type \isa{bool} and $t@1$ and $t@2$ are of the same type.
|
11450
|
158 |
\item[\isa{let $x$ = $t$ in $u$}]\index{*let expressions}
|
8743
|
159 |
is equivalent to $u$ where all occurrences of $x$ have been replaced by
|
|
160 |
$t$. For example,
|
8771
|
161 |
\isa{let x = 0 in x+x} is equivalent to \isa{0+0}. Multiple bindings are separated
|
|
162 |
by semicolons: \isa{let $x@1$ = $t@1$; \dots; $x@n$ = $t@n$ in $u$}.
|
|
163 |
\item[\isa{case $e$ of $c@1$ \isasymFun~$e@1$ |~\dots~| $c@n$ \isasymFun~$e@n$}]
|
11450
|
164 |
\index{*case expressions}
|
8771
|
165 |
evaluates to $e@i$ if $e$ is of the form $c@i$.
|
8743
|
166 |
\end{description}
|
|
167 |
|
|
168 |
Terms may also contain
|
11450
|
169 |
\isasymlambda-abstractions.\index{lambda@$\lambda$ expressions}
|
|
170 |
For example,
|
8771
|
171 |
\isa{\isasymlambda{}x.~x+1} is the function that takes an argument \isa{x} and
|
|
172 |
returns \isa{x+1}. Instead of
|
|
173 |
\isa{\isasymlambda{}x.\isasymlambda{}y.\isasymlambda{}z.~$t$} we can write
|
11450
|
174 |
\isa{\isasymlambda{}x~y~z.~$t$}.%
|
|
175 |
\index{terms|)}
|
8743
|
176 |
|
11450
|
177 |
\index{formulae|(}%
|
|
178 |
\textbf{Formulae} are terms of type \tydx{bool}.
|
11428
|
179 |
There are the basic constants \cdx{True} and \cdx{False} and
|
8771
|
180 |
the usual logical connectives (in decreasing order of priority):
|
11420
|
181 |
\indexboldpos{\protect\isasymnot}{$HOL0not}, \indexboldpos{\protect\isasymand}{$HOL0and},
|
|
182 |
\indexboldpos{\protect\isasymor}{$HOL0or}, and \indexboldpos{\protect\isasymimp}{$HOL0imp},
|
8743
|
183 |
all of which (except the unary \isasymnot) associate to the right. In
|
8771
|
184 |
particular \isa{A \isasymimp~B \isasymimp~C} means \isa{A \isasymimp~(B
|
|
185 |
\isasymimp~C)} and is thus logically equivalent to \isa{A \isasymand~B
|
|
186 |
\isasymimp~C} (which is \isa{(A \isasymand~B) \isasymimp~C}).
|
8743
|
187 |
|
11450
|
188 |
Equality\index{equality} is available in the form of the infix function
|
|
189 |
\isa{=} of type \isa{'a \isasymFun~'a
|
8771
|
190 |
\isasymFun~bool}. Thus \isa{$t@1$ = $t@2$} is a formula provided $t@1$
|
11450
|
191 |
and $t@2$ are terms of the same type. If $t@1$ and $t@2$ are of type
|
|
192 |
\isa{bool} then \isa{=} acts as \rmindex{if-and-only-if}.
|
|
193 |
The formula
|
8771
|
194 |
\isa{$t@1$~\isasymnoteq~$t@2$} is merely an abbreviation for
|
|
195 |
\isa{\isasymnot($t@1$ = $t@2$)}.
|
8743
|
196 |
|
11450
|
197 |
Quantifiers\index{quantifiers} are written as
|
|
198 |
\isa{\isasymforall{}x.~$P$} and \isa{\isasymexists{}x.~$P$}.
|
11420
|
199 |
There is even
|
11450
|
200 |
\isa{\isasymuniqex{}x.~$P$}, which
|
11420
|
201 |
means that there exists exactly one \isa{x} that satisfies \isa{$P$}.
|
|
202 |
Nested quantifications can be abbreviated:
|
|
203 |
\isa{\isasymforall{}x~y~z.~$P$} means
|
11450
|
204 |
\isa{\isasymforall{}x.\isasymforall{}y.\isasymforall{}z.~$P$}.%
|
|
205 |
\index{formulae|)}
|
8743
|
206 |
|
|
207 |
Despite type inference, it is sometimes necessary to attach explicit
|
11428
|
208 |
\bfindex{type constraints} to a term. The syntax is
|
8771
|
209 |
\isa{$t$::$\tau$} as in \isa{x < (y::nat)}. Note that
|
10538
|
210 |
\ttindexboldpos{::}{$Isatype} binds weakly and should therefore be enclosed
|
11450
|
211 |
in parentheses. For instance,
|
|
212 |
\isa{x < y::nat} is ill-typed because it is interpreted as
|
|
213 |
\isa{(x < y)::nat}. Type constraints may be needed to disambiguate
|
|
214 |
expressions
|
|
215 |
involving overloaded functions such as~\isa{+},
|
|
216 |
\isa{*} and~\isa{<}. Section~\ref{sec:overloading}
|
|
217 |
discusses overloading, while Table~\ref{tab:overloading} presents the most
|
10695
|
218 |
important overloaded function symbols.
|
8743
|
219 |
|
11450
|
220 |
In general, HOL's concrete \rmindex{syntax} tries to follow the conventions of
|
|
221 |
functional programming and mathematics. Here are the main rules that you
|
|
222 |
should be familiar with to avoid certain syntactic traps:
|
8743
|
223 |
\begin{itemize}
|
|
224 |
\item
|
8771
|
225 |
Remember that \isa{f t u} means \isa{(f t) u} and not \isa{f(t u)}!
|
8743
|
226 |
\item
|
8771
|
227 |
Isabelle allows infix functions like \isa{+}. The prefix form of function
|
|
228 |
application binds more strongly than anything else and hence \isa{f~x + y}
|
|
229 |
means \isa{(f~x)~+~y} and not \isa{f(x+y)}.
|
8743
|
230 |
\item Remember that in HOL if-and-only-if is expressed using equality. But
|
|
231 |
equality has a high priority, as befitting a relation, while if-and-only-if
|
8771
|
232 |
typically has the lowest priority. Thus, \isa{\isasymnot~\isasymnot~P =
|
|
233 |
P} means \isa{\isasymnot\isasymnot(P = P)} and not
|
|
234 |
\isa{(\isasymnot\isasymnot P) = P}. When using \isa{=} to mean
|
|
235 |
logical equivalence, enclose both operands in parentheses, as in \isa{(A
|
8743
|
236 |
\isasymand~B) = (B \isasymand~A)}.
|
|
237 |
\item
|
|
238 |
Constructs with an opening but without a closing delimiter bind very weakly
|
|
239 |
and should therefore be enclosed in parentheses if they appear in subterms, as
|
11450
|
240 |
in \isa{(\isasymlambda{}x.~x) = f}. This includes
|
|
241 |
\isa{if},\index{*if expressions}
|
|
242 |
\isa{let},\index{*let expressions}
|
|
243 |
\isa{case},\index{*case expressions}
|
|
244 |
\isa{\isasymlambda}, and quantifiers.
|
8743
|
245 |
\item
|
8771
|
246 |
Never write \isa{\isasymlambda{}x.x} or \isa{\isasymforall{}x.x=x}
|
11450
|
247 |
because \isa{x.x} is always taken as a single qualified identifier that
|
8771
|
248 |
refers to an item \isa{x} in theory \isa{x}. Write
|
|
249 |
\isa{\isasymlambda{}x.~x} and \isa{\isasymforall{}x.~x=x} instead.
|
11450
|
250 |
\item Identifiers\indexbold{identifiers} may contain the characters \isa{_}
|
|
251 |
and~\isa{'}.
|
8743
|
252 |
\end{itemize}
|
|
253 |
|
11450
|
254 |
For the sake of readability, we use the usual mathematical symbols throughout
|
10983
|
255 |
the tutorial. Their \textsc{ascii}-equivalents are shown in table~\ref{tab:ascii} in
|
8771
|
256 |
the appendix.
|
|
257 |
|
11450
|
258 |
\begin{warn}
|
|
259 |
A particular
|
|
260 |
problem for novices can be the priority of operators. If you are unsure, use
|
|
261 |
additional parentheses. In those cases where Isabelle echoes your
|
|
262 |
input, you can see which parentheses are dropped --- they were superfluous. If
|
|
263 |
you are unsure how to interpret Isabelle's output because you don't know
|
|
264 |
where the (dropped) parentheses go, set the flag\index{flags}
|
|
265 |
\isa{show_brackets}\index{*show_brackets (flag)}:
|
|
266 |
\begin{ttbox}
|
|
267 |
ML "set show_brackets"; \(\dots\); ML "reset show_brackets";
|
|
268 |
\end{ttbox}
|
|
269 |
\end{warn}
|
|
270 |
|
8743
|
271 |
|
|
272 |
\section{Variables}
|
|
273 |
\label{sec:variables}
|
11450
|
274 |
\index{variables|(}
|
8743
|
275 |
|
11450
|
276 |
Isabelle distinguishes free and bound variables, as is customary. Bound
|
8743
|
277 |
variables are automatically renamed to avoid clashes with free variables. In
|
11428
|
278 |
addition, Isabelle has a third kind of variable, called a \textbf{schematic
|
|
279 |
variable}\index{variables!schematic} or \textbf{unknown}\index{unknowns},
|
|
280 |
which must a~\isa{?} as its first character.
|
|
281 |
Logically, an unknown is a free variable. But it may be
|
8743
|
282 |
instantiated by another term during the proof process. For example, the
|
8771
|
283 |
mathematical theorem $x = x$ is represented in Isabelle as \isa{?x = ?x},
|
8743
|
284 |
which means that Isabelle can instantiate it arbitrarily. This is in contrast
|
|
285 |
to ordinary variables, which remain fixed. The programming language Prolog
|
|
286 |
calls unknowns {\em logical\/} variables.
|
|
287 |
|
|
288 |
Most of the time you can and should ignore unknowns and work with ordinary
|
|
289 |
variables. Just don't be surprised that after you have finished the proof of
|
11450
|
290 |
a theorem, Isabelle will turn your free variables into unknowns. It
|
8743
|
291 |
indicates that Isabelle will automatically instantiate those unknowns
|
|
292 |
suitably when the theorem is used in some other proof.
|
9689
|
293 |
Note that for readability we often drop the \isa{?}s when displaying a theorem.
|
8743
|
294 |
\begin{warn}
|
11450
|
295 |
For historical reasons, Isabelle accepts \isa{?} as an ASCII representation
|
|
296 |
of the \(\exists\) symbol. However, the \isa{?} character must then be followed
|
|
297 |
by a space, as in \isa{?~x. f(x) = 0}. Otherwise, \isa{?x} is
|
|
298 |
interpreted as a schematic variable. The preferred ASCII representation of
|
|
299 |
the \(\exists\) symbol is \isa{EX}\@.
|
|
300 |
\end{warn}%
|
|
301 |
\index{variables|)}
|
8743
|
302 |
|
10885
|
303 |
\section{Interaction and Interfaces}
|
8771
|
304 |
|
|
305 |
Interaction with Isabelle can either occur at the shell level or through more
|
11301
|
306 |
advanced interfaces. To keep the tutorial independent of the interface, we
|
|
307 |
have phrased the description of the interaction in a neutral language. For
|
8771
|
308 |
example, the phrase ``to abandon a proof'' means to type \isacommand{oops} at the
|
|
309 |
shell level, which is explained the first time the phrase is used. Other
|
|
310 |
interfaces perform the same act by cursor movements and/or mouse clicks.
|
|
311 |
Although shell-based interaction is quite feasible for the kind of proof
|
|
312 |
scripts currently presented in this tutorial, the recommended interface for
|
|
313 |
Isabelle/Isar is the Emacs-based \bfindex{Proof
|
11450
|
314 |
General}~\cite{proofgeneral,Aspinall:TACAS:2000}.
|
8771
|
315 |
|
|
316 |
Some interfaces (including the shell level) offer special fonts with
|
10983
|
317 |
mathematical symbols. For those that do not, remember that \textsc{ascii}-equivalents
|
10978
|
318 |
are shown in table~\ref{tab:ascii} in the appendix.
|
8771
|
319 |
|
9541
|
320 |
Finally, a word about semicolons.\indexbold{$Isar@\texttt{;}}
|
|
321 |
Commands may but need not be terminated by semicolons.
|
|
322 |
At the shell level it is advisable to use semicolons to enforce that a command
|
8771
|
323 |
is executed immediately; otherwise Isabelle may wait for the next keyword
|
9541
|
324 |
before it knows that the command is complete.
|
8771
|
325 |
|
|
326 |
|
10885
|
327 |
\section{Getting Started}
|
8743
|
328 |
|
|
329 |
Assuming you have installed Isabelle, you start it by typing \texttt{isabelle
|
|
330 |
-I HOL} in a shell window.\footnote{Simply executing \texttt{isabelle -I}
|
|
331 |
starts the default logic, which usually is already \texttt{HOL}. This is
|
|
332 |
controlled by the \texttt{ISABELLE_LOGIC} setting, see \emph{The Isabelle
|
|
333 |
System Manual} for more details.} This presents you with Isabelle's most
|
10983
|
334 |
basic \textsc{ascii} interface. In addition you need to open an editor window to
|
11450
|
335 |
create theory files. While you are developing a theory, we recommend that you
|
8743
|
336 |
type each command into the file first and then enter it into Isabelle by
|
|
337 |
copy-and-paste, thus ensuring that you have a complete record of your theory.
|
8771
|
338 |
As mentioned above, Proof General offers a much superior interface.
|
10795
|
339 |
If you have installed Proof General, you can start it by typing \texttt{Isabelle}.
|