doc-src/TutorialI/fp.tex
author nipkow
Wed, 02 Aug 2000 13:17:11 +0200
changeset 9494 44fefb6e9994
parent 9493 494f8cd34df7
child 9541 d17c0b34d5c8
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
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     8
The dedicated functional programmer should be warned: HOL offers only {\em
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
     9
  total functional programming} --- all functions in HOL must be total; lazy
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    10
data structures are not directly available. On the positive side, functions
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    11
in HOL need not be computable: HOL is a specification language that goes well
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    12
beyond what can be expressed as a program. However, for the time being we
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
    13
concentrate on the computable.
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
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   122
  the files have not been modified). The only time when you need to load a
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   123
  theory by hand is when you simply want to check if it loads successfully
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   124
  without wanting to make use of the theory itself. This you can do by typing
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   125
  \isa{\isacommand{use\_thy}\indexbold{*use_thy}~"T"}.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   126
  
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   127
  If you suddenly discover that you need to modify a parent theory of your
9494
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   128
  current theory you must first abandon your current theory\indexbold{abandon
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   129
  theory}\indexbold{theory!abandon} (at the shell
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   130
  level type \isacommand{kill}\indexbold{*kill}). After the parent theory has
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   131
  been modified you go back to your original theory. When its first line
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   132
  \isacommand{theory}\texttt{~T~=}~\dots~\texttt{:} is processed, the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   133
  modified parent is reloaded automatically.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   134
\end{description}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   135
Further commands are found in the Isabelle/Isar Reference Manual.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   136
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   137
We now examine Isabelle's functional programming constructs systematically,
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   138
starting with inductive datatypes.
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   139
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   140
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   141
\section{Datatypes}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   142
\label{sec:datatype}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   143
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   144
Inductive datatypes are part of almost every non-trivial application of HOL.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   145
First we take another look at a very important example, the datatype of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   146
lists, before we turn to datatypes in general. The section closes with a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   147
case study.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   148
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   149
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   150
\subsection{Lists}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   151
\indexbold{*list}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   152
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   153
Lists are one of the essential datatypes in computing. Readers of this
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   154
tutorial and users of HOL need to be familiar with their basic operations.
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   155
Theory \isa{ToyList} is only a small fragment of HOL's predefined theory
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   156
\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
   157
The latter contains many further operations. For example, the functions
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   158
\isaindexbold{hd} (``head'') and \isaindexbold{tl} (``tail'') return the first
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   159
element and the remainder of a list. (However, pattern-matching is usually
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   160
preferable to \isa{hd} and \isa{tl}.)  Theory \isa{List} also contains
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   161
more syntactic sugar: \isa{[}$x@1$\isa{,}\dots\isa{,}$x@n$\isa{]} abbreviates
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   162
$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
   163
always use HOL's predefined lists.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   164
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   165
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   166
\subsection{The general format}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   167
\label{sec:general-datatype}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   168
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   169
The general HOL \isacommand{datatype} definition is of the form
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   170
\[
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   171
\isacommand{datatype}~(\alpha@1, \dots, \alpha@n) \, t ~=~
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   172
C@1~\tau@{11}~\dots~\tau@{1k@1} ~\mid~ \dots ~\mid~
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   173
C@m~\tau@{m1}~\dots~\tau@{mk@m}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   174
\]
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   175
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
   176
constructor names and $\tau@{ij}$ are types; it is customary to capitalize
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   177
the first letter in constructor names. There are a number of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   178
restrictions (such as that the type should not be empty) detailed
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   179
elsewhere~\cite{isabelle-HOL}. Isabelle notifies you if you violate them.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   180
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   181
Laws about datatypes, such as \isa{[] \isasymnoteq~x\#xs} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   182
\isa{(x\#xs = y\#ys) = (x=y \isasymand~xs=ys)}, are used automatically
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   183
during proofs by simplification.  The same is true for the equations in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   184
primitive recursive function definitions.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   185
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   186
\subsection{Primitive recursion}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   187
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   188
Functions on datatypes are usually defined by recursion. In fact, most of the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   189
time they are defined by what is called \bfindex{primitive recursion}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   190
The keyword \isacommand{primrec}\indexbold{*primrec} is followed by a list of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   191
equations
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   192
\[ 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
   193
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
   194
$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
   195
Isabelle immediately sees that $f$ terminates because one (fixed!) argument
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   196
becomes smaller with every recursive call. There must be exactly one equation
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   197
for each constructor.  Their order is immaterial.
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   198
A more general method for defining total recursive functions is introduced in
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   199
\S\ref{sec:recdef}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   200
9493
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   201
\begin{exercise}\label{ex:Tree}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   202
\input{Misc/document/Tree.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   203
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   204
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   205
\subsection{Case expressions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   206
\label{sec:case-expressions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   207
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   208
HOL also features \isaindexbold{case}-expressions for analyzing
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   209
elements of a datatype. For example,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   210
% case xs of [] => 0 | y#ys => y
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   211
\begin{isabellepar}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   212
~~~case~xs~of~[]~{\isasymRightarrow}~0~|~y~\#~ys~{\isasymRightarrow}~y
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   213
\end{isabellepar}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   214
evaluates to \isa{0} if \isa{xs} is \isa{[]} and to \isa{y} if 
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   215
\isa{xs} is \isa{y\#ys}. (Since the result in both branches must be of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   216
the same type, it follows that \isa{y::nat} and hence
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   217
\isa{xs::(nat)list}.)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   218
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   219
In general, if $e$ is a term of the datatype $t$ defined in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   220
\S\ref{sec:general-datatype} above, the corresponding
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   221
\isa{case}-expression analyzing $e$ is
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   222
\[
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   223
\begin{array}{rrcl}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   224
\isa{case}~e~\isa{of} & C@1~x@{11}~\dots~x@{1k@1} & \To & e@1 \\
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   225
                           \vdots \\
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   226
                           \mid & C@m~x@{m1}~\dots~x@{mk@m} & \To & e@m
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   227
\end{array}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   228
\]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   229
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   230
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   231
{\em All} constructors must be present, their order is fixed, and nested
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   232
patterns are not supported.  Violating these restrictions results in strange
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   233
error messages.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   234
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   235
\noindent
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   236
Nested patterns can be simulated by nested \isa{case}-expressions: instead
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   237
of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   238
% case xs of [] => 0 | [x] => x | x#(y#zs) => y
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   239
\begin{isabellepar}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   240
~~~case~xs~of~[]~{\isasymRightarrow}~0~|~[x]~{\isasymRightarrow}~x~|~x~\#~y~\#~zs~{\isasymRightarrow}~y
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   241
\end{isabellepar}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   242
write
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   243
% term(latex xsymbols symbols)"case xs of [] => 0 | x#ys => (case ys of [] => x | y#zs => y)";
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   244
\begin{isabellepar}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   245
~~~case~xs~of~[]~{\isasymRightarrow}~0~|~x~\#~ys~{\isasymRightarrow}~(case~ys~of~[]~{\isasymRightarrow}~x~|~y~\#~zs~{\isasymRightarrow}~y)%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   246
\end{isabellepar}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   247
Note that \isa{case}-expressions should be enclosed in parentheses to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   248
indicate their scope.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   249
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   250
\subsection{Structural induction and case distinction}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   251
\indexbold{structural induction}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   252
\indexbold{induction!structural}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   253
\indexbold{case distinction}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   254
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   255
Almost all the basic laws about a datatype are applied automatically during
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   256
simplification. Only induction is invoked by hand via \isaindex{induct_tac},
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   257
which works for any datatype. In some cases, induction is overkill and a case
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   258
distinction over all constructors of the datatype suffices. This is performed
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   259
by \isaindexbold{case_tac}. A trivial example:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   260
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   261
\input{Misc/document/cases.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   262
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   263
Note that we do not need to give a lemma a name if we do not intend to refer
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   264
to it explicitly in the future.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   265
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   266
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   267
  Induction is only allowed on free (or \isasymAnd-bound) variables that
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   268
  should not occur among the assumptions of the subgoal.  Case distinction
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   269
  (\isa{case_tac}) works for arbitrary terms, which need to be
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   270
  quoted if they are non-atomic.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   271
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   272
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   273
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   274
\subsection{Case study: boolean expressions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   275
\label{sec:boolex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   276
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   277
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
   278
expressions and some algorithms for manipulating them, and it demonstrates
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   279
the constructs introduced above.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   280
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   281
\input{Ifexpr/document/Ifexpr.tex}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   282
\medskip
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   283
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   284
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
   285
without them and study carefully what \isa{auto} leaves unproved. This has
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   286
to provide the clue.  The necessity of universal quantification
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   287
(\isa{\isasymforall{}t e}) in the two lemmas is explained in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   288
\S\ref{sec:InductionHeuristics}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   289
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   290
\begin{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   291
  We strengthen the definition of a \isa{normal} If-expression as follows:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   292
  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
   293
  development to this changed requirement. (Hint: you may need to formulate
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   294
  some of the goals as implications (\isasymimp) rather than equalities
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   295
  (\isa{=}).)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   296
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   297
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   298
\section{Some basic types}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   299
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   300
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   301
\subsection{Natural numbers}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   302
\index{arithmetic|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   303
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   304
\input{Misc/document/fakenat.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   305
\input{Misc/document/natsum.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   306
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   307
The usual arithmetic operations \ttindexboldpos{+}{$HOL2arithfun},
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   308
\ttindexboldpos{-}{$HOL2arithfun}, \ttindexboldpos{*}{$HOL2arithfun},
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   309
\isaindexbold{div}, \isaindexbold{mod}, \isaindexbold{min} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   310
\isaindexbold{max} are predefined, as are the relations
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   311
\indexboldpos{\isasymle}{$HOL2arithrel} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   312
\ttindexboldpos{<}{$HOL2arithrel}. There is even a least number operation
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   313
\isaindexbold{LEAST}. For example, \isa{(LEAST n.$\,$1 < n) = 2}, although
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   314
Isabelle does not prove this completely automatically. Note that \isa{1} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   315
\isa{2} are available as abbreviations for the corresponding
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   316
\isa{Suc}-expressions. If you need the full set of numerals,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   317
see~\S\ref{nat-numerals}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   318
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   319
\begin{warn}
9494
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   320
  The constant \ttindexbold{0} and the operations
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   321
  \ttindexboldpos{+}{$HOL2arithfun}, \ttindexboldpos{-}{$HOL2arithfun},
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   322
  \ttindexboldpos{*}{$HOL2arithfun}, \isaindexbold{min}, \isaindexbold{max},
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   323
  \indexboldpos{\isasymle}{$HOL2arithrel} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   324
  \ttindexboldpos{<}{$HOL2arithrel} are overloaded, i.e.\ they are available
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   325
  not just for natural numbers but at other types as well (see
9494
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   326
  \S\ref{sec:TypeClasses}). For example, given the goal \isa{x+0 = x}, there
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   327
  is nothing to indicate that you are talking about natural numbers.  Hence
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   328
  Isabelle can only infer that \isa{x} is of some arbitrary type where
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   329
  \isa{0} and \isa{+} are declared. As a consequence, you will be unable to
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   330
  prove the goal (although it may take you some time to realize what has
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   331
  happened if \texttt{show_types} is not set).  In this particular example,
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   332
  you need to include an explicit type constraint, for example \isa{x+0 =
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   333
    (x::nat)}.  If there is enough contextual information this may not be
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   334
  necessary: \isa{Suc x = x} automatically implies \isa{x::nat} because
44fefb6e9994 *** empty log message ***
nipkow
parents: 9493
diff changeset
   335
  \isa{Suc} is not overloaded.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   336
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   337
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   338
Simple arithmetic goals are proved automatically by both \isa{auto}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   339
and the simplification methods introduced in \S\ref{sec:Simplification}.  For
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   340
example,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   341
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   342
\input{Misc/document/arith1.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   343
is proved automatically. The main restriction is that only addition is taken
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   344
into account; other arithmetic operations and quantified formulae are ignored.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   345
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   346
For more complex goals, there is the special method
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   347
\isaindexbold{arith} (which attacks the first subgoal). It proves
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   348
arithmetic goals involving the usual logical connectives (\isasymnot,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   349
\isasymand, \isasymor, \isasymimp), the relations \isasymle\ and \isa{<}, and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   350
the operations \isa{+}, \isa{-}, \isa{min} and \isa{max}. For example,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   351
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   352
\input{Misc/document/arith2.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   353
succeeds because \isa{k*k} can be treated as atomic.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   354
In contrast,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   355
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   356
\input{Misc/document/arith3.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   357
is not even proved by \isa{arith} because the proof relies essentially
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   358
on properties of multiplication.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   359
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   360
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   361
  The running time of \isa{arith} is exponential in the number of occurrences
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   362
  of \ttindexboldpos{-}{$HOL2arithfun}, \isaindexbold{min} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   363
  \isaindexbold{max} because they are first eliminated by case distinctions.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   364
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   365
  \isa{arith} is incomplete even for the restricted class of formulae
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   366
  described above (known as ``linear arithmetic''). If divisibility plays a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   367
  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
   368
  Fortunately, such examples are rare in practice.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   369
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   370
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   371
\index{arithmetic|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   372
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   373
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   374
\subsection{Products}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   375
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   376
HOL also has pairs: \isa{($a@1$,$a@2$)} is of type \isa{$\tau@1$ *
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   377
  $\tau@2$} provided each $a@i$ is of type $\tau@i$. The components of a pair
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   378
are extracted by \isa{fst} and \isa{snd}: \isa{fst($x$,$y$) = $x$} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   379
\isa{snd($x$,$y$) = $y$}. Tuples are simulated by pairs nested to the right:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   380
\isa{($a@1$,$a@2$,$a@3$)} stands for \isa{($a@1$,($a@2$,$a@3$))} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   381
\isa{$\tau@1$ * $\tau@2$ * $\tau@3$} for \isa{$\tau@1$ * ($\tau@2$ *
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   382
  $\tau@3$)}. Therefore we have \isa{fst(snd($a@1$,$a@2$,$a@3$)) = $a@2$}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   383
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   384
It is possible to use (nested) tuples as patterns in abstractions, for
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   385
example \isa{\isasymlambda(x,y,z).x+y+z} and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   386
\isa{\isasymlambda((x,y),z).x+y+z}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   387
In addition to explicit $\lambda$-abstractions, tuple patterns can be used in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   388
most variable binding constructs. Typical examples are
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   389
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   390
\input{Misc/document/pairs.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   391
Further important examples are quantifiers and sets (see~\S\ref{quant-pats}).
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   392
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   393
%FIXME move stuff below into section on proofs about products?
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   394
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   395
  Abstraction over pairs and tuples is merely a convenient shorthand for a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   396
  more complex internal representation.  Thus the internal and external form
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   397
  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
   398
  complication, use \isa{fst} and \isa{snd}, i.e.\ write
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   399
  \isa{\isasymlambda{}p.~fst p + snd p} instead of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   400
  \isa{\isasymlambda(x,y).~x + y}.  See~\S\ref{proofs-about-products} for
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   401
  theorem proving with tuple patterns.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   402
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   403
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   404
Finally note that products, like natural numbers, are datatypes, which means
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   405
in particular that \isa{induct_tac} and \isa{case_tac} are applicable to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   406
products (see \S\ref{proofs-about-products}).
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   407
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   408
\section{Definitions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   409
\label{sec:Definitions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   410
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   411
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
   412
construction. In particular, definitions cannot be recursive. Isabelle offers
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   413
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
   414
called type synonyms, those on the term level are called (constant)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   415
definitions.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   416
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   417
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   418
\subsection{Type synonyms}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   419
\indexbold{type synonym}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   420
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   421
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
   422
explanatory:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   423
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   424
\input{Misc/document/types.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   425
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   426
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
   427
the form $f\,x@1\,\dots\,x@n~\isasymequiv~t$.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   428
Section~\S\ref{sec:Simplification} explains how definitions are used
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   429
in proofs.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   430
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   431
\begin{warn}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   432
A common mistake when writing definitions is to introduce extra free
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   433
variables on the right-hand side as in the following fictitious definition:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   434
\input{Misc/document/prime_def.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   435
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   436
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   437
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   438
\chapter{More Functional Programming}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   439
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   440
The purpose of this chapter is to deepen the reader's understanding of the
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   441
concepts encountered so far and to introduce advanced forms of datatypes and
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   442
recursive functions. The first two sections give a structured presentation of
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   443
theorem proving by simplification (\S\ref{sec:Simplification}) and discuss
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   444
important heuristics for induction (\S\ref{sec:InductionHeuristics}). They can
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   445
be skipped by readers less interested in proofs. They are followed by a case
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   446
study, a compiler for expressions (\S\ref{sec:ExprCompiler}). Advanced
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   447
datatypes, including those involving function spaces, are covered in
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   448
\S\ref{sec:advanced-datatypes}, which closes with another case study, search
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   449
trees (``tries'').  Finally we introduce \isacommand{recdef}, a very general
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   450
form of recursive function definition which goes well beyond what
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   451
\isacommand{primrec} allows (\S\ref{sec:recdef}).
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   452
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   453
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   454
\section{Simplification}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   455
\label{sec:Simplification}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   456
\index{simplification|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   457
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   458
So far we have proved our theorems by \isa{auto}, which ``simplifies'' all
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   459
subgoals. In fact, \isa{auto} can do much more than that, except that it
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   460
did not need to so far. However, when you go beyond toy examples, you need to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   461
understand the ingredients of \isa{auto}.  This section covers the method
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   462
that \isa{auto} always applies first, namely simplification.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   463
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   464
Simplification is one of the central theorem proving tools in Isabelle and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   465
many other systems. The tool itself is called the \bfindex{simplifier}. The
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   466
purpose of this section is twofold: to introduce the many features of the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   467
simplifier (\S\ref{sec:SimpFeatures}) and to explain a little bit how the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   468
simplifier works (\S\ref{sec:SimpHow}).  Anybody intending to use HOL should
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   469
read \S\ref{sec:SimpFeatures}, and the serious student should read
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   470
\S\ref{sec:SimpHow} as well in order to understand what happened in case
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   471
things do not simplify as expected.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   472
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   473
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   474
\subsection{Using the simplifier}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   475
\label{sec:SimpFeatures}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   476
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   477
\subsubsection{What is simplification}
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   478
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   479
In its most basic form, simplification means repeated application of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   480
equations from left to right. For example, taking the rules for \isa{\at}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   481
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
   482
simplification steps:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   483
\begin{ttbox}\makeatother
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   484
(0#1#[]) @ []  \(\leadsto\)  0#((1#[]) @ [])  \(\leadsto\)  0#(1#([] @ []))  \(\leadsto\)  0#1#[]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   485
\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   486
This is also known as \bfindex{term rewriting} and the equations are referred
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   487
to as \bfindex{rewrite rules}. ``Rewriting'' is more honest than ``simplification''
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   488
because the terms do not necessarily become simpler in the process.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   489
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   490
\subsubsection{Simplification rules}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   491
\indexbold{simplification rules}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   492
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   493
To facilitate simplification, theorems can be declared to be simplification
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   494
rules (with the help of the attribute \isa{[simp]}\index{*simp
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   495
  (attribute)}), in which case proofs by simplification make use of these
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   496
rules automatically. In addition the constructs \isacommand{datatype} and
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   497
\isacommand{primrec} (and a few others) invisibly declare useful
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   498
simplification rules. Explicit definitions are \emph{not} declared
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   499
simplification rules automatically!
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   500
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   501
Not merely equations but pretty much any theorem can become a simplification
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   502
rule. The simplifier will try to make sense of it.  For example, a theorem
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   503
\isasymnot$P$ is automatically turned into \isa{$P$ = False}. The details
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   504
are explained in \S\ref{sec:SimpHow}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   505
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   506
The simplification attribute of theorems can be turned on and off as follows:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   507
\begin{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   508
theorems [simp] = \(list of theorem names\);
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   509
theorems [simp del] = \(list of theorem names\);
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   510
\end{ttbox}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   511
As a rule of thumb, equations that really simplify (like \isa{rev(rev xs) =
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   512
  xs} and \mbox{\isa{xs \at\ [] = xs}}) should be made simplification
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   513
rules.  Those of a more specific nature (e.g.\ distributivity laws, which
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   514
alter the structure of terms considerably) should only be used selectively,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   515
i.e.\ they should not be default simplification rules.  Conversely, it may
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   516
also happen that a simplification rule needs to be disabled in certain
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   517
proofs.  Frequent changes in the simplification status of a theorem may
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   518
indicate a badly designed theory.
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   519
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   520
  Simplification may not terminate, for example if both $f(x) = g(x)$ and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   521
  $g(x) = f(x)$ are simplification rules. It is the user's responsibility not
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   522
  to include simplification rules that can lead to nontermination, either on
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   523
  their own or in combination with other simplification rules.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   524
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   525
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   526
\subsubsection{The simplification method}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   527
\index{*simp (method)|bold}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   528
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   529
The general format of the simplification method is
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   530
\begin{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   531
simp \(list of modifiers\)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   532
\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   533
where the list of \emph{modifiers} helps to fine tune the behaviour and may
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   534
be empty. Most if not all of the proofs seen so far could have been performed
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   535
with \isa{simp} instead of \isa{auto}, except that \isa{simp} attacks
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   536
only the first subgoal and may thus need to be repeated.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   537
Note that \isa{simp} fails if nothing changes.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   538
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   539
\subsubsection{Adding and deleting simplification rules}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   540
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   541
If a certain theorem is merely needed in a few proofs by simplification,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   542
we do not need to make it a global simplification rule. Instead we can modify
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   543
the set of simplification rules used in a simplification step by adding rules
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   544
to it and/or deleting rules from it. The two modifiers for this are
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   545
\begin{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   546
add: \(list of theorem names\)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   547
del: \(list of theorem names\)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   548
\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   549
In case you want to use only a specific list of theorems and ignore all
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   550
others:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   551
\begin{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   552
only: \(list of theorem names\)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   553
\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   554
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   555
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   556
\subsubsection{Assumptions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   557
\index{simplification!with/of assumptions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   558
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   559
{\makeatother\input{Misc/document/asm_simp.tex}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   560
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   561
\subsubsection{Rewriting with definitions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   562
\index{simplification!with definitions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   563
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   564
\input{Misc/document/def_rewr.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   565
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   566
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   567
  If you have defined $f\,x\,y~\isasymequiv~t$ then you can only expand
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   568
  occurrences of $f$ with at least two arguments. Thus it is safer to define
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   569
  $f$~\isasymequiv~\isasymlambda$x\,y.\;t$.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   570
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   571
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   572
\subsubsection{Simplifying let-expressions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   573
\index{simplification!of let-expressions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   574
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   575
Proving a goal containing \isaindex{let}-expressions almost invariably
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   576
requires the \isa{let}-con\-structs to be expanded at some point. Since
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   577
\isa{let}-\isa{in} is just syntactic sugar for a predefined constant (called
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   578
\isa{Let}), expanding \isa{let}-constructs means rewriting with
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   579
\isa{Let_def}:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   580
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   581
{\makeatother\input{Misc/document/let_rewr.tex}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   582
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   583
\subsubsection{Conditional equations}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   584
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   585
\input{Misc/document/cond_rewr.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   586
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   587
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   588
\subsubsection{Automatic case splits}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   589
\indexbold{case splits}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   590
\index{*split|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   591
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   592
{\makeatother\input{Misc/document/case_splits.tex}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   593
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   594
\index{*split|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   595
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   596
\begin{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   597
  The simplifier merely simplifies the condition of an \isa{if} but not the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   598
  \isa{then} or \isa{else} parts. The latter are simplified only after the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   599
  condition reduces to \isa{True} or \isa{False}, or after splitting. The
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   600
  same is true for \isaindex{case}-expressions: only the selector is
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   601
  simplified at first, until either the expression reduces to one of the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   602
  cases or it is split.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   603
\end{warn}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   604
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   605
\subsubsection{Arithmetic}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   606
\index{arithmetic}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   607
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   608
The simplifier routinely solves a small class of linear arithmetic formulae
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   609
(over type \isa{nat} and other numeric types): it only takes into account
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   610
assumptions and conclusions that are (possibly negated) (in)equalities
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   611
(\isa{=}, \isasymle, \isa{<}) and it only knows about addition. Thus
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   612
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   613
\input{Misc/document/arith1.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   614
is proved by simplification, whereas the only slightly more complex
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   615
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   616
\input{Misc/document/arith4.tex}%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   617
is not proved by simplification and requires \isa{arith}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   618
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   619
\subsubsection{Permutative rewrite rules}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   620
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   621
A rewrite rule is {\bf permutative} if the left-hand side and right-hand side
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   622
are the same up to renaming of variables.  The most common permutative rule
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   623
is commutativity: $x+y = y+x$.  Another example is $(x-y)-z = (x-z)-y$.  Such
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   624
rules are problematic because once they apply, they can be used forever.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   625
The simplifier is aware of this danger and treats permutative rules
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   626
separately. For details see~\cite{isabelle-ref}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   627
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   628
\subsubsection{Tracing}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   629
\indexbold{tracing the simplifier}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   630
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   631
{\makeatother\input{Misc/document/trace_simp.tex}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   632
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   633
\subsection{How it works}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   634
\label{sec:SimpHow}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   635
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   636
\subsubsection{Higher-order patterns}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   637
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   638
\subsubsection{Local assumptions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   639
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   640
\subsubsection{The preprocessor}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   641
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   642
\index{simplification|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   643
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   644
\section{Induction heuristics}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   645
\label{sec:InductionHeuristics}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   646
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   647
The purpose of this section is to illustrate some simple heuristics for
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   648
inductive proofs. The first one we have already mentioned in our initial
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   649
example:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   650
\begin{quote}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   651
{\em 1. Theorems about recursive functions are proved by induction.}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   652
\end{quote}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   653
In case the function has more than one argument
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   654
\begin{quote}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   655
{\em 2. Do induction on argument number $i$ if the function is defined by
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   656
recursion in argument number $i$.}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   657
\end{quote}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   658
When we look at the proof of {\makeatother\isa{(xs @ ys) @ zs = xs @ (ys @
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   659
  zs)}} in \S\ref{sec:intro-proof} we find (a) \isa{\at} is recursive in
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   660
the first argument, (b) \isa{xs} occurs only as the first argument of
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   661
\isa{\at}, and (c) both \isa{ys} and \isa{zs} occur at least once as
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   662
the second argument of \isa{\at}. Hence it is natural to perform induction
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   663
on \isa{xs}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   664
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   665
The key heuristic, and the main point of this section, is to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   666
generalize the goal before induction. The reason is simple: if the goal is
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   667
too specific, the induction hypothesis is too weak to allow the induction
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   668
step to go through. Let us now illustrate the idea with an example.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   669
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   670
{\makeatother\input{Misc/document/Itrev.tex}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   671
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   672
A final point worth mentioning is the orientation of the equation we just
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   673
proved: the more complex notion (\isa{itrev}) is on the left-hand
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   674
side, the simpler \isa{rev} on the right-hand side. This constitutes
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   675
another, albeit weak heuristic that is not restricted to induction:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   676
\begin{quote}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   677
  {\em 5. The right-hand side of an equation should (in some sense) be
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   678
    simpler than the left-hand side.}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   679
\end{quote}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   680
The heuristic is tricky to apply because it is not obvious that
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   681
\isa{rev xs \at\ ys} is simpler than \isa{itrev xs ys}. But see what
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   682
happens if you try to prove \isa{rev xs \at\ ys = itrev xs ys}!
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   683
9493
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   684
\begin{exercise}
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   685
\input{Misc/document/Tree2.tex}%
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
   686
\end{exercise}
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   687
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   688
\section{Case study: compiling expressions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   689
\label{sec:ExprCompiler}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   690
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   691
{\makeatother\input{CodeGen/document/CodeGen.tex}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   692
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   693
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   694
\section{Advanced datatypes}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   695
\label{sec:advanced-datatypes}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   696
\index{*datatype|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   697
\index{*primrec|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   698
%|)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   699
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   700
This section presents advanced forms of \isacommand{datatype}s.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   701
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   702
\subsection{Mutual recursion}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   703
\label{sec:datatype-mut-rec}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   704
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   705
\input{Datatype/document/ABexpr.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   706
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   707
\subsection{Nested recursion}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   708
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   709
\input{Datatype/document/Nested.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   710
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   711
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   712
\subsection{The limits of nested recursion}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   713
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   714
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
   715
reduce nested to mutual recursion provided the nested recursion only involves
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   716
previously defined datatypes. This does not include functions:
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   717
\begin{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   718
datatype t = C (t => bool)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   719
\end{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   720
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
   721
\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
   722
same cardinality---an impossibility. For the same reason it is not possible
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   723
to allow recursion involving the type \isa{set}, which is isomorphic to
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   724
\isa{t \isasymFun\ bool}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   725
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   726
Fortunately, a limited form of recursion
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   727
involving function spaces is permitted: the recursive type may occur on the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   728
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
   729
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
   730
accepted:
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   731
\smallskip
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   732
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   733
\input{Datatype/document/Fundata.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   734
\bigskip
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   735
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   736
If you need nested recursion on the left of a function arrow,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   737
there are alternatives to pure HOL: LCF~\cite{paulson87} is a logic where types like
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   738
\begin{ttbox}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   739
datatype lam = C (lam -> lam)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   740
\end{ttbox}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   741
do indeed make sense (note the intentionally different arrow \isa{->}).
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   742
There is even a version of LCF on top of HOL, called
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   743
HOLCF~\cite{MuellerNvOS99}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   744
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   745
\index{*primrec|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   746
\index{*datatype|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   747
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   748
\subsection{Case study: Tries}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   749
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   750
Tries are a classic search tree data structure~\cite{Knuth3-75} for fast
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   751
indexing with strings. Figure~\ref{fig:trie} gives a graphical example of a
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   752
trie containing the words ``all'', ``an'', ``ape'', ``can'', ``car'' and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   753
``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
   754
examined sequentially. Each letter determines which subtrie to search next.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   755
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
   756
update function, and prove that they behave as expected.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   757
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   758
\begin{figure}[htbp]
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   759
\begin{center}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   760
\unitlength1mm
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   761
\begin{picture}(60,30)
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   762
\put( 5, 0){\makebox(0,0)[b]{l}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   763
\put(25, 0){\makebox(0,0)[b]{e}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   764
\put(35, 0){\makebox(0,0)[b]{n}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   765
\put(45, 0){\makebox(0,0)[b]{r}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   766
\put(55, 0){\makebox(0,0)[b]{t}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   767
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   768
\put( 5, 9){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   769
\put(25, 9){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   770
\put(44, 9){\line(-3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   771
\put(45, 9){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   772
\put(46, 9){\line(3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   773
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   774
\put( 5,10){\makebox(0,0)[b]{l}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   775
\put(15,10){\makebox(0,0)[b]{n}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   776
\put(25,10){\makebox(0,0)[b]{p}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   777
\put(45,10){\makebox(0,0)[b]{a}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   778
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   779
\put(14,19){\line(-3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   780
\put(15,19){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   781
\put(16,19){\line(3,-2){9}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   782
\put(45,19){\line(0,-1){5}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   783
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   784
\put(15,20){\makebox(0,0)[b]{a}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   785
\put(45,20){\makebox(0,0)[b]{c}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   786
%
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   787
\put(30,30){\line(-3,-2){13}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   788
\put(30,30){\line(3,-2){13}}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   789
\end{picture}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   790
\end{center}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   791
\caption{A sample trie}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   792
\label{fig:trie}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   793
\end{figure}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   794
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   795
Proper tries associate some value with each string. Since the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   796
information is stored only in the final node associated with the string, many
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   797
nodes do not carry any value. This distinction is captured by the
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   798
following predefined datatype (from theory \isa{Option}, which is a parent
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   799
of \isa{Main}):
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   800
\smallskip
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   801
\input{Trie/document/Option2.tex}
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8743
diff changeset
   802
\indexbold{*option}\indexbold{*None}\indexbold{*Some}%
8743
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   803
\input{Trie/document/Trie.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   804
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   805
\begin{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   806
  Write an improved version of \isa{update} that does not suffer from the
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   807
  space leak in the version above. Prove the main theorem for your improved
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   808
  \isa{update}.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   809
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   810
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   811
\begin{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   812
  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
   813
  a clever modification of \isa{update} which allows both insertion and
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   814
  deletion with a single function.  Prove (a modified version of) the main
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   815
  theorem above. Optimize you function such that it shrinks tries after
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   816
  deletion, if possible.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   817
\end{exercise}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   818
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   819
\section{Total recursive functions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   820
\label{sec:recdef}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   821
\index{*recdef|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   822
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   823
Although many total functions have a natural primitive recursive definition,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   824
this is not always the case. Arbitrary total recursive functions can be
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   825
defined by means of \isacommand{recdef}: you can use full pattern-matching,
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   826
recursion need not involve datatypes, and termination is proved by showing
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   827
that the arguments of all recursive calls are smaller in a suitable (user
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   828
supplied) sense.
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   829
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   830
\subsection{Defining recursive functions}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   831
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   832
\input{Recdef/document/examples.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   833
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   834
\subsection{Proving termination}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   835
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   836
\input{Recdef/document/termination.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   837
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   838
\subsection{Simplification with recdef}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   839
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   840
\input{Recdef/document/simplification.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   841
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   842
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   843
\subsection{Induction}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   844
\index{induction!recursion|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   845
\index{recursion induction|(}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   846
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   847
\input{Recdef/document/Induction.tex}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   848
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   849
\index{induction!recursion|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   850
\index{recursion induction|)}
3253c6046d57 I wonder if that's all?
nipkow
parents:
diff changeset
   851
\index{*recdef|)}