doc-src/TutorialI/Fun/fun0.thy
author paulson
Fri, 02 Nov 2007 16:38:37 +0100
changeset 25265 3a5d33e8a019
parent 25263 b54744935036
child 25339 ef2a8a3bae4a
permissions -rw-r--r--
tweaked
nipkow@25260
     1
(*<*)
nipkow@25260
     2
theory fun0 imports Main begin
nipkow@25260
     3
(*>*)
nipkow@25260
     4
nipkow@25260
     5
text{*
nipkow@25260
     6
\subsection{Definition}
nipkow@25260
     7
\label{sec:fun-examples}
nipkow@25260
     8
nipkow@25260
     9
Here is a simple example, the \rmindex{Fibonacci function}:
nipkow@25260
    10
*}
nipkow@25260
    11
nipkow@25260
    12
fun fib :: "nat \<Rightarrow> nat" where
nipkow@25260
    13
  "fib 0 = 0" |
nipkow@25260
    14
  "fib (Suc 0) = 1" |
nipkow@25260
    15
  "fib (Suc(Suc x)) = fib x + fib (Suc x)"
nipkow@25260
    16
nipkow@25260
    17
text{*\noindent
nipkow@25260
    18
This resembles ordinary functional programming languages. Note the obligatory
nipkow@25260
    19
\isacommand{where} and \isa{|}. Command \isacommand{fun} declares and
nipkow@25260
    20
defines the function in one go. Isabelle establishes termination automatically
nipkow@25260
    21
because @{const fib}'s argument decreases in every recursive call.
nipkow@25260
    22
nipkow@25260
    23
Slightly more interesting is the insertion of a fixed element
nipkow@25260
    24
between any two elements of a list:
nipkow@25260
    25
*}
nipkow@25260
    26
nipkow@25260
    27
fun sep :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@25260
    28
  "sep a []     = []" |
nipkow@25260
    29
  "sep a [x]    = [x]" |
nipkow@25260
    30
  "sep a (x#y#zs) = x # a # sep a (y#zs)";
nipkow@25260
    31
nipkow@25260
    32
text{*\noindent
nipkow@25260
    33
This time the length of the list decreases with the
nipkow@25260
    34
recursive call; the first argument is irrelevant for termination.
nipkow@25260
    35
nipkow@25260
    36
Pattern matching\index{pattern matching!and \isacommand{fun}}
nipkow@25260
    37
need not be exhaustive and may employ wildcards:
nipkow@25260
    38
*}
nipkow@25260
    39
nipkow@25260
    40
fun last :: "'a list \<Rightarrow> 'a" where
nipkow@25260
    41
  "last [x]      = x" |
nipkow@25260
    42
  "last (_#y#zs) = last (y#zs)"
nipkow@25260
    43
nipkow@25260
    44
text{*
nipkow@25260
    45
Overlapping patterns are disambiguated by taking the order of equations into
nipkow@25260
    46
account, just as in functional programming:
nipkow@25260
    47
*}
nipkow@25260
    48
nipkow@25260
    49
fun sep1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@25260
    50
  "sep1 a (x#y#zs) = x # a # sep1 a (y#zs)" |
nipkow@25260
    51
  "sep1 _ xs       = xs"
nipkow@25260
    52
nipkow@25260
    53
text{*\noindent
nipkow@25260
    54
To guarantee that the second equation can only be applied if the first
nipkow@25260
    55
one does not match, Isabelle internally replaces the second equation
nipkow@25260
    56
by the two possibilities that are left: @{prop"sep1 a [] = []"} and
nipkow@25260
    57
@{prop"sep1 a [x] = [x]"}.  Thus the functions @{const sep} and
nipkow@25260
    58
@{const sep1} are identical.
nipkow@25260
    59
nipkow@25263
    60
Because of its pattern matching syntax, \isacommand{fun} is also useful
nipkow@25260
    61
for the definition of non-recursive functions:
nipkow@25260
    62
*}
nipkow@25260
    63
nipkow@25260
    64
fun swap12 :: "'a list \<Rightarrow> 'a list" where
nipkow@25260
    65
  "swap12 (x#y#zs) = y#x#zs" |
nipkow@25260
    66
  "swap12 zs       = zs"
nipkow@25260
    67
nipkow@25260
    68
text{*
nipkow@25260
    69
After a function~$f$ has been defined via \isacommand{fun},
nipkow@25260
    70
its defining equations (or variants derived from them) are available
nipkow@25260
    71
under the name $f$@{text".simps"} as theorems.
nipkow@25260
    72
For example, look (via \isacommand{thm}) at
nipkow@25260
    73
@{thm[source]sep.simps} and @{thm[source]sep1.simps} to see that they define
nipkow@25260
    74
the same function. What is more, those equations are automatically declared as
nipkow@25260
    75
simplification rules.
nipkow@25260
    76
nipkow@25260
    77
\subsection{Termination}
nipkow@25260
    78
nipkow@25260
    79
Isabelle's automatic termination prover for \isacommand{fun} has a
nipkow@25260
    80
fixed notion of the \emph{size} (of type @{typ nat}) of an
nipkow@25260
    81
argument. The size of a natural number is the number itself. The size
nipkow@25260
    82
of a list is its length. In general, every datatype @{text t} comes
nipkow@25260
    83
with its own size function (named @{text"t.size"}) which counts the
nipkow@25260
    84
number of constructors in a term of type @{text t} --- more precisely
nipkow@25260
    85
those constructors where one of the arguments is of the type itself:
nipkow@25260
    86
@{const Suc} in the case of @{typ nat} and @{const "op #"} in the case
nipkow@25260
    87
of lists. A recursive function is accepted if \isacommand{fun} can
nipkow@25260
    88
show that the size of one fixed argument becomes smaller with each
nipkow@25260
    89
recursive call.
nipkow@25260
    90
nipkow@25261
    91
More generally, \isacommand{fun} allows any \emph{lexicographic
nipkow@25260
    92
combination} of size measures in case there are multiple
nipkow@25261
    93
arguments. For example, the following version of \rmindex{Ackermann's
nipkow@25260
    94
function} is accepted: *}
nipkow@25260
    95
nipkow@25260
    96
fun ack2 :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
nipkow@25260
    97
  "ack2 n 0 = Suc n" |
nipkow@25260
    98
  "ack2 0 (Suc m) = ack2 (Suc 0) m" |
nipkow@25260
    99
  "ack2 (Suc n) (Suc m) = ack2 (ack2 n (Suc m)) m"
nipkow@25260
   100
nipkow@25263
   101
text{* The order of arguments has no influence on whether
nipkow@25260
   102
\isacommand{fun} can prove termination of a function. For more details
nipkow@25260
   103
see elsewhere~\cite{bulwahnKN07}.
nipkow@25260
   104
nipkow@25260
   105
\subsection{Simplification}
nipkow@25260
   106
\label{sec:fun-simplification}
nipkow@25260
   107
paulson@25265
   108
Upon a successful termination proof, the recursion equations become
nipkow@25260
   109
simplification rules, just as with \isacommand{primrec}.
nipkow@25260
   110
In most cases this works fine, but there is a subtle
nipkow@25260
   111
problem that must be mentioned: simplification may not
nipkow@25260
   112
terminate because of automatic splitting of @{text "if"}.
nipkow@25260
   113
\index{*if expressions!splitting of}
nipkow@25260
   114
Let us look at an example:
nipkow@25260
   115
*}
nipkow@25260
   116
nipkow@25261
   117
fun gcd :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
nipkow@25261
   118
  "gcd m n = (if n=0 then m else gcd n (m mod n))"
nipkow@25260
   119
nipkow@25260
   120
text{*\noindent
nipkow@25260
   121
The second argument decreases with each recursive call.
nipkow@25260
   122
The termination condition
nipkow@25260
   123
@{prop[display]"n ~= (0::nat) ==> m mod n < n"}
nipkow@25260
   124
is proved automatically because it is already present as a lemma in
nipkow@25260
   125
HOL\@.  Thus the recursion equation becomes a simplification
nipkow@25260
   126
rule. Of course the equation is nonterminating if we are allowed to unfold
nipkow@25260
   127
the recursive call inside the @{text else} branch, which is why programming
nipkow@25260
   128
languages and our simplifier don't do that. Unfortunately the simplifier does
nipkow@25260
   129
something else that leads to the same problem: it splits 
nipkow@25260
   130
each @{text "if"}-expression unless its
nipkow@25260
   131
condition simplifies to @{term True} or @{term False}.  For
nipkow@25260
   132
example, simplification reduces
nipkow@25261
   133
@{prop[display]"gcd m n = k"}
nipkow@25260
   134
in one step to
nipkow@25261
   135
@{prop[display]"(if n=0 then m else gcd n (m mod n)) = k"}
nipkow@25260
   136
where the condition cannot be reduced further, and splitting leads to
nipkow@25261
   137
@{prop[display]"(n=0 --> m=k) & (n ~= 0 --> gcd n (m mod n)=k)"}
nipkow@25261
   138
Since the recursive call @{term"gcd n (m mod n)"} is no longer protected by
nipkow@25260
   139
an @{text "if"}, it is unfolded again, which leads to an infinite chain of
nipkow@25260
   140
simplification steps. Fortunately, this problem can be avoided in many
nipkow@25260
   141
different ways.
nipkow@25260
   142
nipkow@25260
   143
The most radical solution is to disable the offending theorem
nipkow@25260
   144
@{thm[source]split_if},
nipkow@25260
   145
as shown in \S\ref{sec:AutoCaseSplits}.  However, we do not recommend this
nipkow@25260
   146
approach: you will often have to invoke the rule explicitly when
nipkow@25260
   147
@{text "if"} is involved.
nipkow@25260
   148
nipkow@25260
   149
If possible, the definition should be given by pattern matching on the left
nipkow@25260
   150
rather than @{text "if"} on the right. In the case of @{term gcd} the
nipkow@25260
   151
following alternative definition suggests itself:
nipkow@25260
   152
*}
nipkow@25260
   153
nipkow@25260
   154
fun gcd1 :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
nipkow@25260
   155
  "gcd1 m 0 = m" |
nipkow@25260
   156
  "gcd1 m n = gcd1 n (m mod n)"
nipkow@25260
   157
nipkow@25260
   158
text{*\noindent
nipkow@25260
   159
The order of equations is important: it hides the side condition
nipkow@25263
   160
@{prop"n ~= (0::nat)"}.  Unfortunately, not all conditionals can be
nipkow@25263
   161
expressed by pattern matching.
nipkow@25260
   162
nipkow@25260
   163
A simple alternative is to replace @{text "if"} by @{text case}, 
nipkow@25260
   164
which is also available for @{typ bool} and is not split automatically:
nipkow@25260
   165
*}
nipkow@25260
   166
nipkow@25260
   167
fun gcd2 :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
nipkow@25260
   168
  "gcd2 m n = (case n=0 of True \<Rightarrow> m | False \<Rightarrow> gcd2 n (m mod n))"
nipkow@25260
   169
nipkow@25260
   170
text{*\noindent
nipkow@25260
   171
This is probably the neatest solution next to pattern matching, and it is
nipkow@25260
   172
always available.
nipkow@25260
   173
nipkow@25260
   174
A final alternative is to replace the offending simplification rules by
nipkow@25260
   175
derived conditional ones. For @{term gcd} it means we have to prove
nipkow@25260
   176
these lemmas:
nipkow@25260
   177
*}
nipkow@25260
   178
nipkow@25261
   179
lemma [simp]: "gcd m 0 = m"
nipkow@25260
   180
apply(simp)
nipkow@25260
   181
done
nipkow@25260
   182
nipkow@25261
   183
lemma [simp]: "n \<noteq> 0 \<Longrightarrow> gcd m n = gcd n (m mod n)"
nipkow@25260
   184
apply(simp)
nipkow@25260
   185
done
nipkow@25260
   186
nipkow@25260
   187
text{*\noindent
nipkow@25260
   188
Simplification terminates for these proofs because the condition of the @{text
nipkow@25260
   189
"if"} simplifies to @{term True} or @{term False}.
nipkow@25260
   190
Now we can disable the original simplification rule:
nipkow@25260
   191
*}
nipkow@25260
   192
nipkow@25260
   193
declare gcd.simps [simp del]
nipkow@25260
   194
nipkow@25260
   195
text{*
nipkow@25260
   196
\index{induction!recursion|(}
nipkow@25260
   197
\index{recursion induction|(}
nipkow@25260
   198
nipkow@25260
   199
\subsection{Induction}
nipkow@25260
   200
\label{sec:fun-induction}
nipkow@25260
   201
nipkow@25260
   202
Having defined a function we might like to prove something about it.
nipkow@25260
   203
Since the function is recursive, the natural proof principle is
nipkow@25260
   204
again induction. But this time the structural form of induction that comes
nipkow@25260
   205
with datatypes is unlikely to work well --- otherwise we could have defined the
nipkow@25260
   206
function by \isacommand{primrec}. Therefore \isacommand{fun} automatically
nipkow@25260
   207
proves a suitable induction rule $f$@{text".induct"} that follows the
nipkow@25260
   208
recursion pattern of the particular function $f$. We call this
nipkow@25260
   209
\textbf{recursion induction}. Roughly speaking, it
nipkow@25260
   210
requires you to prove for each \isacommand{fun} equation that the property
nipkow@25260
   211
you are trying to establish holds for the left-hand side provided it holds
nipkow@25260
   212
for all recursive calls on the right-hand side. Here is a simple example
nipkow@25260
   213
involving the predefined @{term"map"} functional on lists:
nipkow@25260
   214
*}
nipkow@25260
   215
nipkow@25260
   216
lemma "map f (sep x xs) = sep (f x) (map f xs)"
nipkow@25260
   217
nipkow@25260
   218
txt{*\noindent
nipkow@25260
   219
Note that @{term"map f xs"}
nipkow@25260
   220
is the result of applying @{term"f"} to all elements of @{term"xs"}. We prove
nipkow@25260
   221
this lemma by recursion induction over @{term"sep"}:
nipkow@25260
   222
*}
nipkow@25260
   223
nipkow@25260
   224
apply(induct_tac x xs rule: sep.induct);
nipkow@25260
   225
nipkow@25260
   226
txt{*\noindent
nipkow@25260
   227
The resulting proof state has three subgoals corresponding to the three
nipkow@25260
   228
clauses for @{term"sep"}:
nipkow@25260
   229
@{subgoals[display,indent=0]}
nipkow@25260
   230
The rest is pure simplification:
nipkow@25260
   231
*}
nipkow@25260
   232
nipkow@25260
   233
apply simp_all;
nipkow@25260
   234
done
nipkow@25260
   235
nipkow@25263
   236
text{*\noindent The proof goes smoothly because the induction rule
nipkow@25263
   237
follows the recursion of @{const sep}.  Try proving the above lemma by
nipkow@25263
   238
structural induction, and you find that you need an additional case
nipkow@25263
   239
distinction.
nipkow@25260
   240
nipkow@25260
   241
In general, the format of invoking recursion induction is
nipkow@25260
   242
\begin{quote}
nipkow@25260
   243
\isacommand{apply}@{text"(induct_tac"} $x@1 \dots x@n$ @{text"rule:"} $f$@{text".induct)"}
nipkow@25260
   244
\end{quote}\index{*induct_tac (method)}%
nipkow@25260
   245
where $x@1~\dots~x@n$ is a list of free variables in the subgoal and $f$ the
nipkow@25263
   246
name of a function that takes an $n$ arguments. Usually the subgoal will
nipkow@25263
   247
contain the term $f x@1 \dots x@n$ but this need not be the case. The
nipkow@25260
   248
induction rules do not mention $f$ at all. Here is @{thm[source]sep.induct}:
nipkow@25260
   249
\begin{isabelle}
nipkow@25260
   250
{\isasymlbrakk}~{\isasymAnd}a.~P~a~[];\isanewline
nipkow@25260
   251
~~{\isasymAnd}a~x.~P~a~[x];\isanewline
nipkow@25260
   252
~~{\isasymAnd}a~x~y~zs.~P~a~(y~\#~zs)~{\isasymLongrightarrow}~P~a~(x~\#~y~\#~zs){\isasymrbrakk}\isanewline
nipkow@25260
   253
{\isasymLongrightarrow}~P~u~v%
nipkow@25260
   254
\end{isabelle}
nipkow@25260
   255
It merely says that in order to prove a property @{term"P"} of @{term"u"} and
nipkow@25260
   256
@{term"v"} you need to prove it for the three cases where @{term"v"} is the
nipkow@25260
   257
empty list, the singleton list, and the list with at least two elements.
nipkow@25260
   258
The final case has an induction hypothesis:  you may assume that @{term"P"}
nipkow@25260
   259
holds for the tail of that list.
nipkow@25260
   260
\index{induction!recursion|)}
nipkow@25260
   261
\index{recursion induction|)}
nipkow@25260
   262
*}
nipkow@25260
   263
(*<*)
nipkow@25260
   264
end
nipkow@25260
   265
(*>*)