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