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