doc-src/TutorialI/fp.tex
author nipkow
Mon Mar 12 18:17:45 2001 +0100 (2001-03-12)
changeset 11201 eddc69b55fac
parent 11159 07b13770c4d6
child 11203 881222d48777
permissions -rw-r--r--
*** empty log message ***
nipkow@8743
     1
\chapter{Functional Programming in HOL}
nipkow@8743
     2
nipkow@8743
     3
Although on the surface this chapter is mainly concerned with how to write
nipkow@10983
     4
functional programs in HOL and how to verify them, most of the constructs and
nipkow@10983
     5
proof procedures introduced are general purpose and recur in any specification
nipkow@10983
     6
or verification task. In fact, it we should really speak of functional
nipkow@10983
     7
\emph{modelling} rather than \emph{programming} because our aim is not
nipkow@10983
     8
primarily to write programs but to design abstract models of systems.  HOL is
nipkow@10983
     9
a specification language that goes well beyond what can be expressed as a
nipkow@10983
    10
program. However, for the time being we concentrate on the computable.
nipkow@8743
    11
nipkow@9541
    12
The dedicated functional programmer should be warned: HOL offers only
nipkow@10983
    13
\emph{total functional programming} --- all functions in HOL must be total,
nipkow@10983
    14
i.e.\ they must terminate for all inputs; lazy data structures are not
nipkow@10983
    15
directly available.
nipkow@8743
    16
paulson@10885
    17
\section{An Introductory Theory}
nipkow@8743
    18
\label{sec:intro-theory}
nipkow@8743
    19
nipkow@8743
    20
Functional programming needs datatypes and functions. Both of them can be
nipkow@8743
    21
defined in a theory with a syntax reminiscent of languages like ML or
nipkow@8743
    22
Haskell. As an example consider the theory in figure~\ref{fig:ToyList}.
nipkow@8743
    23
We will now examine it line by line.
nipkow@8743
    24
nipkow@8743
    25
\begin{figure}[htbp]
nipkow@8743
    26
\begin{ttbox}\makeatother
nipkow@8743
    27
\input{ToyList2/ToyList1}\end{ttbox}
nipkow@8743
    28
\caption{A theory of lists}
nipkow@8743
    29
\label{fig:ToyList}
nipkow@8743
    30
\end{figure}
nipkow@8743
    31
nipkow@8743
    32
{\makeatother\input{ToyList/document/ToyList.tex}}
nipkow@8743
    33
paulson@10795
    34
The complete proof script is shown in Fig.\ts\ref{fig:ToyList-proofs}. The
paulson@10795
    35
concatenation of Figs.\ts\ref{fig:ToyList} and~\ref{fig:ToyList-proofs}
nipkow@8743
    36
constitutes the complete theory \texttt{ToyList} and should reside in file
nipkow@8743
    37
\texttt{ToyList.thy}. It is good practice to present all declarations and
nipkow@8743
    38
definitions at the beginning of a theory to facilitate browsing.
nipkow@8743
    39
nipkow@8743
    40
\begin{figure}[htbp]
nipkow@8743
    41
\begin{ttbox}\makeatother
nipkow@8743
    42
\input{ToyList2/ToyList2}\end{ttbox}
nipkow@8743
    43
\caption{Proofs about lists}
nipkow@8743
    44
\label{fig:ToyList-proofs}
nipkow@8743
    45
\end{figure}
nipkow@8743
    46
nipkow@8743
    47
\subsubsection*{Review}
nipkow@8743
    48
nipkow@8743
    49
This is the end of our toy proof. It should have familiarized you with
nipkow@8743
    50
\begin{itemize}
nipkow@8743
    51
\item the standard theorem proving procedure:
nipkow@8743
    52
state a goal (lemma or theorem); proceed with proof until a separate lemma is
nipkow@8743
    53
required; prove that lemma; come back to the original goal.
nipkow@8743
    54
\item a specific procedure that works well for functional programs:
nipkow@8743
    55
induction followed by all-out simplification via \isa{auto}.
nipkow@8743
    56
\item a basic repertoire of proof commands.
nipkow@8743
    57
\end{itemize}
nipkow@8743
    58
nipkow@8743
    59
paulson@10885
    60
\section{Some Helpful Commands}
nipkow@8743
    61
\label{sec:commands-and-hints}
nipkow@8743
    62
nipkow@8743
    63
This section discusses a few basic commands for manipulating the proof state
nipkow@8743
    64
and can be skipped by casual readers.
nipkow@8743
    65
nipkow@8743
    66
There are two kinds of commands used during a proof: the actual proof
nipkow@8743
    67
commands and auxiliary commands for examining the proof state and controlling
nipkow@8743
    68
the display. Simple proof commands are of the form
nipkow@8743
    69
\isacommand{apply}\isa{(method)}\indexbold{apply} where \bfindex{method} is a
nipkow@8743
    70
synonym for ``theorem proving function''. Typical examples are
nipkow@8743
    71
\isa{induct_tac} and \isa{auto}. Further methods are introduced throughout
nipkow@8743
    72
the tutorial.  Unless stated otherwise you may assume that a method attacks
nipkow@8743
    73
merely the first subgoal. An exception is \isa{auto} which tries to solve all
nipkow@8743
    74
subgoals.
nipkow@8743
    75
nipkow@8743
    76
The most useful auxiliary commands are:
nipkow@8743
    77
\begin{description}
nipkow@8743
    78
\item[Undoing:] \isacommand{undo}\indexbold{*undo} undoes the effect of the
nipkow@8743
    79
  last command; \isacommand{undo} can be undone by
nipkow@8743
    80
  \isacommand{redo}\indexbold{*redo}.  Both are only needed at the shell
nipkow@8743
    81
  level and should not occur in the final theory.
nipkow@8743
    82
\item[Printing the current state:] \isacommand{pr}\indexbold{*pr} redisplays
nipkow@8743
    83
  the current proof state, for example when it has disappeared off the
nipkow@8743
    84
  screen.
nipkow@8743
    85
\item[Limiting the number of subgoals:] \isacommand{pr}~$n$ tells Isabelle to
nipkow@8743
    86
  print only the first $n$ subgoals from now on and redisplays the current
nipkow@8743
    87
  proof state. This is helpful when there are many subgoals.
nipkow@8743
    88
\item[Modifying the order of subgoals:]
nipkow@8743
    89
\isacommand{defer}\indexbold{*defer} moves the first subgoal to the end and
nipkow@8743
    90
\isacommand{prefer}\indexbold{*prefer}~$n$ moves subgoal $n$ to the front.
nipkow@8743
    91
\item[Printing theorems:]
nipkow@8743
    92
  \isacommand{thm}\indexbold{*thm}~\textit{name}$@1$~\dots~\textit{name}$@n$
nipkow@8743
    93
  prints the named theorems.
nipkow@8743
    94
\item[Displaying types:] We have already mentioned the flag
nipkow@8743
    95
  \ttindex{show_types} above. It can also be useful for detecting typos in
nipkow@8743
    96
  formulae early on. For example, if \texttt{show_types} is set and the goal
nipkow@8743
    97
  \isa{rev(rev xs) = xs} is started, Isabelle prints the additional output
nipkow@8743
    98
\par\noindent
nipkow@8743
    99
\begin{isabelle}%
nipkow@8743
   100
Variables:\isanewline
nipkow@8743
   101
~~xs~::~'a~list
nipkow@8743
   102
\end{isabelle}%
nipkow@8743
   103
\par\noindent
nipkow@8743
   104
which tells us that Isabelle has correctly inferred that
nipkow@8743
   105
\isa{xs} is a variable of list type. On the other hand, had we
nipkow@8743
   106
made a typo as in \isa{rev(re xs) = xs}, the response
nipkow@8743
   107
\par\noindent
nipkow@8743
   108
\begin{isabelle}%
nipkow@8743
   109
Variables:\isanewline
nipkow@8743
   110
~~re~::~'a~list~{\isasymRightarrow}~'a~list\isanewline
nipkow@8743
   111
~~xs~::~'a~list%
nipkow@8743
   112
\end{isabelle}%
nipkow@8743
   113
\par\noindent
nipkow@8743
   114
would have alerted us because of the unexpected variable \isa{re}.
nipkow@8743
   115
\item[Reading terms and types:] \isacommand{term}\indexbold{*term}
nipkow@8743
   116
  \textit{string} reads, type-checks and prints the given string as a term in
nipkow@8743
   117
  the current context; the inferred type is output as well.
nipkow@8743
   118
  \isacommand{typ}\indexbold{*typ} \textit{string} reads and prints the given
nipkow@8743
   119
  string as a type in the current context.
nipkow@8743
   120
\item[(Re)loading theories:] When you start your interaction you must open a
nipkow@8771
   121
  named theory with the line \isa{\isacommand{theory}~T~=~\dots~:}. Isabelle
nipkow@8771
   122
  automatically loads all the required parent theories from their respective
nipkow@8771
   123
  files (which may take a moment, unless the theories are already loaded and
nipkow@9541
   124
  the files have not been modified).
nipkow@8743
   125
  
nipkow@8743
   126
  If you suddenly discover that you need to modify a parent theory of your
nipkow@10971
   127
  current theory, you must first abandon your current theory\indexbold{abandon
nipkow@9494
   128
  theory}\indexbold{theory!abandon} (at the shell
nipkow@8743
   129
  level type \isacommand{kill}\indexbold{*kill}). After the parent theory has
nipkow@10971
   130
  been modified, you go back to your original theory. When its first line
nipkow@10971
   131
  \isa{\isacommand{theory}~T~=~\dots~:} is processed, the
nipkow@8743
   132
  modified parent is reloaded automatically.
nipkow@9541
   133
  
nipkow@10978
   134
%  The only time when you need to load a theory by hand is when you simply
nipkow@10978
   135
%  want to check if it loads successfully without wanting to make use of the
nipkow@10978
   136
%  theory itself. This you can do by typing
nipkow@10978
   137
%  \isa{\isacommand{use\_thy}\indexbold{*use_thy}~"T"}.
nipkow@8743
   138
\end{description}
nipkow@8743
   139
Further commands are found in the Isabelle/Isar Reference Manual.
nipkow@8743
   140
nipkow@8771
   141
We now examine Isabelle's functional programming constructs systematically,
nipkow@8771
   142
starting with inductive datatypes.
nipkow@8771
   143
nipkow@8743
   144
nipkow@8743
   145
\section{Datatypes}
nipkow@8743
   146
\label{sec:datatype}
nipkow@8743
   147
nipkow@8743
   148
Inductive datatypes are part of almost every non-trivial application of HOL.
nipkow@8743
   149
First we take another look at a very important example, the datatype of
nipkow@8743
   150
lists, before we turn to datatypes in general. The section closes with a
nipkow@8743
   151
case study.
nipkow@8743
   152
nipkow@8743
   153
nipkow@8743
   154
\subsection{Lists}
nipkow@8743
   155
\indexbold{*list}
nipkow@8743
   156
nipkow@8743
   157
Lists are one of the essential datatypes in computing. Readers of this
nipkow@8743
   158
tutorial and users of HOL need to be familiar with their basic operations.
nipkow@8771
   159
Theory \isa{ToyList} is only a small fragment of HOL's predefined theory
nipkow@8771
   160
\isa{List}\footnote{\url{http://isabelle.in.tum.de/library/HOL/List.html}}.
nipkow@8743
   161
The latter contains many further operations. For example, the functions
nipkow@8771
   162
\isaindexbold{hd} (``head'') and \isaindexbold{tl} (``tail'') return the first
nipkow@8743
   163
element and the remainder of a list. (However, pattern-matching is usually
paulson@10795
   164
preferable to \isa{hd} and \isa{tl}.)  
nipkow@10800
   165
Also available are higher-order functions like \isa{map} and \isa{filter}.
paulson@10795
   166
Theory \isa{List} also contains
nipkow@8743
   167
more syntactic sugar: \isa{[}$x@1$\isa{,}\dots\isa{,}$x@n$\isa{]} abbreviates
nipkow@8743
   168
$x@1$\isa{\#}\dots\isa{\#}$x@n$\isa{\#[]}.  In the rest of the tutorial we
nipkow@8743
   169
always use HOL's predefined lists.
nipkow@8743
   170
nipkow@8743
   171
paulson@10885
   172
\subsection{The General Format}
nipkow@8743
   173
\label{sec:general-datatype}
nipkow@8743
   174
nipkow@8743
   175
The general HOL \isacommand{datatype} definition is of the form
nipkow@8743
   176
\[
nipkow@8743
   177
\isacommand{datatype}~(\alpha@1, \dots, \alpha@n) \, t ~=~
nipkow@8743
   178
C@1~\tau@{11}~\dots~\tau@{1k@1} ~\mid~ \dots ~\mid~
nipkow@8743
   179
C@m~\tau@{m1}~\dots~\tau@{mk@m}
nipkow@8743
   180
\]
nipkow@8771
   181
where $\alpha@i$ are distinct type variables (the parameters), $C@i$ are distinct
nipkow@8743
   182
constructor names and $\tau@{ij}$ are types; it is customary to capitalize
nipkow@8743
   183
the first letter in constructor names. There are a number of
nipkow@8743
   184
restrictions (such as that the type should not be empty) detailed
nipkow@8743
   185
elsewhere~\cite{isabelle-HOL}. Isabelle notifies you if you violate them.
nipkow@8743
   186
nipkow@8743
   187
Laws about datatypes, such as \isa{[] \isasymnoteq~x\#xs} and
nipkow@8743
   188
\isa{(x\#xs = y\#ys) = (x=y \isasymand~xs=ys)}, are used automatically
nipkow@8743
   189
during proofs by simplification.  The same is true for the equations in
nipkow@8743
   190
primitive recursive function definitions.
nipkow@8743
   191
nipkow@9644
   192
Every datatype $t$ comes equipped with a \isa{size} function from $t$ into
nipkow@10538
   193
the natural numbers (see~{\S}\ref{sec:nat} below). For lists, \isa{size} is
nipkow@9644
   194
just the length, i.e.\ \isa{size [] = 0} and \isa{size(x \# xs) = size xs +
nipkow@10237
   195
  1}.  In general, \isaindexbold{size} returns \isa{0} for all constructors
nipkow@10237
   196
that do not have an argument of type $t$, and for all other constructors
nipkow@10237
   197
\isa{1 +} the sum of the sizes of all arguments of type $t$. Note that because
nipkow@9644
   198
\isa{size} is defined on every datatype, it is overloaded; on lists
nipkow@10237
   199
\isa{size} is also called \isaindexbold{length}, which is not overloaded.
paulson@10795
   200
Isabelle will always show \isa{size} on lists as \isa{length}.
nipkow@9644
   201
nipkow@9644
   202
paulson@10885
   203
\subsection{Primitive Recursion}
nipkow@8743
   204
nipkow@8743
   205
Functions on datatypes are usually defined by recursion. In fact, most of the
nipkow@8743
   206
time they are defined by what is called \bfindex{primitive recursion}.
nipkow@8743
   207
The keyword \isacommand{primrec}\indexbold{*primrec} is followed by a list of
nipkow@8743
   208
equations
nipkow@8743
   209
\[ f \, x@1 \, \dots \, (C \, y@1 \, \dots \, y@k)\, \dots \, x@n = r \]
nipkow@8743
   210
such that $C$ is a constructor of the datatype $t$ and all recursive calls of
nipkow@8743
   211
$f$ in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. Thus
nipkow@8743
   212
Isabelle immediately sees that $f$ terminates because one (fixed!) argument
nipkow@10654
   213
becomes smaller with every recursive call. There must be at most one equation
nipkow@8743
   214
for each constructor.  Their order is immaterial.
nipkow@8771
   215
A more general method for defining total recursive functions is introduced in
nipkow@10538
   216
{\S}\ref{sec:recdef}.
nipkow@8743
   217
nipkow@9493
   218
\begin{exercise}\label{ex:Tree}
nipkow@8743
   219
\input{Misc/document/Tree.tex}%
nipkow@8743
   220
\end{exercise}
nipkow@8743
   221
nipkow@9721
   222
\input{Misc/document/case_exprs.tex}
nipkow@8743
   223
nipkow@8743
   224
\input{Ifexpr/document/Ifexpr.tex}
nipkow@8743
   225
paulson@10885
   226
\section{Some Basic Types}
nipkow@8743
   227
nipkow@8743
   228
paulson@10885
   229
\subsection{Natural Numbers}
nipkow@9644
   230
\label{sec:nat}
nipkow@8743
   231
\index{arithmetic|(}
nipkow@8743
   232
nipkow@8743
   233
\input{Misc/document/fakenat.tex}
nipkow@8743
   234
\input{Misc/document/natsum.tex}
nipkow@8743
   235
nipkow@8743
   236
\index{arithmetic|)}
nipkow@8743
   237
nipkow@8743
   238
nipkow@10396
   239
\subsection{Pairs}
nipkow@9541
   240
\input{Misc/document/pairs.tex}
nipkow@8743
   241
nipkow@10608
   242
\subsection{Datatype {\tt\slshape option}}
nipkow@10543
   243
\label{sec:option}
nipkow@10543
   244
\input{Misc/document/Option2.tex}
nipkow@10543
   245
nipkow@8743
   246
\section{Definitions}
nipkow@8743
   247
\label{sec:Definitions}
nipkow@8743
   248
nipkow@8743
   249
A definition is simply an abbreviation, i.e.\ a new name for an existing
nipkow@8743
   250
construction. In particular, definitions cannot be recursive. Isabelle offers
nipkow@8743
   251
definitions on the level of types and terms. Those on the type level are
nipkow@8743
   252
called type synonyms, those on the term level are called (constant)
nipkow@8743
   253
definitions.
nipkow@8743
   254
nipkow@8743
   255
paulson@10885
   256
\subsection{Type Synonyms}
nipkow@8771
   257
\indexbold{type synonym}
nipkow@8743
   258
paulson@10795
   259
Type synonyms are similar to those found in ML\@. Their syntax is fairly self
nipkow@8743
   260
explanatory:
nipkow@8743
   261
nipkow@8743
   262
\input{Misc/document/types.tex}%
nipkow@8743
   263
nipkow@8743
   264
Note that pattern-matching is not allowed, i.e.\ each definition must be of
nipkow@8743
   265
the form $f\,x@1\,\dots\,x@n~\isasymequiv~t$.
nipkow@10538
   266
Section~{\S}\ref{sec:Simplification} explains how definitions are used
nipkow@8743
   267
in proofs.
nipkow@8743
   268
nipkow@9844
   269
\input{Misc/document/prime_def.tex}
nipkow@8743
   270
nipkow@8743
   271
nipkow@11201
   272
\section{The Definitional Approach}
nipkow@11201
   273
\label{sec:definitional}
nipkow@11201
   274
nipkow@11201
   275
As we pointed out at the beginning of the chapter, asserting arbitrary
nipkow@11201
   276
axioms, e.g. $f(n) = f(n) + 1$, may easily lead to contradictions. In order
nipkow@11201
   277
to avoid this danger, this tutorial advocates the definitional rather than
nipkow@11201
   278
the axiomatic approach: introduce new concepts by definitions, thus
nipkow@11201
   279
preserving consistency. However, on the face of it, Isabelle/HOL seems to
nipkow@11201
   280
support many more constructs than just definitions, for example
nipkow@11201
   281
\isacommand{primrec}. The crucial point is that internally, everything
nipkow@11201
   282
(except real axioms) is reduced to a definition. For example, given some
nipkow@11201
   283
\isacommand{primrec} function definition, this is turned into a proper
nipkow@11201
   284
(nonrecursive!) definition, and the user-supplied recursion equations are
nipkow@11201
   285
derived as theorems from the definition. This tricky process is completely
nipkow@11201
   286
hidden from the user and it is not necessary to understand the details. The
nipkow@11201
   287
result of the definitional approach is that \isacommand{primrec} is as safe
nipkow@11201
   288
as pure \isacommand{defs} are, but more convenient. And this is not just the
nipkow@11201
   289
case for \isacommand{primrec} but also for the other commands described
nipkow@11201
   290
later, like \isacommand{recdef} and \isacommand{inductive}.
nipkow@11201
   291
nipkow@8743
   292
\chapter{More Functional Programming}
nipkow@8743
   293
nipkow@8743
   294
The purpose of this chapter is to deepen the reader's understanding of the
nipkow@8771
   295
concepts encountered so far and to introduce advanced forms of datatypes and
nipkow@8771
   296
recursive functions. The first two sections give a structured presentation of
nipkow@10538
   297
theorem proving by simplification ({\S}\ref{sec:Simplification}) and discuss
nipkow@10538
   298
important heuristics for induction ({\S}\ref{sec:InductionHeuristics}). They can
nipkow@8771
   299
be skipped by readers less interested in proofs. They are followed by a case
nipkow@10538
   300
study, a compiler for expressions ({\S}\ref{sec:ExprCompiler}). Advanced
nipkow@8771
   301
datatypes, including those involving function spaces, are covered in
nipkow@10538
   302
{\S}\ref{sec:advanced-datatypes}, which closes with another case study, search
nipkow@8771
   303
trees (``tries'').  Finally we introduce \isacommand{recdef}, a very general
nipkow@8771
   304
form of recursive function definition which goes well beyond what
nipkow@10538
   305
\isacommand{primrec} allows ({\S}\ref{sec:recdef}).
nipkow@8743
   306
nipkow@8743
   307
nipkow@8743
   308
\section{Simplification}
nipkow@8743
   309
\label{sec:Simplification}
nipkow@8743
   310
\index{simplification|(}
nipkow@8743
   311
paulson@10795
   312
So far we have proved our theorems by \isa{auto}, which simplifies
nipkow@9541
   313
\emph{all} subgoals. In fact, \isa{auto} can do much more than that, except
nipkow@9541
   314
that it did not need to so far. However, when you go beyond toy examples, you
nipkow@9541
   315
need to understand the ingredients of \isa{auto}.  This section covers the
nipkow@10971
   316
method that \isa{auto} always applies first, simplification.
nipkow@8743
   317
nipkow@8743
   318
Simplification is one of the central theorem proving tools in Isabelle and
nipkow@8743
   319
many other systems. The tool itself is called the \bfindex{simplifier}. The
nipkow@11159
   320
purpose of this section is to introduce the many features of the simplifier.
nipkow@10971
   321
Anybody intending to perform proofs in HOL should read this section. Later on
nipkow@10538
   322
({\S}\ref{sec:simplification-II}) we explain some more advanced features and a
nipkow@9754
   323
little bit of how the simplifier works. The serious student should read that
nipkow@9754
   324
section as well, in particular in order to understand what happened if things
nipkow@9754
   325
do not simplify as expected.
nipkow@8743
   326
nipkow@10978
   327
\subsubsection{What is Simplification}
nipkow@9458
   328
nipkow@8743
   329
In its most basic form, simplification means repeated application of
nipkow@8743
   330
equations from left to right. For example, taking the rules for \isa{\at}
nipkow@8743
   331
and applying them to the term \isa{[0,1] \at\ []} results in a sequence of
nipkow@8743
   332
simplification steps:
nipkow@8743
   333
\begin{ttbox}\makeatother
nipkow@8743
   334
(0#1#[]) @ []  \(\leadsto\)  0#((1#[]) @ [])  \(\leadsto\)  0#(1#([] @ []))  \(\leadsto\)  0#1#[]
nipkow@8743
   335
\end{ttbox}
nipkow@9933
   336
This is also known as \bfindex{term rewriting}\indexbold{rewriting} and the
nipkow@9933
   337
equations are referred to as \textbf{rewrite rules}\indexbold{rewrite rule}.
nipkow@9933
   338
``Rewriting'' is more honest than ``simplification'' because the terms do not
nipkow@9933
   339
necessarily become simpler in the process.
nipkow@8743
   340
nipkow@9844
   341
\input{Misc/document/simp.tex}
nipkow@8743
   342
nipkow@8743
   343
\index{simplification|)}
nipkow@8743
   344
nipkow@9844
   345
\input{Misc/document/Itrev.tex}
nipkow@8743
   346
nipkow@9493
   347
\begin{exercise}
nipkow@9493
   348
\input{Misc/document/Tree2.tex}%
nipkow@9493
   349
\end{exercise}
nipkow@8743
   350
nipkow@9844
   351
\input{CodeGen/document/CodeGen.tex}
nipkow@8743
   352
nipkow@8743
   353
paulson@10885
   354
\section{Advanced Datatypes}
nipkow@8743
   355
\label{sec:advanced-datatypes}
nipkow@8743
   356
\index{*datatype|(}
nipkow@8743
   357
\index{*primrec|(}
nipkow@8743
   358
%|)
nipkow@8743
   359
nipkow@8743
   360
This section presents advanced forms of \isacommand{datatype}s.
nipkow@8743
   361
paulson@10885
   362
\subsection{Mutual Recursion}
nipkow@8743
   363
\label{sec:datatype-mut-rec}
nipkow@8743
   364
nipkow@8743
   365
\input{Datatype/document/ABexpr.tex}
nipkow@8743
   366
paulson@10885
   367
\subsection{Nested Recursion}
nipkow@9644
   368
\label{sec:nested-datatype}
nipkow@8743
   369
nipkow@9644
   370
{\makeatother\input{Datatype/document/Nested.tex}}
nipkow@8743
   371
nipkow@8743
   372
paulson@10885
   373
\subsection{The Limits of Nested Recursion}
nipkow@8743
   374
nipkow@8743
   375
How far can we push nested recursion? By the unfolding argument above, we can
nipkow@8743
   376
reduce nested to mutual recursion provided the nested recursion only involves
nipkow@8743
   377
previously defined datatypes. This does not include functions:
nipkow@9792
   378
\begin{isabelle}
nipkow@9792
   379
\isacommand{datatype} t = C "t \isasymRightarrow\ bool"
nipkow@9792
   380
\end{isabelle}
paulson@10795
   381
This declaration is a real can of worms.
paulson@10795
   382
In HOL it must be ruled out because it requires a type
nipkow@8743
   383
\isa{t} such that \isa{t} and its power set \isa{t \isasymFun\ bool} have the
nipkow@10971
   384
same cardinality --- an impossibility. For the same reason it is not possible
nipkow@8743
   385
to allow recursion involving the type \isa{set}, which is isomorphic to
nipkow@8743
   386
\isa{t \isasymFun\ bool}.
nipkow@8743
   387
nipkow@8743
   388
Fortunately, a limited form of recursion
nipkow@8743
   389
involving function spaces is permitted: the recursive type may occur on the
nipkow@8743
   390
right of a function arrow, but never on the left. Hence the above can of worms
nipkow@8743
   391
is ruled out but the following example of a potentially infinitely branching tree is
nipkow@8743
   392
accepted:
nipkow@8771
   393
\smallskip
nipkow@8743
   394
nipkow@8743
   395
\input{Datatype/document/Fundata.tex}
nipkow@8743
   396
\bigskip
nipkow@8743
   397
nipkow@9792
   398
If you need nested recursion on the left of a function arrow, there are
nipkow@9792
   399
alternatives to pure HOL: LCF~\cite{paulson87} is a logic where types like
nipkow@9792
   400
\begin{isabelle}
nipkow@9792
   401
\isacommand{datatype} lam = C "lam \isasymrightarrow\ lam"
nipkow@9792
   402
\end{isabelle}
paulson@10795
   403
do indeed make sense.  Note the different arrow,
paulson@10795
   404
\isa{\isasymrightarrow} instead of \isa{\isasymRightarrow},
nipkow@10971
   405
expressing the type of \emph{continuous} functions. 
paulson@10795
   406
There is even a version of LCF on top of HOL,
nipkow@9792
   407
called HOLCF~\cite{MuellerNvOS99}.
nipkow@8743
   408
nipkow@8743
   409
\index{*primrec|)}
nipkow@8743
   410
\index{*datatype|)}
nipkow@8743
   411
paulson@10885
   412
\subsection{Case Study: Tries}
nipkow@10543
   413
\label{sec:Trie}
nipkow@8743
   414
nipkow@8743
   415
Tries are a classic search tree data structure~\cite{Knuth3-75} for fast
nipkow@8743
   416
indexing with strings. Figure~\ref{fig:trie} gives a graphical example of a
nipkow@8743
   417
trie containing the words ``all'', ``an'', ``ape'', ``can'', ``car'' and
nipkow@8743
   418
``cat''.  When searching a string in a trie, the letters of the string are
nipkow@8743
   419
examined sequentially. Each letter determines which subtrie to search next.
nipkow@8743
   420
In this case study we model tries as a datatype, define a lookup and an
nipkow@8743
   421
update function, and prove that they behave as expected.
nipkow@8743
   422
nipkow@8743
   423
\begin{figure}[htbp]
nipkow@8743
   424
\begin{center}
nipkow@8743
   425
\unitlength1mm
nipkow@8743
   426
\begin{picture}(60,30)
nipkow@8743
   427
\put( 5, 0){\makebox(0,0)[b]{l}}
nipkow@8743
   428
\put(25, 0){\makebox(0,0)[b]{e}}
nipkow@8743
   429
\put(35, 0){\makebox(0,0)[b]{n}}
nipkow@8743
   430
\put(45, 0){\makebox(0,0)[b]{r}}
nipkow@8743
   431
\put(55, 0){\makebox(0,0)[b]{t}}
nipkow@8743
   432
%
nipkow@8743
   433
\put( 5, 9){\line(0,-1){5}}
nipkow@8743
   434
\put(25, 9){\line(0,-1){5}}
nipkow@8743
   435
\put(44, 9){\line(-3,-2){9}}
nipkow@8743
   436
\put(45, 9){\line(0,-1){5}}
nipkow@8743
   437
\put(46, 9){\line(3,-2){9}}
nipkow@8743
   438
%
nipkow@8743
   439
\put( 5,10){\makebox(0,0)[b]{l}}
nipkow@8743
   440
\put(15,10){\makebox(0,0)[b]{n}}
nipkow@8743
   441
\put(25,10){\makebox(0,0)[b]{p}}
nipkow@8743
   442
\put(45,10){\makebox(0,0)[b]{a}}
nipkow@8743
   443
%
nipkow@8743
   444
\put(14,19){\line(-3,-2){9}}
nipkow@8743
   445
\put(15,19){\line(0,-1){5}}
nipkow@8743
   446
\put(16,19){\line(3,-2){9}}
nipkow@8743
   447
\put(45,19){\line(0,-1){5}}
nipkow@8743
   448
%
nipkow@8743
   449
\put(15,20){\makebox(0,0)[b]{a}}
nipkow@8743
   450
\put(45,20){\makebox(0,0)[b]{c}}
nipkow@8743
   451
%
nipkow@8743
   452
\put(30,30){\line(-3,-2){13}}
nipkow@8743
   453
\put(30,30){\line(3,-2){13}}
nipkow@8743
   454
\end{picture}
nipkow@8743
   455
\end{center}
nipkow@8743
   456
\caption{A sample trie}
nipkow@8743
   457
\label{fig:trie}
nipkow@8743
   458
\end{figure}
nipkow@8743
   459
nipkow@8743
   460
Proper tries associate some value with each string. Since the
nipkow@8743
   461
information is stored only in the final node associated with the string, many
nipkow@10543
   462
nodes do not carry any value. This distinction is modeled with the help
nipkow@10543
   463
of the predefined datatype \isa{option} (see {\S}\ref{sec:option}).
nipkow@8743
   464
\input{Trie/document/Trie.tex}
nipkow@8743
   465
nipkow@8743
   466
\begin{exercise}
nipkow@8743
   467
  Write an improved version of \isa{update} that does not suffer from the
nipkow@8743
   468
  space leak in the version above. Prove the main theorem for your improved
nipkow@8743
   469
  \isa{update}.
nipkow@8743
   470
\end{exercise}
nipkow@8743
   471
nipkow@8743
   472
\begin{exercise}
nipkow@8743
   473
  Write a function to \emph{delete} entries from a trie. An easy solution is
nipkow@8743
   474
  a clever modification of \isa{update} which allows both insertion and
nipkow@8743
   475
  deletion with a single function.  Prove (a modified version of) the main
nipkow@8743
   476
  theorem above. Optimize you function such that it shrinks tries after
nipkow@8743
   477
  deletion, if possible.
nipkow@8743
   478
\end{exercise}
nipkow@8743
   479
paulson@10885
   480
\section{Total Recursive Functions}
nipkow@8743
   481
\label{sec:recdef}
nipkow@8743
   482
\index{*recdef|(}
nipkow@8743
   483
nipkow@8743
   484
Although many total functions have a natural primitive recursive definition,
nipkow@8743
   485
this is not always the case. Arbitrary total recursive functions can be
nipkow@8743
   486
defined by means of \isacommand{recdef}: you can use full pattern-matching,
nipkow@8743
   487
recursion need not involve datatypes, and termination is proved by showing
nipkow@8743
   488
that the arguments of all recursive calls are smaller in a suitable (user
nipkow@11159
   489
supplied) sense. In this section we restrict ourselves to measure functions;
nipkow@10538
   490
more advanced termination proofs are discussed in {\S}\ref{sec:beyond-measure}.
nipkow@8743
   491
paulson@10885
   492
\subsection{Defining Recursive Functions}
nipkow@10654
   493
\label{sec:recdef-examples}
nipkow@8743
   494
\input{Recdef/document/examples.tex}
nipkow@8743
   495
paulson@10885
   496
\subsection{Proving Termination}
nipkow@8743
   497
nipkow@8743
   498
\input{Recdef/document/termination.tex}
nipkow@8743
   499
paulson@10885
   500
\subsection{Simplification with Recdef}
paulson@10181
   501
\label{sec:recdef-simplification}
nipkow@8743
   502
nipkow@8743
   503
\input{Recdef/document/simplification.tex}
nipkow@8743
   504
nipkow@8743
   505
\subsection{Induction}
nipkow@8743
   506
\index{induction!recursion|(}
nipkow@8743
   507
\index{recursion induction|(}
nipkow@8743
   508
nipkow@8743
   509
\input{Recdef/document/Induction.tex}
nipkow@9644
   510
\label{sec:recdef-induction}
nipkow@8743
   511
nipkow@8743
   512
\index{induction!recursion|)}
nipkow@8743
   513
\index{recursion induction|)}
nipkow@8743
   514
\index{*recdef|)}