doc-src/TutorialI/fp.tex
author nipkow
Tue, 05 Sep 2000 13:53:39 +0200
changeset 9844 8016321c7de1
parent 9792 bbefb6ce5cb2
child 9933 9feb1e0c4cb3
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     1
\chapter{Functional Programming in HOL}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     2
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     3
Although on the surface this chapter is mainly concerned with how to write
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     4
functional programs in HOL and how to verify them, most of the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     5
constructs and proof procedures introduced are general purpose and recur in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     6
any specification or verification task.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     7
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
     8
The dedicated functional programmer should be warned: HOL offers only
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
     9
\emph{total functional programming} --- all functions in HOL must be total;
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
    10
lazy data structures are not directly available. On the positive side,
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
    11
functions in HOL need not be computable: HOL is a specification language that
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
    12
goes well beyond what can be expressed as a program. However, for the time
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
    13
being we concentrate on the computable.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    14
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    15
\section{An introductory theory}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    16
\label{sec:intro-theory}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    17
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    18
Functional programming needs datatypes and functions. Both of them can be
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    19
defined in a theory with a syntax reminiscent of languages like ML or
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    20
Haskell. As an example consider the theory in figure~\ref{fig:ToyList}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    21
We will now examine it line by line.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    22
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    23
\begin{figure}[htbp]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    24
\begin{ttbox}\makeatother
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    25
\input{ToyList2/ToyList1}\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    26
\caption{A theory of lists}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    27
\label{fig:ToyList}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    28
\end{figure}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    29
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    30
{\makeatother\input{ToyList/document/ToyList.tex}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    31
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    32
The complete proof script is shown in figure~\ref{fig:ToyList-proofs}. The
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    33
concatenation of figures \ref{fig:ToyList} and \ref{fig:ToyList-proofs}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    34
constitutes the complete theory \texttt{ToyList} and should reside in file
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    35
\texttt{ToyList.thy}. It is good practice to present all declarations and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    36
definitions at the beginning of a theory to facilitate browsing.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    37
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    38
\begin{figure}[htbp]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    39
\begin{ttbox}\makeatother
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    40
\input{ToyList2/ToyList2}\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    41
\caption{Proofs about lists}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    42
\label{fig:ToyList-proofs}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    43
\end{figure}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    44
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    45
\subsubsection*{Review}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    46
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    47
This is the end of our toy proof. It should have familiarized you with
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    48
\begin{itemize}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    49
\item the standard theorem proving procedure:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    50
state a goal (lemma or theorem); proceed with proof until a separate lemma is
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    51
required; prove that lemma; come back to the original goal.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    52
\item a specific procedure that works well for functional programs:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    53
induction followed by all-out simplification via \isa{auto}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    54
\item a basic repertoire of proof commands.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    55
\end{itemize}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    56
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    57
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    58
\section{Some helpful commands}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    59
\label{sec:commands-and-hints}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    60
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    61
This section discusses a few basic commands for manipulating the proof state
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    62
and can be skipped by casual readers.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    63
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    64
There are two kinds of commands used during a proof: the actual proof
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    65
commands and auxiliary commands for examining the proof state and controlling
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    66
the display. Simple proof commands are of the form
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    67
\isacommand{apply}\isa{(method)}\indexbold{apply} where \bfindex{method} is a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    68
synonym for ``theorem proving function''. Typical examples are
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    69
\isa{induct_tac} and \isa{auto}. Further methods are introduced throughout
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    70
the tutorial.  Unless stated otherwise you may assume that a method attacks
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    71
merely the first subgoal. An exception is \isa{auto} which tries to solve all
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    72
subgoals.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    73
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    74
The most useful auxiliary commands are:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    75
\begin{description}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    76
\item[Undoing:] \isacommand{undo}\indexbold{*undo} undoes the effect of the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    77
  last command; \isacommand{undo} can be undone by
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    78
  \isacommand{redo}\indexbold{*redo}.  Both are only needed at the shell
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    79
  level and should not occur in the final theory.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    80
\item[Printing the current state:] \isacommand{pr}\indexbold{*pr} redisplays
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    81
  the current proof state, for example when it has disappeared off the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    82
  screen.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    83
\item[Limiting the number of subgoals:] \isacommand{pr}~$n$ tells Isabelle to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    84
  print only the first $n$ subgoals from now on and redisplays the current
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    85
  proof state. This is helpful when there are many subgoals.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    86
\item[Modifying the order of subgoals:]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    87
\isacommand{defer}\indexbold{*defer} moves the first subgoal to the end and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    88
\isacommand{prefer}\indexbold{*prefer}~$n$ moves subgoal $n$ to the front.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    89
\item[Printing theorems:]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    90
  \isacommand{thm}\indexbold{*thm}~\textit{name}$@1$~\dots~\textit{name}$@n$
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    91
  prints the named theorems.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    92
\item[Displaying types:] We have already mentioned the flag
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    93
  \ttindex{show_types} above. It can also be useful for detecting typos in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    94
  formulae early on. For example, if \texttt{show_types} is set and the goal
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    95
  \isa{rev(rev xs) = xs} is started, Isabelle prints the additional output
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    96
\par\noindent
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    97
\begin{isabelle}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    98
Variables:\isanewline
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    99
~~xs~::~'a~list
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   100
\end{isabelle}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   101
\par\noindent
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   102
which tells us that Isabelle has correctly inferred that
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   103
\isa{xs} is a variable of list type. On the other hand, had we
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   104
made a typo as in \isa{rev(re xs) = xs}, the response
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   105
\par\noindent
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   106
\begin{isabelle}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   107
Variables:\isanewline
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   108
~~re~::~'a~list~{\isasymRightarrow}~'a~list\isanewline
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   109
~~xs~::~'a~list%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   110
\end{isabelle}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   111
\par\noindent
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   112
would have alerted us because of the unexpected variable \isa{re}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   113
\item[Reading terms and types:] \isacommand{term}\indexbold{*term}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   114
  \textit{string} reads, type-checks and prints the given string as a term in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   115
  the current context; the inferred type is output as well.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   116
  \isacommand{typ}\indexbold{*typ} \textit{string} reads and prints the given
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   117
  string as a type in the current context.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   118
\item[(Re)loading theories:] When you start your interaction you must open a
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   119
  named theory with the line \isa{\isacommand{theory}~T~=~\dots~:}. Isabelle
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   120
  automatically loads all the required parent theories from their respective
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   121
  files (which may take a moment, unless the theories are already loaded and
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   122
  the files have not been modified).
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   123
  
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   124
  If you suddenly discover that you need to modify a parent theory of your
9494
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   125
  current theory you must first abandon your current theory\indexbold{abandon
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   126
  theory}\indexbold{theory!abandon} (at the shell
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   127
  level type \isacommand{kill}\indexbold{*kill}). After the parent theory has
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   128
  been modified you go back to your original theory. When its first line
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   129
  \isacommand{theory}\texttt{~T~=}~\dots~\texttt{:} is processed, the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   130
  modified parent is reloaded automatically.
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   131
  
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   132
  The only time when you need to load a theory by hand is when you simply
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   133
  want to check if it loads successfully without wanting to make use of the
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   134
  theory itself. This you can do by typing
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   135
  \isa{\isacommand{use\_thy}\indexbold{*use_thy}~"T"}.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   136
\end{description}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   137
Further commands are found in the Isabelle/Isar Reference Manual.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   138
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   139
We now examine Isabelle's functional programming constructs systematically,
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   140
starting with inductive datatypes.
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   141
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   142
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   143
\section{Datatypes}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   144
\label{sec:datatype}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   145
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   146
Inductive datatypes are part of almost every non-trivial application of HOL.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   147
First we take another look at a very important example, the datatype of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   148
lists, before we turn to datatypes in general. The section closes with a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   149
case study.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   150
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   151
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   152
\subsection{Lists}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   153
\indexbold{*list}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   154
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   155
Lists are one of the essential datatypes in computing. Readers of this
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   156
tutorial and users of HOL need to be familiar with their basic operations.
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   157
Theory \isa{ToyList} is only a small fragment of HOL's predefined theory
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   158
\isa{List}\footnote{\url{http://isabelle.in.tum.de/library/HOL/List.html}}.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   159
The latter contains many further operations. For example, the functions
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   160
\isaindexbold{hd} (``head'') and \isaindexbold{tl} (``tail'') return the first
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   161
element and the remainder of a list. (However, pattern-matching is usually
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   162
preferable to \isa{hd} and \isa{tl}.)  Theory \isa{List} also contains
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   163
more syntactic sugar: \isa{[}$x@1$\isa{,}\dots\isa{,}$x@n$\isa{]} abbreviates
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   164
$x@1$\isa{\#}\dots\isa{\#}$x@n$\isa{\#[]}.  In the rest of the tutorial we
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   165
always use HOL's predefined lists.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   166
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   167
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   168
\subsection{The general format}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   169
\label{sec:general-datatype}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   170
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   171
The general HOL \isacommand{datatype} definition is of the form
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   172
\[
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   173
\isacommand{datatype}~(\alpha@1, \dots, \alpha@n) \, t ~=~
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   174
C@1~\tau@{11}~\dots~\tau@{1k@1} ~\mid~ \dots ~\mid~
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   175
C@m~\tau@{m1}~\dots~\tau@{mk@m}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   176
\]
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   177
where $\alpha@i$ are distinct type variables (the parameters), $C@i$ are distinct
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   178
constructor names and $\tau@{ij}$ are types; it is customary to capitalize
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   179
the first letter in constructor names. There are a number of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   180
restrictions (such as that the type should not be empty) detailed
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   181
elsewhere~\cite{isabelle-HOL}. Isabelle notifies you if you violate them.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   182
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   183
Laws about datatypes, such as \isa{[] \isasymnoteq~x\#xs} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   184
\isa{(x\#xs = y\#ys) = (x=y \isasymand~xs=ys)}, are used automatically
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   185
during proofs by simplification.  The same is true for the equations in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   186
primitive recursive function definitions.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   187
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   188
Every datatype $t$ comes equipped with a \isa{size} function from $t$ into
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   189
the natural numbers (see~\S\ref{sec:nat} below). For lists, \isa{size} is
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   190
just the length, i.e.\ \isa{size [] = 0} and \isa{size(x \# xs) = size xs +
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   191
  1}.  In general, \isa{size} returns \isa{0} for all constructors that do
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   192
not have an argument of type $t$, and for all other constructors \isa{1 +}
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   193
the sum of the sizes of all arguments of type $t$. Note that because
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   194
\isa{size} is defined on every datatype, it is overloaded; on lists
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   195
\isa{size} is also called \isa{length}, which is not overloaded.
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   196
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   197
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   198
\subsection{Primitive recursion}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   199
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   200
Functions on datatypes are usually defined by recursion. In fact, most of the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   201
time they are defined by what is called \bfindex{primitive recursion}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   202
The keyword \isacommand{primrec}\indexbold{*primrec} is followed by a list of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   203
equations
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   204
\[ f \, x@1 \, \dots \, (C \, y@1 \, \dots \, y@k)\, \dots \, x@n = r \]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   205
such that $C$ is a constructor of the datatype $t$ and all recursive calls of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   206
$f$ in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. Thus
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   207
Isabelle immediately sees that $f$ terminates because one (fixed!) argument
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   208
becomes smaller with every recursive call. There must be exactly one equation
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   209
for each constructor.  Their order is immaterial.
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   210
A more general method for defining total recursive functions is introduced in
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   211
\S\ref{sec:recdef}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   212
9493
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   213
\begin{exercise}\label{ex:Tree}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   214
\input{Misc/document/Tree.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   215
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   216
9721
7e51c9f3d5a0 *** empty log message ***
nipkow
parents: 9689
diff changeset
   217
\input{Misc/document/case_exprs.tex}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   218
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   219
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   220
  Induction is only allowed on free (or \isasymAnd-bound) variables that
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   221
  should not occur among the assumptions of the subgoal; see
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   222
  \S\ref{sec:ind-var-in-prems} for details. Case distinction
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   223
  (\isa{case_tac}) works for arbitrary terms, which need to be
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   224
  quoted if they are non-atomic.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   225
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   226
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   227
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   228
\subsection{Case study: boolean expressions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   229
\label{sec:boolex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   230
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   231
The aim of this case study is twofold: it shows how to model boolean
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   232
expressions and some algorithms for manipulating them, and it demonstrates
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   233
the constructs introduced above.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   234
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   235
\input{Ifexpr/document/Ifexpr.tex}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   236
\medskip
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   237
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   238
How does one come up with the required lemmas? Try to prove the main theorems
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   239
without them and study carefully what \isa{auto} leaves unproved. This has
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   240
to provide the clue.  The necessity of universal quantification
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   241
(\isa{\isasymforall{}t e}) in the two lemmas is explained in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   242
\S\ref{sec:InductionHeuristics}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   243
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   244
\begin{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   245
  We strengthen the definition of a \isa{normal} If-expression as follows:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   246
  the first argument of all \isa{IF}s must be a variable. Adapt the above
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   247
  development to this changed requirement. (Hint: you may need to formulate
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   248
  some of the goals as implications (\isasymimp) rather than equalities
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   249
  (\isa{=}).)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   250
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   251
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   252
\section{Some basic types}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   253
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   254
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   255
\subsection{Natural numbers}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   256
\label{sec:nat}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   257
\index{arithmetic|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   258
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   259
\input{Misc/document/fakenat.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   260
\input{Misc/document/natsum.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   261
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   262
The usual arithmetic operations \ttindexboldpos{+}{$HOL2arithfun},
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   263
\ttindexboldpos{-}{$HOL2arithfun}, \ttindexboldpos{*}{$HOL2arithfun},
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   264
\isaindexbold{div}, \isaindexbold{mod}, \isaindexbold{min} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   265
\isaindexbold{max} are predefined, as are the relations
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   266
\indexboldpos{\isasymle}{$HOL2arithrel} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   267
\ttindexboldpos{<}{$HOL2arithrel}. There is even a least number operation
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   268
\isaindexbold{LEAST}. For example, \isa{(LEAST n.$\,$1 < n) = 2}, although
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   269
Isabelle does not prove this completely automatically. Note that \isa{1} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   270
\isa{2} are available as abbreviations for the corresponding
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   271
\isa{Suc}-expressions. If you need the full set of numerals,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   272
see~\S\ref{nat-numerals}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   273
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   274
\begin{warn}
9494
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   275
  The constant \ttindexbold{0} and the operations
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   276
  \ttindexboldpos{+}{$HOL2arithfun}, \ttindexboldpos{-}{$HOL2arithfun},
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   277
  \ttindexboldpos{*}{$HOL2arithfun}, \isaindexbold{min}, \isaindexbold{max},
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   278
  \indexboldpos{\isasymle}{$HOL2arithrel} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   279
  \ttindexboldpos{<}{$HOL2arithrel} are overloaded, i.e.\ they are available
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   280
  not just for natural numbers but at other types as well (see
9494
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   281
  \S\ref{sec:TypeClasses}). For example, given the goal \isa{x+0 = x}, there
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   282
  is nothing to indicate that you are talking about natural numbers.  Hence
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   283
  Isabelle can only infer that \isa{x} is of some arbitrary type where
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   284
  \isa{0} and \isa{+} are declared. As a consequence, you will be unable to
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   285
  prove the goal (although it may take you some time to realize what has
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   286
  happened if \texttt{show_types} is not set).  In this particular example,
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   287
  you need to include an explicit type constraint, for example \isa{x+0 =
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   288
    (x::nat)}.  If there is enough contextual information this may not be
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   289
  necessary: \isa{Suc x = x} automatically implies \isa{x::nat} because
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   290
  \isa{Suc} is not overloaded.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   291
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   292
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   293
Simple arithmetic goals are proved automatically by both \isa{auto}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   294
and the simplification methods introduced in \S\ref{sec:Simplification}.  For
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   295
example,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   296
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   297
\input{Misc/document/arith1.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   298
is proved automatically. The main restriction is that only addition is taken
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   299
into account; other arithmetic operations and quantified formulae are ignored.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   300
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   301
For more complex goals, there is the special method
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   302
\isaindexbold{arith} (which attacks the first subgoal). It proves
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   303
arithmetic goals involving the usual logical connectives (\isasymnot,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   304
\isasymand, \isasymor, \isasymimp), the relations \isasymle\ and \isa{<}, and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   305
the operations \isa{+}, \isa{-}, \isa{min} and \isa{max}. For example,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   306
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   307
\input{Misc/document/arith2.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   308
succeeds because \isa{k*k} can be treated as atomic.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   309
In contrast,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   310
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   311
\input{Misc/document/arith3.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   312
is not even proved by \isa{arith} because the proof relies essentially
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   313
on properties of multiplication.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   314
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   315
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   316
  The running time of \isa{arith} is exponential in the number of occurrences
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   317
  of \ttindexboldpos{-}{$HOL2arithfun}, \isaindexbold{min} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   318
  \isaindexbold{max} because they are first eliminated by case distinctions.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   319
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   320
  \isa{arith} is incomplete even for the restricted class of formulae
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   321
  described above (known as ``linear arithmetic''). If divisibility plays a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   322
  role, it may fail to prove a valid formula, for example $m+m \neq n+n+1$.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   323
  Fortunately, such examples are rare in practice.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   324
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   325
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   326
\index{arithmetic|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   327
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   328
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   329
\subsection{Products}
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   330
\input{Misc/document/pairs.tex}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   331
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   332
%FIXME move stuff below into section on proofs about products?
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   333
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   334
  Abstraction over pairs and tuples is merely a convenient shorthand for a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   335
  more complex internal representation.  Thus the internal and external form
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   336
  of a term may differ, which can affect proofs. If you want to avoid this
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   337
  complication, use \isa{fst} and \isa{snd}, i.e.\ write
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   338
  \isa{\isasymlambda{}p.~fst p + snd p} instead of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   339
  \isa{\isasymlambda(x,y).~x + y}.  See~\S\ref{proofs-about-products} for
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   340
  theorem proving with tuple patterns.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   341
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   342
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   343
Note that products, like natural numbers, are datatypes, which means
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   344
in particular that \isa{induct_tac} and \isa{case_tac} are applicable to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   345
products (see \S\ref{proofs-about-products}).
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   346
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   347
Instead of tuples with many components (where ``many'' is not much above 2),
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   348
it is far preferable to use record types (see \S\ref{sec:records}).
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   349
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   350
\section{Definitions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   351
\label{sec:Definitions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   352
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   353
A definition is simply an abbreviation, i.e.\ a new name for an existing
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   354
construction. In particular, definitions cannot be recursive. Isabelle offers
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   355
definitions on the level of types and terms. Those on the type level are
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   356
called type synonyms, those on the term level are called (constant)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   357
definitions.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   358
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   359
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   360
\subsection{Type synonyms}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   361
\indexbold{type synonym}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   362
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   363
Type synonyms are similar to those found in ML. Their syntax is fairly self
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   364
explanatory:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   365
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   366
\input{Misc/document/types.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   367
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   368
Note that pattern-matching is not allowed, i.e.\ each definition must be of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   369
the form $f\,x@1\,\dots\,x@n~\isasymequiv~t$.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   370
Section~\S\ref{sec:Simplification} explains how definitions are used
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   371
in proofs.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   372
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   373
\input{Misc/document/prime_def.tex}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   374
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   375
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   376
\chapter{More Functional Programming}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   377
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   378
The purpose of this chapter is to deepen the reader's understanding of the
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   379
concepts encountered so far and to introduce advanced forms of datatypes and
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   380
recursive functions. The first two sections give a structured presentation of
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   381
theorem proving by simplification (\S\ref{sec:Simplification}) and discuss
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   382
important heuristics for induction (\S\ref{sec:InductionHeuristics}). They can
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   383
be skipped by readers less interested in proofs. They are followed by a case
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   384
study, a compiler for expressions (\S\ref{sec:ExprCompiler}). Advanced
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   385
datatypes, including those involving function spaces, are covered in
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   386
\S\ref{sec:advanced-datatypes}, which closes with another case study, search
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   387
trees (``tries'').  Finally we introduce \isacommand{recdef}, a very general
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   388
form of recursive function definition which goes well beyond what
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   389
\isacommand{primrec} allows (\S\ref{sec:recdef}).
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   390
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   391
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   392
\section{Simplification}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   393
\label{sec:Simplification}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   394
\index{simplification|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   395
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   396
So far we have proved our theorems by \isa{auto}, which ``simplifies''
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   397
\emph{all} subgoals. In fact, \isa{auto} can do much more than that, except
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   398
that it did not need to so far. However, when you go beyond toy examples, you
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   399
need to understand the ingredients of \isa{auto}.  This section covers the
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9494
diff changeset
   400
method that \isa{auto} always applies first, namely simplification.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   401
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   402
Simplification is one of the central theorem proving tools in Isabelle and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   403
many other systems. The tool itself is called the \bfindex{simplifier}. The
9754
a123a64cadeb *** empty log message ***
nipkow
parents: 9742
diff changeset
   404
purpose of this section is introduce the many features of the simplifier.
a123a64cadeb *** empty log message ***
nipkow
parents: 9742
diff changeset
   405
Anybody intending to use HOL should read this section. Later on
a123a64cadeb *** empty log message ***
nipkow
parents: 9742
diff changeset
   406
(\S\ref{sec:simplification-II}) we explain some more advanced features and a
a123a64cadeb *** empty log message ***
nipkow
parents: 9742
diff changeset
   407
little bit of how the simplifier works. The serious student should read that
a123a64cadeb *** empty log message ***
nipkow
parents: 9742
diff changeset
   408
section as well, in particular in order to understand what happened if things
a123a64cadeb *** empty log message ***
nipkow
parents: 9742
diff changeset
   409
do not simplify as expected.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   410
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   411
\subsubsection{What is simplification}
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   412
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   413
In its most basic form, simplification means repeated application of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   414
equations from left to right. For example, taking the rules for \isa{\at}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   415
and applying them to the term \isa{[0,1] \at\ []} results in a sequence of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   416
simplification steps:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   417
\begin{ttbox}\makeatother
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   418
(0#1#[]) @ []  \(\leadsto\)  0#((1#[]) @ [])  \(\leadsto\)  0#(1#([] @ []))  \(\leadsto\)  0#1#[]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   419
\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   420
This is also known as \bfindex{term rewriting} and the equations are referred
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   421
to as \bfindex{rewrite rules}. ``Rewriting'' is more honest than ``simplification''
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   422
because the terms do not necessarily become simpler in the process.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   423
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   424
\input{Misc/document/simp.tex}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   425
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   426
\index{simplification|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   427
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   428
\input{Misc/document/Itrev.tex}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   429
9493
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   430
\begin{exercise}
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   431
\input{Misc/document/Tree2.tex}%
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   432
\end{exercise}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   433
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   434
\input{CodeGen/document/CodeGen.tex}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   435
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   436
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   437
\section{Advanced datatypes}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   438
\label{sec:advanced-datatypes}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   439
\index{*datatype|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   440
\index{*primrec|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   441
%|)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   442
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   443
This section presents advanced forms of \isacommand{datatype}s.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   444
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   445
\subsection{Mutual recursion}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   446
\label{sec:datatype-mut-rec}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   447
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   448
\input{Datatype/document/ABexpr.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   449
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   450
\subsection{Nested recursion}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   451
\label{sec:nested-datatype}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   452
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   453
{\makeatother\input{Datatype/document/Nested.tex}}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   454
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   455
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   456
\subsection{The limits of nested recursion}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   457
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   458
How far can we push nested recursion? By the unfolding argument above, we can
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   459
reduce nested to mutual recursion provided the nested recursion only involves
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   460
previously defined datatypes. This does not include functions:
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   461
\begin{isabelle}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   462
\isacommand{datatype} t = C "t \isasymRightarrow\ bool"
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   463
\end{isabelle}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   464
is a real can of worms: in HOL it must be ruled out because it requires a type
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   465
\isa{t} such that \isa{t} and its power set \isa{t \isasymFun\ bool} have the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   466
same cardinality---an impossibility. For the same reason it is not possible
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   467
to allow recursion involving the type \isa{set}, which is isomorphic to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   468
\isa{t \isasymFun\ bool}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   469
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   470
Fortunately, a limited form of recursion
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   471
involving function spaces is permitted: the recursive type may occur on the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   472
right of a function arrow, but never on the left. Hence the above can of worms
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   473
is ruled out but the following example of a potentially infinitely branching tree is
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   474
accepted:
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   475
\smallskip
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   476
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   477
\input{Datatype/document/Fundata.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   478
\bigskip
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   479
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   480
If you need nested recursion on the left of a function arrow, there are
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   481
alternatives to pure HOL: LCF~\cite{paulson87} is a logic where types like
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   482
\begin{isabelle}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   483
\isacommand{datatype} lam = C "lam \isasymrightarrow\ lam"
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   484
\end{isabelle}
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   485
do indeed make sense (but note the intentionally different arrow
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   486
\isa{\isasymrightarrow}). There is even a version of LCF on top of HOL,
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
   487
called HOLCF~\cite{MuellerNvOS99}.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   488
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   489
\index{*primrec|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   490
\index{*datatype|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   491
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   492
\subsection{Case study: Tries}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   493
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   494
Tries are a classic search tree data structure~\cite{Knuth3-75} for fast
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   495
indexing with strings. Figure~\ref{fig:trie} gives a graphical example of a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   496
trie containing the words ``all'', ``an'', ``ape'', ``can'', ``car'' and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   497
``cat''.  When searching a string in a trie, the letters of the string are
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   498
examined sequentially. Each letter determines which subtrie to search next.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   499
In this case study we model tries as a datatype, define a lookup and an
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   500
update function, and prove that they behave as expected.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   501
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   502
\begin{figure}[htbp]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   503
\begin{center}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   504
\unitlength1mm
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   505
\begin{picture}(60,30)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   506
\put( 5, 0){\makebox(0,0)[b]{l}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   507
\put(25, 0){\makebox(0,0)[b]{e}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   508
\put(35, 0){\makebox(0,0)[b]{n}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   509
\put(45, 0){\makebox(0,0)[b]{r}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   510
\put(55, 0){\makebox(0,0)[b]{t}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   511
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   512
\put( 5, 9){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   513
\put(25, 9){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   514
\put(44, 9){\line(-3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   515
\put(45, 9){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   516
\put(46, 9){\line(3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   517
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   518
\put( 5,10){\makebox(0,0)[b]{l}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   519
\put(15,10){\makebox(0,0)[b]{n}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   520
\put(25,10){\makebox(0,0)[b]{p}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   521
\put(45,10){\makebox(0,0)[b]{a}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   522
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   523
\put(14,19){\line(-3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   524
\put(15,19){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   525
\put(16,19){\line(3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   526
\put(45,19){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   527
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   528
\put(15,20){\makebox(0,0)[b]{a}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   529
\put(45,20){\makebox(0,0)[b]{c}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   530
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   531
\put(30,30){\line(-3,-2){13}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   532
\put(30,30){\line(3,-2){13}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   533
\end{picture}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   534
\end{center}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   535
\caption{A sample trie}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   536
\label{fig:trie}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   537
\end{figure}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   538
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   539
Proper tries associate some value with each string. Since the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   540
information is stored only in the final node associated with the string, many
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   541
nodes do not carry any value. This distinction is captured by the
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   542
following predefined datatype (from theory \isa{Option}, which is a parent
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   543
of \isa{Main}):
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   544
\smallskip
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   545
\input{Trie/document/Option2.tex}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   546
\indexbold{*option}\indexbold{*None}\indexbold{*Some}%
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   547
\input{Trie/document/Trie.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   548
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   549
\begin{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   550
  Write an improved version of \isa{update} that does not suffer from the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   551
  space leak in the version above. Prove the main theorem for your improved
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   552
  \isa{update}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   553
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   554
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   555
\begin{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   556
  Write a function to \emph{delete} entries from a trie. An easy solution is
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   557
  a clever modification of \isa{update} which allows both insertion and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   558
  deletion with a single function.  Prove (a modified version of) the main
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   559
  theorem above. Optimize you function such that it shrinks tries after
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   560
  deletion, if possible.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   561
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   562
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   563
\section{Total recursive functions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   564
\label{sec:recdef}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   565
\index{*recdef|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   566
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   567
Although many total functions have a natural primitive recursive definition,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   568
this is not always the case. Arbitrary total recursive functions can be
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   569
defined by means of \isacommand{recdef}: you can use full pattern-matching,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   570
recursion need not involve datatypes, and termination is proved by showing
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   571
that the arguments of all recursive calls are smaller in a suitable (user
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   572
supplied) sense.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   573
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   574
\subsection{Defining recursive functions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   575
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   576
\input{Recdef/document/examples.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   577
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   578
\subsection{Proving termination}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   579
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   580
\input{Recdef/document/termination.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   581
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   582
\subsection{Simplification with recdef}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   583
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   584
\input{Recdef/document/simplification.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   585
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   586
\subsection{Induction}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   587
\index{induction!recursion|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   588
\index{recursion induction|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   589
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   590
\input{Recdef/document/Induction.tex}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   591
\label{sec:recdef-induction}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   592
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   593
\index{induction!recursion|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   594
\index{recursion induction|)}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   595
9689
751fde5307e4 *** empty log message ***
nipkow
parents: 9673
diff changeset
   596
\subsection{Advanced forms of recursion}
751fde5307e4 *** empty log message ***
nipkow
parents: 9673
diff changeset
   597
\label{sec:advanced-recdef}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   598
9689
751fde5307e4 *** empty log message ***
nipkow
parents: 9673
diff changeset
   599
\input{Recdef/document/Nested0.tex}
751fde5307e4 *** empty log message ***
nipkow
parents: 9673
diff changeset
   600
\input{Recdef/document/Nested1.tex}
751fde5307e4 *** empty log message ***
nipkow
parents: 9673
diff changeset
   601
\input{Recdef/document/Nested2.tex}
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   602
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   603
\index{*recdef|)}