doc-src/Tutorial/basics.tex
author nipkow
Wed, 26 Aug 1998 16:57:49 +0200
changeset 5375 1463e182c533
child 6148 d97a944c6ea3
permissions -rw-r--r--
The HOL tutorial.
nipkow@5375
     1
\chapter{Basic Concepts}
nipkow@5375
     2
nipkow@5375
     3
\section{Introduction}
nipkow@5375
     4
nipkow@5375
     5
This is a tutorial on how to use Isabelle/HOL as a specification and
nipkow@5375
     6
verification system. Isabelle is a generic system for implementing logical
nipkow@5375
     7
formalisms, and Isabelle/HOL is the specialization of Isabelle for
nipkow@5375
     8
HOL, which abbreviates Higher-Order Logic. We introduce HOL step by step
nipkow@5375
     9
following the equation
nipkow@5375
    10
\[ \mbox{HOL} = \mbox{Functional Programming} + \mbox{Logic}. \]
nipkow@5375
    11
We assume that the reader is familiar with the basic concepts of both fields.
nipkow@5375
    12
For excellent introductions to functional programming consult the textbooks
nipkow@5375
    13
by Bird and Wadler~\cite{Bird-Wadler} or Paulson~\cite{Paulson-ML}.  Although
nipkow@5375
    14
this tutorial initially concentrates on functional programming, do not be
nipkow@5375
    15
misled: HOL can express most mathematical concepts, and functional
nipkow@5375
    16
programming is just one particularly simple and ubiquitous instance.
nipkow@5375
    17
nipkow@5375
    18
A tutorial is by definition incomplete. To fully exploit the power of the
nipkow@5375
    19
system you need to consult the Isabelle Reference Manual~\cite{Isa-Ref-Man}
nipkow@5375
    20
for details about Isabelle and the HOL chapter of the Logics
nipkow@5375
    21
manual~\cite{Isa-Logics-Man} for details relating to HOL. Both manuals have a
nipkow@5375
    22
comprehensive index.
nipkow@5375
    23
nipkow@5375
    24
\section{Theories, proofs and interaction}
nipkow@5375
    25
\label{sec:Basic:Theories}
nipkow@5375
    26
nipkow@5375
    27
Working with Isabelle means creating two different kinds of documents:
nipkow@5375
    28
theories and proof scripts. Roughly speaking, a \bfindex{theory} is a named
nipkow@5375
    29
collection of types and functions, much like a module in a programming
nipkow@5375
    30
language or a specification in a specification language. In fact, theories in
nipkow@5375
    31
HOL can be either. Theories must reside in files with the suffix
nipkow@5375
    32
\texttt{.thy}. The general format of a theory file \texttt{T.thy} is
nipkow@5375
    33
\begin{ttbox}
nipkow@5375
    34
T = B\(@1\) + \(\cdots\) + B\(@n\) +
nipkow@5375
    35
\({<}declarations{>}\)
nipkow@5375
    36
end
nipkow@5375
    37
\end{ttbox}
nipkow@5375
    38
where \texttt{B}$@1$, \dots, \texttt{B}$@n$ are the names of existing
nipkow@5375
    39
theories that \texttt{T} is based on and ${<}declarations{>}$ stands for the
nipkow@5375
    40
newly introduced concepts (types, functions etc). The \texttt{B}$@i$ are the
nipkow@5375
    41
direct \textbf{parent theories}\indexbold{parent theory} of \texttt{T}.
nipkow@5375
    42
Everything defined in the parent theories (and their parents \dots) is
nipkow@5375
    43
automatically visible. To avoid name clashes, identifiers can be qualified by
nipkow@5375
    44
theory names as in \texttt{T.f} and \texttt{B.f}. HOL's theory library is
nipkow@5375
    45
available online at \verb$http://www.in.tum.de/~isabelle/library/HOL/$ and is
nipkow@5375
    46
recommended browsing.
nipkow@5375
    47
\begin{warn}
nipkow@5375
    48
  HOL contains a theory \ttindexbold{Main}, the union of all the basic
nipkow@5375
    49
  predefined theories like arithmetic, lists, sets, etc.\ (see the online
nipkow@5375
    50
  library).  Unless you know what you are doing, always include \texttt{Main}
nipkow@5375
    51
  as a direct or indirect parent theory of all your theories.
nipkow@5375
    52
\end{warn}
nipkow@5375
    53
nipkow@5375
    54
This tutorial is concerned with introducing you to the different linguistic
nipkow@5375
    55
constructs that can fill ${<}declarations{>}$ in the above theory template.
nipkow@5375
    56
A complete grammar of the basic constructs is found in Appendix~A
nipkow@5375
    57
of~\cite{Isa-Ref-Man}, for reference in times of doubt.
nipkow@5375
    58
nipkow@5375
    59
The tutorial is also concerned with showing you how to prove theorems about
nipkow@5375
    60
the concepts in a theory. This involves invoking predefined theorem proving
nipkow@5375
    61
commands. Because Isabelle is written in the programming language
nipkow@5375
    62
ML,\footnote{Many concepts in HOL and ML are similar. Make sure you do not
nipkow@5375
    63
  confuse the two levels.} interacting with Isabelle means calling ML
nipkow@5375
    64
functions. Hence \bfindex{proof scripts} are sequences of calls to ML
nipkow@5375
    65
functions that perform specific theorem proving tasks. Nevertheless,
nipkow@5375
    66
familiarity with ML is absolutely not required.  All proof scripts for theory
nipkow@5375
    67
\texttt{T} (defined in file \texttt{T.thy}) should be contained in file
nipkow@5375
    68
\texttt{T.ML}. Theory and proof scripts are loaded (and checked!) by calling
nipkow@5375
    69
the ML function \ttindexbold{use_thy}:
nipkow@5375
    70
\begin{ttbox}
nipkow@5375
    71
use_thy "T";
nipkow@5375
    72
\end{ttbox}
nipkow@5375
    73
nipkow@5375
    74
There are more advanced interfaces for Isabelle that hide the ML level from
nipkow@5375
    75
you and replace function calls by menu selection. There is even a special
nipkow@5375
    76
font with mathematical symbols. For details see the Isabelle home page.  This
nipkow@5375
    77
tutorial concentrates on the bare essentials and ignores such niceties.
nipkow@5375
    78
nipkow@5375
    79
\section{Types, terms and formulae}
nipkow@5375
    80
\label{sec:TypesTermsForms}
nipkow@5375
    81
nipkow@5375
    82
Embedded in the declarations of a theory are the types, terms and formulae of
nipkow@5375
    83
HOL. HOL is a typed logic whose type system resembles that of functional
nipkow@5375
    84
programming languages like ML or Haskell. Thus there are
nipkow@5375
    85
\begin{description}
nipkow@5375
    86
\item[base types,] in particular \ttindex{bool}, the type of truth values,
nipkow@5375
    87
and \ttindex{nat}, the type of natural numbers.
nipkow@5375
    88
\item[type constructors,] in particular \ttindex{list}, the type of
nipkow@5375
    89
lists, and \ttindex{set}, the type of sets. Type constructors are written
nipkow@5375
    90
postfix, e.g.\ \texttt{(nat)list} is the type of lists whose elements are
nipkow@5375
    91
natural numbers. Parentheses around single arguments can be dropped (as in
nipkow@5375
    92
\texttt{nat list}), multiple arguments are separated by commas (as in
nipkow@5375
    93
\texttt{(bool,nat)foo}).
nipkow@5375
    94
\item[function types,] denoted by \ttindexbold{=>}. In HOL
nipkow@5375
    95
\texttt{=>} represents {\em total} functions only. As is customary,
nipkow@5375
    96
\texttt{$\tau@1$ => $\tau@2$ => $\tau@3$} means
nipkow@5375
    97
\texttt{$\tau@1$ => ($\tau@2$ => $\tau@3$)}. Isabelle also supports the
nipkow@5375
    98
notation \texttt{[$\tau@1,\dots,\tau@n$] => $\tau$} which abbreviates
nipkow@5375
    99
\texttt{$\tau@1$ => $\cdots$ => $\tau@n$ => $\tau$}.
nipkow@5375
   100
\item[type variables,] denoted by \texttt{'a}, \texttt{'b} etc, just like in
nipkow@5375
   101
ML. They give rise to polymorphic types like \texttt{'a => 'a}, the type of the
nipkow@5375
   102
identity function.
nipkow@5375
   103
\end{description}
nipkow@5375
   104
\begin{warn}
nipkow@5375
   105
  Types are extremely important because they prevent us from writing
nipkow@5375
   106
  nonsense.  Isabelle insists that all terms and formulae must be well-typed
nipkow@5375
   107
  and will print an error message if a type mismatch is encountered. To
nipkow@5375
   108
  reduce the amount of explicit type information that needs to be provided by
nipkow@5375
   109
  the user, Isabelle infers the type of all variables automatically (this is
nipkow@5375
   110
  called \bfindex{type inference}) and keeps quiet about it. Occasionally
nipkow@5375
   111
  this may lead to misunderstandings between you and the system. If anything
nipkow@5375
   112
  strange happens, we recommend to set the flag \ttindexbold{show_types} that
nipkow@5375
   113
  tells Isabelle to display type information that is usually suppressed:
nipkow@5375
   114
  simply type
nipkow@5375
   115
\begin{ttbox}
nipkow@5375
   116
set show_types;
nipkow@5375
   117
\end{ttbox}
nipkow@5375
   118
nipkow@5375
   119
\noindent
nipkow@5375
   120
at the ML-level. This can be reversed by \texttt{reset show_types;}.
nipkow@5375
   121
\end{warn}
nipkow@5375
   122
nipkow@5375
   123
nipkow@5375
   124
\textbf{Terms}\indexbold{term}
nipkow@5375
   125
are formed as in functional programming by applying functions to
nipkow@5375
   126
arguments. If \texttt{f} is a function of type \texttt{$\tau@1$ => $\tau@2$}
nipkow@5375
   127
and \texttt{t} is a term of type $\tau@1$ then \texttt{f~t} is a term of type
nipkow@5375
   128
$\tau@2$. HOL also supports infix functions like \texttt{+} and some basic
nipkow@5375
   129
constructs from functional programming:
nipkow@5375
   130
\begin{description}
nipkow@5375
   131
\item[\texttt{if $b$ then $t@1$ else $t@2$}]\indexbold{*if}
nipkow@5375
   132
means what you think it means and requires that
nipkow@5375
   133
$b$ is of type \texttt{bool} and $t@1$ and $t@2$ are of the same type.
nipkow@5375
   134
\item[\texttt{let $x$ = $t$ in $u$}]\indexbold{*let}
nipkow@5375
   135
is equivalent to $u$ where all occurrences of $x$ have been replaced by
nipkow@5375
   136
$t$. For example,
nipkow@5375
   137
\texttt{let x = 0 in x+x} means \texttt{0+0}. Multiple bindings are separated
nipkow@5375
   138
by semicolons: \texttt{let $x@1$ = $t@1$; \dots; $x@n$ = $t@n$ in $u$}.
nipkow@5375
   139
\item[\texttt{case $e$ of $c@1$ => $e@1$ | \dots | $c@n$ => $e@n$}]
nipkow@5375
   140
\indexbold{*case}
nipkow@5375
   141
evaluates to $e@i$ if $e$ is of the form
nipkow@5375
   142
$c@i$. See~\S\ref{sec:case-expressions} for details.
nipkow@5375
   143
\end{description}
nipkow@5375
   144
nipkow@5375
   145
Terms may also contain $\lambda$-abstractions. For example, $\lambda
nipkow@5375
   146
x. x+1$ is the function that takes an argument $x$ and returns $x+1$. In
nipkow@5375
   147
Isabelle we write \texttt{\%x.~x+1}.\index{==>@{\tt\%}|bold}
nipkow@5375
   148
Instead of \texttt{\%x.~\%y.~\%z.~t} we can write \texttt{\%x~y~z.~t}.
nipkow@5375
   149
nipkow@5375
   150
\textbf{Formulae}\indexbold{formula}
nipkow@5375
   151
are terms of type \texttt{bool}. There are the basic
nipkow@5375
   152
constants \ttindexbold{True} and \ttindexbold{False} and the usual logical
nipkow@5375
   153
connectives (in decreasing order of priority):
nipkow@5375
   154
\verb$~$\index{$HOL1@{\ttnot}|bold} (`not'),
nipkow@5375
   155
\texttt{\&}\index{$HOL2@{\tt\&}|bold} (`and'),
nipkow@5375
   156
\texttt{|}\index{$HOL2@{\ttor}|bold} (`or') and
nipkow@5375
   157
\texttt{-->}\index{$HOL2@{\tt-->}|bold} (`implies'),
nipkow@5375
   158
all of which (except the unary \verb$~$) associate to the right. In
nipkow@5375
   159
particular \texttt{A --> B --> C} means \texttt{A --> (B --> C)} and is thus
nipkow@5375
   160
logically equivalent with \texttt{A \& B --> C}
nipkow@5375
   161
(which is \texttt{(A \& B) --> C}).
nipkow@5375
   162
nipkow@5375
   163
Equality is available in the form of the infix function
nipkow@5375
   164
\texttt{=} of type \texttt{'a => 'a => bool}. Thus \texttt{$t@1$ = $t@2$} is
nipkow@5375
   165
a formula provided $t@1$ and $t@2$ are terms of the same type. In case $t@1$
nipkow@5375
   166
and $t@2$ are of type \texttt{bool}, \texttt{=} acts as if-and-only-if.
nipkow@5375
   167
nipkow@5375
   168
The syntax for quantifiers is
nipkow@5375
   169
\texttt{!~$x$.$\,P$}\index{$HOLQ@{\ttall}|bold} (`for all $x$') and
nipkow@5375
   170
\texttt{?~$x$.$\,P$}\index{$HOLQ@{\tt?}|bold} (`exists $x$').
nipkow@5375
   171
There is even \texttt{?!~$x$.$\,P$}\index{$HOLQ@{\ttuniquex}|bold}, which
nipkow@5375
   172
means that there exists exactly one $x$ that satisfies $P$. Instead of
nipkow@5375
   173
\texttt{!} and \texttt{?} you may also write \texttt{ALL} and \texttt{EX}.
nipkow@5375
   174
Nested quantifications can be abbreviated:
nipkow@5375
   175
\texttt{!$x~y~z$.$\,P$} means \texttt{!$x$.~!$y$.~!$z$.$\,P$}.
nipkow@5375
   176
nipkow@5375
   177
Despite type inference, it is sometimes necessary to attach explicit
nipkow@5375
   178
\bfindex{type constraints} to a term.  The syntax is \texttt{$t$::$\tau$} as
nipkow@5375
   179
in \texttt{x < (y::nat)}. Note that \texttt{::} binds weakly and should
nipkow@5375
   180
therefore be enclosed in parentheses: \texttt{x < y::nat} is ill-typed
nipkow@5375
   181
because it is interpreted as \texttt{(x < y)::nat}. The main reason for type
nipkow@5375
   182
constraints are overloaded functions like \texttt{+}, \texttt{*} and
nipkow@5375
   183
\texttt{<}. (See \S\ref{sec:TypeClasses} for a full discussion of
nipkow@5375
   184
overloading.)
nipkow@5375
   185
nipkow@5375
   186
\begin{warn}
nipkow@5375
   187
In general, HOL's concrete syntax tries to follow the conventions of
nipkow@5375
   188
functional programming and mathematics. Below we list the main rules that you
nipkow@5375
   189
should be familiar with to avoid certain syntactic traps. A particular
nipkow@5375
   190
problem for novices can be the priority of operators. If you are unsure, use
nipkow@5375
   191
more rather than fewer parentheses. In those cases where Isabelle echoes your
nipkow@5375
   192
input, you can see which parentheses are dropped---they were superfluous. If
nipkow@5375
   193
you are unsure how to interpret Isabelle's output because you don't know
nipkow@5375
   194
where the (dropped) parentheses go, set (and possibly reset) the flag
nipkow@5375
   195
\ttindexbold{show_brackets}:
nipkow@5375
   196
\begin{ttbox}
nipkow@5375
   197
set show_brackets; \(\dots\); reset show_brackets;
nipkow@5375
   198
\end{ttbox}
nipkow@5375
   199
\end{warn}
nipkow@5375
   200
nipkow@5375
   201
\begin{itemize}
nipkow@5375
   202
\item
nipkow@5375
   203
Remember that \texttt{f t u} means \texttt{(f t) u} and not \texttt{f(t u)}!
nipkow@5375
   204
\item
nipkow@5375
   205
Isabelle allows infix functions like \texttt{+}. The prefix form of function
nipkow@5375
   206
application binds more strongly than anything else and hence \texttt{f~x + y}
nipkow@5375
   207
means \texttt{(f~x)~+~y} and not \texttt{f(x+y)}.
nipkow@5375
   208
\item
nipkow@5375
   209
Remember that in HOL if-and-only-if is expressed using equality.  But equality
nipkow@5375
   210
has a high priority, as befitting a  relation, while if-and-only-if typically
nipkow@5375
   211
has the lowest priority.  Thus, \verb$~ ~ P = P$ means \verb$~ ~(P = P)$ and
nipkow@5375
   212
not \verb$(~ ~P) = P$. When using \texttt{=} to mean logical equivalence,
nipkow@5375
   213
enclose both operands in parentheses, as in \texttt{(A \& B) = (B \& A)}.
nipkow@5375
   214
\item
nipkow@5375
   215
Constructs with an opening but without a closing delimiter bind very weakly
nipkow@5375
   216
and should therefore be enclosed in parentheses if they appear in subterms, as
nipkow@5375
   217
in \texttt{f = (\%x.~x)}. This includes
nipkow@5375
   218
\ttindex{if}, \ttindex{let}, \ttindex{case}, \verb$%$ and quantifiers.
nipkow@5375
   219
\item
nipkow@5375
   220
Never write \texttt{\%x.x} or \texttt{!x.x=x} because \texttt{x.x} is always
nipkow@5375
   221
read as a single qualified identifier that refers to an item \texttt{x} in
nipkow@5375
   222
theory \texttt{x}. Write \texttt{\%x.~x} and \texttt{!x.~x=x} instead.
nipkow@5375
   223
\end{itemize}
nipkow@5375
   224
nipkow@5375
   225
\section{Variables}
nipkow@5375
   226
\label{sec:variables}
nipkow@5375
   227
nipkow@5375
   228
Isabelle distinguishes free and bound variables just as is customary. Bound
nipkow@5375
   229
variables are automatically renamed to avoid clashes with free variables. In
nipkow@5375
   230
addition, Isabelle has a third kind of variable, called a \bfindex{schematic
nipkow@5375
   231
  variable} or \bfindex{unknown}, which starts with a \texttt{?}.  Logically,
nipkow@5375
   232
an unknown is a free variable. But it may be instantiated by another term
nipkow@5375
   233
during the proof process. For example, the mathematical theorem $x = x$ is
nipkow@5375
   234
represented in Isabelle as \texttt{?x = ?x}, which means that Isabelle can
nipkow@5375
   235
instantiate it arbitrarily. This is in contrast to ordinary variables, which
nipkow@5375
   236
remain fixed. The programming language Prolog calls unknowns {\em logical\/}
nipkow@5375
   237
variables.
nipkow@5375
   238
nipkow@5375
   239
Most of the time you can and should ignore unknowns and work with ordinary
nipkow@5375
   240
variables. Just don't be surprised that after you have finished the
nipkow@5375
   241
proof of a theorem, Isabelle will turn your free variables into unknowns: it
nipkow@5375
   242
merely indicates that Isabelle will automatically instantiate those unknowns
nipkow@5375
   243
suitably when the theorem is used in some other proof.
nipkow@5375
   244
\begin{warn}
nipkow@5375
   245
  The existential quantifier \texttt{?}\index{$HOLQ@{\tt?}} needs to be
nipkow@5375
   246
  followed by a space. Otherwise \texttt{?x} is interpreted as a schematic
nipkow@5375
   247
  variable.
nipkow@5375
   248
\end{warn}
nipkow@5375
   249
nipkow@5375
   250
\section{Getting started}
nipkow@5375
   251
nipkow@5375
   252
Assuming you have installed Isabelle, you start it by typing \texttt{isabelle
nipkow@5375
   253
  HOL} in a shell window.\footnote{Since you will always want to use HOL when
nipkow@5375
   254
  studying this tutorial, you can set the shell variable
nipkow@5375
   255
  \texttt{ISABELLE_LOGIC} to \texttt{HOL} once and for all and simply execute
nipkow@5375
   256
  \texttt{isabelle}.} This presents you with Isabelle's most basic ASCII
nipkow@5375
   257
interface. In addition you need to open an editor window to create theories
nipkow@5375
   258
(\texttt{.thy} files) and proof scripts (\texttt{.ML} files). While you are
nipkow@5375
   259
developing a proof, we recommend to type each proof command into the ML-file
nipkow@5375
   260
first and then enter it into Isabelle by copy-and-paste, thus ensuring that
nipkow@5375
   261
you have a complete record of your proof.