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