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