doc-src/Functions/Thy/Functions.thy
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 15:03:34 +0200
branchisac-from-Isabelle2009-2
changeset 37913 20e3616b2d9c
parent 33856 14a658faadb6
child 39985 06fc1a79b4bf
permissions -rw-r--r--
prepare reactivation of isac-update-Isa09-2
krauss@29718
     1
(*  Title:      doc-src/IsarAdvanced/Functions/Thy/Fundefs.thy
krauss@21212
     2
    Author:     Alexander Krauss, TU Muenchen
krauss@21212
     3
krauss@21212
     4
Tutorial for function definitions with the new "function" package.
krauss@21212
     5
*)
krauss@21212
     6
krauss@21212
     7
theory Functions
krauss@21212
     8
imports Main
krauss@21212
     9
begin
krauss@21212
    10
krauss@23188
    11
section {* Function Definitions for Dummies *}
krauss@21212
    12
krauss@21212
    13
text {*
krauss@21212
    14
  In most cases, defining a recursive function is just as simple as other definitions:
krauss@23003
    15
*}
krauss@21212
    16
krauss@21212
    17
fun fib :: "nat \<Rightarrow> nat"
krauss@21212
    18
where
krauss@21212
    19
  "fib 0 = 1"
krauss@21212
    20
| "fib (Suc 0) = 1"
krauss@21212
    21
| "fib (Suc (Suc n)) = fib n + fib (Suc n)"
krauss@21212
    22
krauss@21212
    23
text {*
krauss@23003
    24
  The syntax is rather self-explanatory: We introduce a function by
krauss@25091
    25
  giving its name, its type, 
krauss@25091
    26
  and a set of defining recursive equations.
krauss@25091
    27
  If we leave out the type, the most general type will be
krauss@25091
    28
  inferred, which can sometimes lead to surprises: Since both @{term
krauss@25278
    29
  "1::nat"} and @{text "+"} are overloaded, we would end up
krauss@25091
    30
  with @{text "fib :: nat \<Rightarrow> 'a::{one,plus}"}.
krauss@23003
    31
*}
krauss@23003
    32
krauss@23003
    33
text {*
krauss@23003
    34
  The function always terminates, since its argument gets smaller in
krauss@23188
    35
  every recursive call. 
krauss@23188
    36
  Since HOL is a logic of total functions, termination is a
krauss@23188
    37
  fundamental requirement to prevent inconsistencies\footnote{From the
krauss@23188
    38
  \qt{definition} @{text "f(n) = f(n) + 1"} we could prove 
krauss@23188
    39
  @{text "0 = 1"} by subtracting @{text "f(n)"} on both sides.}.
krauss@23188
    40
  Isabelle tries to prove termination automatically when a definition
krauss@23188
    41
  is made. In \S\ref{termination}, we will look at cases where this
krauss@23188
    42
  fails and see what to do then.
krauss@21212
    43
*}
krauss@21212
    44
krauss@21212
    45
subsection {* Pattern matching *}
krauss@21212
    46
krauss@22065
    47
text {* \label{patmatch}
krauss@23003
    48
  Like in functional programming, we can use pattern matching to
krauss@23003
    49
  define functions. At the moment we will only consider \emph{constructor
krauss@21212
    50
  patterns}, which only consist of datatype constructors and
krauss@23805
    51
  variables. Furthermore, patterns must be linear, i.e.\ all variables
krauss@23805
    52
  on the left hand side of an equation must be distinct. In
krauss@23805
    53
  \S\ref{genpats} we discuss more general pattern matching.
krauss@21212
    54
krauss@21212
    55
  If patterns overlap, the order of the equations is taken into
krauss@21212
    56
  account. The following function inserts a fixed element between any
krauss@21212
    57
  two elements of a list:
krauss@21212
    58
*}
krauss@21212
    59
krauss@21212
    60
fun sep :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
krauss@21212
    61
where
krauss@21212
    62
  "sep a (x#y#xs) = x # a # sep a (y # xs)"
krauss@21212
    63
| "sep a xs       = xs"
krauss@21212
    64
krauss@21212
    65
text {* 
krauss@23188
    66
  Overlapping patterns are interpreted as \qt{increments} to what is
krauss@21212
    67
  already there: The second equation is only meant for the cases where
krauss@21212
    68
  the first one does not match. Consequently, Isabelle replaces it
krauss@22065
    69
  internally by the remaining cases, making the patterns disjoint:
krauss@21212
    70
*}
krauss@21212
    71
krauss@22065
    72
thm sep.simps
krauss@21212
    73
krauss@22065
    74
text {* @{thm [display] sep.simps[no_vars]} *}
krauss@21212
    75
krauss@21212
    76
text {* 
krauss@23805
    77
  \noindent The equations from function definitions are automatically used in
krauss@21212
    78
  simplification:
krauss@21212
    79
*}
krauss@21212
    80
krauss@23188
    81
lemma "sep 0 [1, 2, 3] = [1, 0, 2, 0, 3]"
krauss@21212
    82
by simp
krauss@21212
    83
krauss@21212
    84
subsection {* Induction *}
krauss@21212
    85
krauss@22065
    86
text {*
krauss@21212
    87
krauss@23805
    88
  Isabelle provides customized induction rules for recursive
krauss@23805
    89
  functions. These rules follow the recursive structure of the
krauss@23805
    90
  definition. Here is the rule @{text sep.induct} arising from the
krauss@23805
    91
  above definition of @{const sep}:
krauss@23188
    92
krauss@23805
    93
  @{thm [display] sep.induct}
krauss@23805
    94
  
krauss@23805
    95
  We have a step case for list with at least two elements, and two
krauss@23805
    96
  base cases for the zero- and the one-element list. Here is a simple
krauss@23805
    97
  proof about @{const sep} and @{const map}
krauss@23805
    98
*}
krauss@23805
    99
krauss@23805
   100
lemma "map f (sep x ys) = sep (f x) (map f ys)"
krauss@23805
   101
apply (induct x ys rule: sep.induct)
krauss@23805
   102
krauss@23805
   103
txt {*
krauss@23805
   104
  We get three cases, like in the definition.
krauss@23805
   105
krauss@23805
   106
  @{subgoals [display]}
krauss@23805
   107
*}
krauss@23805
   108
krauss@23805
   109
apply auto 
krauss@23805
   110
done
krauss@23805
   111
text {*
krauss@23188
   112
krauss@23188
   113
  With the \cmd{fun} command, you can define about 80\% of the
krauss@23188
   114
  functions that occur in practice. The rest of this tutorial explains
krauss@23188
   115
  the remaining 20\%.
krauss@22065
   116
*}
krauss@21212
   117
krauss@22065
   118
krauss@23188
   119
section {* fun vs.\ function *}
krauss@21212
   120
krauss@21212
   121
text {* 
krauss@23188
   122
  The \cmd{fun} command provides a
krauss@21212
   123
  convenient shorthand notation for simple function definitions. In
krauss@21212
   124
  this mode, Isabelle tries to solve all the necessary proof obligations
krauss@27026
   125
  automatically. If any proof fails, the definition is
krauss@22065
   126
  rejected. This can either mean that the definition is indeed faulty,
krauss@22065
   127
  or that the default proof procedures are just not smart enough (or
krauss@22065
   128
  rather: not designed) to handle the definition.
krauss@21212
   129
krauss@23188
   130
  By expanding the abbreviation to the more verbose \cmd{function} command, these proof obligations become visible and can be analyzed or
krauss@23188
   131
  solved manually. The expansion from \cmd{fun} to \cmd{function} is as follows:
krauss@21212
   132
krauss@22065
   133
\end{isamarkuptext}
krauss@21212
   134
krauss@21212
   135
krauss@23188
   136
\[\left[\;\begin{minipage}{0.25\textwidth}\vspace{6pt}
krauss@23188
   137
\cmd{fun} @{text "f :: \<tau>"}\\%
krauss@23188
   138
\cmd{where}\\%
krauss@23188
   139
\hspace*{2ex}{\it equations}\\%
krauss@23188
   140
\hspace*{2ex}\vdots\vspace*{6pt}
krauss@23188
   141
\end{minipage}\right]
krauss@23188
   142
\quad\equiv\quad
krauss@27026
   143
\left[\;\begin{minipage}{0.48\textwidth}\vspace{6pt}
krauss@23188
   144
\cmd{function} @{text "("}\cmd{sequential}@{text ") f :: \<tau>"}\\%
krauss@23188
   145
\cmd{where}\\%
krauss@23188
   146
\hspace*{2ex}{\it equations}\\%
krauss@23188
   147
\hspace*{2ex}\vdots\\%
krauss@22065
   148
\cmd{by} @{text "pat_completeness auto"}\\%
krauss@23188
   149
\cmd{termination by} @{text "lexicographic_order"}\vspace{6pt}
krauss@23188
   150
\end{minipage}
krauss@23188
   151
\right]\]
krauss@22065
   152
krauss@22065
   153
\begin{isamarkuptext}
krauss@22065
   154
  \vspace*{1em}
krauss@23188
   155
  \noindent Some details have now become explicit:
krauss@21212
   156
krauss@21212
   157
  \begin{enumerate}
krauss@22065
   158
  \item The \cmd{sequential} option enables the preprocessing of
krauss@23805
   159
  pattern overlaps which we already saw. Without this option, the equations
krauss@21212
   160
  must already be disjoint and complete. The automatic completion only
krauss@23188
   161
  works with constructor patterns.
krauss@21212
   162
krauss@23188
   163
  \item A function definition produces a proof obligation which
krauss@23188
   164
  expresses completeness and compatibility of patterns (we talk about
krauss@22065
   165
  this later). The combination of the methods @{text "pat_completeness"} and
krauss@22065
   166
  @{text "auto"} is used to solve this proof obligation.
krauss@21212
   167
krauss@21212
   168
  \item A termination proof follows the definition, started by the
krauss@23188
   169
  \cmd{termination} command. This will be explained in \S\ref{termination}.
krauss@22065
   170
 \end{enumerate}
krauss@22065
   171
  Whenever a \cmd{fun} command fails, it is usually a good idea to
krauss@22065
   172
  expand the syntax to the more verbose \cmd{function} form, to see
krauss@22065
   173
  what is actually going on.
krauss@21212
   174
 *}
krauss@21212
   175
krauss@21212
   176
krauss@23188
   177
section {* Termination *}
krauss@21212
   178
krauss@23188
   179
text {*\label{termination}
krauss@23805
   180
  The method @{text "lexicographic_order"} is the default method for
krauss@23805
   181
  termination proofs. It can prove termination of a
krauss@23188
   182
  certain class of functions by searching for a suitable lexicographic
krauss@23188
   183
  combination of size measures. Of course, not all functions have such
krauss@23805
   184
  a simple termination argument. For them, we can specify the termination
krauss@23805
   185
  relation manually.
krauss@23188
   186
*}
krauss@23188
   187
krauss@23188
   188
subsection {* The {\tt relation} method *}
krauss@23188
   189
text{*
krauss@21212
   190
  Consider the following function, which sums up natural numbers up to
krauss@22065
   191
  @{text "N"}, using a counter @{text "i"}:
krauss@21212
   192
*}
krauss@21212
   193
krauss@21212
   194
function sum :: "nat \<Rightarrow> nat \<Rightarrow> nat"
krauss@21212
   195
where
krauss@21212
   196
  "sum i N = (if i > N then 0 else i + sum (Suc i) N)"
krauss@22065
   197
by pat_completeness auto
krauss@21212
   198
krauss@21212
   199
text {*
krauss@22065
   200
  \noindent The @{text "lexicographic_order"} method fails on this example, because none of the
krauss@23805
   201
  arguments decreases in the recursive call, with respect to the standard size ordering.
krauss@23805
   202
  To prove termination manually, we must provide a custom wellfounded relation.
krauss@21212
   203
krauss@21212
   204
  The termination argument for @{text "sum"} is based on the fact that
krauss@21212
   205
  the \emph{difference} between @{text "i"} and @{text "N"} gets
krauss@21212
   206
  smaller in every step, and that the recursion stops when @{text "i"}
krauss@23805
   207
  is greater than @{text "N"}. Phrased differently, the expression 
krauss@23805
   208
  @{text "N + 1 - i"} always decreases.
krauss@21212
   209
krauss@22065
   210
  We can use this expression as a measure function suitable to prove termination.
krauss@21212
   211
*}
krauss@21212
   212
krauss@27026
   213
termination sum
krauss@23188
   214
apply (relation "measure (\<lambda>(i,N). N + 1 - i)")
krauss@21212
   215
krauss@23188
   216
txt {*
krauss@23003
   217
  The \cmd{termination} command sets up the termination goal for the
krauss@23188
   218
  specified function @{text "sum"}. If the function name is omitted, it
krauss@23003
   219
  implicitly refers to the last function definition.
krauss@23003
   220
krauss@22065
   221
  The @{text relation} method takes a relation of
krauss@22065
   222
  type @{typ "('a \<times> 'a) set"}, where @{typ "'a"} is the argument type of
krauss@22065
   223
  the function. If the function has multiple curried arguments, then
krauss@22065
   224
  these are packed together into a tuple, as it happened in the above
krauss@22065
   225
  example.
krauss@21212
   226
krauss@27026
   227
  The predefined function @{term[source] "measure :: ('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set"} constructs a
krauss@23188
   228
  wellfounded relation from a mapping into the natural numbers (a
krauss@23188
   229
  \emph{measure function}). 
krauss@22065
   230
krauss@22065
   231
  After the invocation of @{text "relation"}, we must prove that (a)
krauss@22065
   232
  the relation we supplied is wellfounded, and (b) that the arguments
krauss@22065
   233
  of recursive calls indeed decrease with respect to the
krauss@23188
   234
  relation:
krauss@22065
   235
krauss@23188
   236
  @{subgoals[display,indent=0]}
krauss@23188
   237
krauss@23188
   238
  These goals are all solved by @{text "auto"}:
krauss@23188
   239
*}
krauss@23188
   240
krauss@23188
   241
apply auto
krauss@23188
   242
done
krauss@23188
   243
krauss@23188
   244
text {*
krauss@22065
   245
  Let us complicate the function a little, by adding some more
krauss@22065
   246
  recursive calls: 
krauss@21212
   247
*}
krauss@21212
   248
krauss@21212
   249
function foo :: "nat \<Rightarrow> nat \<Rightarrow> nat"
krauss@21212
   250
where
krauss@21212
   251
  "foo i N = (if i > N 
krauss@21212
   252
              then (if N = 0 then 0 else foo 0 (N - 1))
krauss@21212
   253
              else i + foo (Suc i) N)"
krauss@21212
   254
by pat_completeness auto
krauss@21212
   255
krauss@21212
   256
text {*
krauss@21212
   257
  When @{text "i"} has reached @{text "N"}, it starts at zero again
krauss@21212
   258
  and @{text "N"} is decremented.
krauss@21212
   259
  This corresponds to a nested
krauss@21212
   260
  loop where one index counts up and the other down. Termination can
krauss@21212
   261
  be proved using a lexicographic combination of two measures, namely
krauss@22065
   262
  the value of @{text "N"} and the above difference. The @{const
krauss@22065
   263
  "measures"} combinator generalizes @{text "measure"} by taking a
krauss@22065
   264
  list of measure functions.  
krauss@21212
   265
*}
krauss@21212
   266
krauss@21212
   267
termination 
krauss@22065
   268
by (relation "measures [\<lambda>(i, N). N, \<lambda>(i,N). N + 1 - i]") auto
krauss@21212
   269
krauss@23188
   270
subsection {* How @{text "lexicographic_order"} works *}
krauss@23188
   271
krauss@23188
   272
(*fun fails :: "nat \<Rightarrow> nat list \<Rightarrow> nat"
krauss@23188
   273
where
krauss@23188
   274
  "fails a [] = a"
krauss@23188
   275
| "fails a (x#xs) = fails (x + a) (x # xs)"
krauss@23188
   276
*)
krauss@23003
   277
krauss@23003
   278
text {*
krauss@23188
   279
  To see how the automatic termination proofs work, let's look at an
krauss@23188
   280
  example where it fails\footnote{For a detailed discussion of the
krauss@23188
   281
  termination prover, see \cite{bulwahnKN07}}:
krauss@23188
   282
krauss@23188
   283
\end{isamarkuptext}  
krauss@23188
   284
\cmd{fun} @{text "fails :: \"nat \<Rightarrow> nat list \<Rightarrow> nat\""}\\%
krauss@23188
   285
\cmd{where}\\%
krauss@23188
   286
\hspace*{2ex}@{text "\"fails a [] = a\""}\\%
krauss@23188
   287
|\hspace*{1.5ex}@{text "\"fails a (x#xs) = fails (x + a) (x#xs)\""}\\
krauss@23188
   288
\begin{isamarkuptext}
krauss@23188
   289
krauss@23188
   290
\noindent Isabelle responds with the following error:
krauss@23188
   291
krauss@23188
   292
\begin{isabelle}
krauss@23805
   293
*** Unfinished subgoals:\newline
krauss@23805
   294
*** (a, 1, <):\newline
krauss@23805
   295
*** \ 1.~@{text "\<And>x. x = 0"}\newline
krauss@23805
   296
*** (a, 1, <=):\newline
krauss@23805
   297
*** \ 1.~False\newline
krauss@23805
   298
*** (a, 2, <):\newline
krauss@23805
   299
*** \ 1.~False\newline
krauss@23188
   300
*** Calls:\newline
krauss@23188
   301
*** a) @{text "(a, x # xs) -->> (x + a, x # xs)"}\newline
krauss@23188
   302
*** Measures:\newline
krauss@23188
   303
*** 1) @{text "\<lambda>x. size (fst x)"}\newline
krauss@23188
   304
*** 2) @{text "\<lambda>x. size (snd x)"}\newline
krauss@23805
   305
*** Result matrix:\newline
krauss@23805
   306
*** \ \ \ \ 1\ \ 2  \newline
krauss@23805
   307
*** a:  ?   <= \newline
krauss@23805
   308
*** Could not find lexicographic termination order.\newline
krauss@23188
   309
*** At command "fun".\newline
krauss@23188
   310
\end{isabelle}
krauss@23003
   311
*}
krauss@23188
   312
text {*
krauss@28040
   313
  The key to this error message is the matrix at the bottom. The rows
krauss@23188
   314
  of that matrix correspond to the different recursive calls (In our
krauss@23188
   315
  case, there is just one). The columns are the function's arguments 
krauss@23188
   316
  (expressed through different measure functions, which map the
krauss@23188
   317
  argument tuple to a natural number). 
krauss@23188
   318
krauss@23188
   319
  The contents of the matrix summarize what is known about argument
krauss@23188
   320
  descents: The second argument has a weak descent (@{text "<="}) at the
krauss@23188
   321
  recursive call, and for the first argument nothing could be proved,
krauss@23805
   322
  which is expressed by @{text "?"}. In general, there are the values
krauss@23805
   323
  @{text "<"}, @{text "<="} and @{text "?"}.
krauss@23188
   324
krauss@23188
   325
  For the failed proof attempts, the unfinished subgoals are also
krauss@23805
   326
  printed. Looking at these will often point to a missing lemma.
krauss@33856
   327
*}
krauss@23188
   328
krauss@33856
   329
subsection {* The @{text size_change} method *}
krauss@33856
   330
krauss@33856
   331
text {*
krauss@33856
   332
  Some termination goals that are beyond the powers of
krauss@33856
   333
  @{text lexicographic_order} can be solved automatically by the
krauss@33856
   334
  more powerful @{text size_change} method, which uses a variant of
krauss@33856
   335
  the size-change principle, together with some other
krauss@33856
   336
  techniques. While the details are discussed
krauss@33856
   337
  elsewhere\cite{krauss_phd},
krauss@33856
   338
  here are a few typical situations where
krauss@33856
   339
  @{text lexicographic_order} has difficulties and @{text size_change}
krauss@33856
   340
  may be worth a try:
krauss@33856
   341
  \begin{itemize}
krauss@33856
   342
  \item Arguments are permuted in a recursive call.
krauss@33856
   343
  \item Several mutually recursive functions with multiple arguments.
krauss@33856
   344
  \item Unusual control flow (e.g., when some recursive calls cannot
krauss@33856
   345
  occur in sequence).
krauss@33856
   346
  \end{itemize}
krauss@33856
   347
krauss@33856
   348
  Loading the theory @{text Multiset} makes the @{text size_change}
krauss@33856
   349
  method a bit stronger: it can then use multiset orders internally.
krauss@23188
   350
*}
krauss@21212
   351
krauss@21212
   352
section {* Mutual Recursion *}
krauss@21212
   353
krauss@21212
   354
text {*
krauss@21212
   355
  If two or more functions call one another mutually, they have to be defined
krauss@23188
   356
  in one step. Here are @{text "even"} and @{text "odd"}:
krauss@21212
   357
*}
krauss@21212
   358
krauss@22065
   359
function even :: "nat \<Rightarrow> bool"
krauss@22065
   360
    and odd  :: "nat \<Rightarrow> bool"
krauss@21212
   361
where
krauss@21212
   362
  "even 0 = True"
krauss@21212
   363
| "odd 0 = False"
krauss@21212
   364
| "even (Suc n) = odd n"
krauss@21212
   365
| "odd (Suc n) = even n"
krauss@22065
   366
by pat_completeness auto
krauss@21212
   367
krauss@21212
   368
text {*
krauss@23188
   369
  To eliminate the mutual dependencies, Isabelle internally
krauss@21212
   370
  creates a single function operating on the sum
krauss@23188
   371
  type @{typ "nat + nat"}. Then, @{const even} and @{const odd} are
krauss@23188
   372
  defined as projections. Consequently, termination has to be proved
krauss@21212
   373
  simultaneously for both functions, by specifying a measure on the
krauss@21212
   374
  sum type: 
krauss@21212
   375
*}
krauss@21212
   376
krauss@21212
   377
termination 
krauss@23188
   378
by (relation "measure (\<lambda>x. case x of Inl n \<Rightarrow> n | Inr n \<Rightarrow> n)") auto
krauss@23188
   379
krauss@23188
   380
text {* 
krauss@23188
   381
  We could also have used @{text lexicographic_order}, which
krauss@23188
   382
  supports mutual recursive termination proofs to a certain extent.
krauss@23188
   383
*}
krauss@21212
   384
krauss@22065
   385
subsection {* Induction for mutual recursion *}
krauss@21212
   386
krauss@22065
   387
text {*
krauss@21212
   388
krauss@22065
   389
  When functions are mutually recursive, proving properties about them
krauss@23188
   390
  generally requires simultaneous induction. The induction rule @{text "even_odd.induct"}
krauss@23188
   391
  generated from the above definition reflects this.
krauss@21212
   392
krauss@22065
   393
  Let us prove something about @{const even} and @{const odd}:
krauss@22065
   394
*}
krauss@21212
   395
krauss@23188
   396
lemma even_odd_mod2:
krauss@22065
   397
  "even n = (n mod 2 = 0)"
krauss@22065
   398
  "odd n = (n mod 2 = 1)"
krauss@21212
   399
krauss@22065
   400
txt {* 
krauss@22065
   401
  We apply simultaneous induction, specifying the induction variable
krauss@22065
   402
  for both goals, separated by \cmd{and}:  *}
krauss@22065
   403
krauss@22065
   404
apply (induct n and n rule: even_odd.induct)
krauss@22065
   405
krauss@22065
   406
txt {* 
krauss@22065
   407
  We get four subgoals, which correspond to the clauses in the
krauss@22065
   408
  definition of @{const even} and @{const odd}:
krauss@22065
   409
  @{subgoals[display,indent=0]}
krauss@22065
   410
  Simplification solves the first two goals, leaving us with two
krauss@22065
   411
  statements about the @{text "mod"} operation to prove:
krauss@22065
   412
*}
krauss@22065
   413
krauss@22065
   414
apply simp_all
krauss@22065
   415
krauss@22065
   416
txt {* 
krauss@22065
   417
  @{subgoals[display,indent=0]} 
krauss@22065
   418
krauss@23805
   419
  \noindent These can be handled by Isabelle's arithmetic decision procedures.
krauss@22065
   420
  
krauss@22065
   421
*}
krauss@22065
   422
krauss@23805
   423
apply arith
krauss@23805
   424
apply arith
krauss@22065
   425
done
krauss@22065
   426
krauss@22065
   427
text {*
krauss@23188
   428
  In proofs like this, the simultaneous induction is really essential:
krauss@23188
   429
  Even if we are just interested in one of the results, the other
krauss@23188
   430
  one is necessary to strengthen the induction hypothesis. If we leave
krauss@27026
   431
  out the statement about @{const odd} and just write @{term True} instead,
krauss@27026
   432
  the same proof fails:
krauss@22065
   433
*}
krauss@22065
   434
krauss@23188
   435
lemma failed_attempt:
krauss@22065
   436
  "even n = (n mod 2 = 0)"
krauss@22065
   437
  "True"
krauss@22065
   438
apply (induct n rule: even_odd.induct)
krauss@22065
   439
krauss@22065
   440
txt {*
krauss@22065
   441
  \noindent Now the third subgoal is a dead end, since we have no
krauss@23188
   442
  useful induction hypothesis available:
krauss@22065
   443
krauss@22065
   444
  @{subgoals[display,indent=0]} 
krauss@22065
   445
*}
krauss@22065
   446
krauss@22065
   447
oops
krauss@22065
   448
krauss@23188
   449
section {* General pattern matching *}
krauss@23805
   450
text{*\label{genpats} *}
krauss@22065
   451
krauss@23188
   452
subsection {* Avoiding automatic pattern splitting *}
krauss@22065
   453
krauss@22065
   454
text {*
krauss@22065
   455
krauss@22065
   456
  Up to now, we used pattern matching only on datatypes, and the
krauss@22065
   457
  patterns were always disjoint and complete, and if they weren't,
krauss@22065
   458
  they were made disjoint automatically like in the definition of
krauss@22065
   459
  @{const "sep"} in \S\ref{patmatch}.
krauss@22065
   460
krauss@23188
   461
  This automatic splitting can significantly increase the number of
krauss@23188
   462
  equations involved, and this is not always desirable. The following
krauss@23188
   463
  example shows the problem:
krauss@22065
   464
  
krauss@23805
   465
  Suppose we are modeling incomplete knowledge about the world by a
krauss@23003
   466
  three-valued datatype, which has values @{term "T"}, @{term "F"}
krauss@23003
   467
  and @{term "X"} for true, false and uncertain propositions, respectively. 
krauss@22065
   468
*}
krauss@22065
   469
krauss@22065
   470
datatype P3 = T | F | X
krauss@22065
   471
krauss@23188
   472
text {* \noindent Then the conjunction of such values can be defined as follows: *}
krauss@22065
   473
krauss@22065
   474
fun And :: "P3 \<Rightarrow> P3 \<Rightarrow> P3"
krauss@22065
   475
where
krauss@22065
   476
  "And T p = p"
krauss@23003
   477
| "And p T = p"
krauss@23003
   478
| "And p F = F"
krauss@23003
   479
| "And F p = F"
krauss@23003
   480
| "And X X = X"
krauss@22065
   481
krauss@22065
   482
krauss@22065
   483
text {* 
krauss@22065
   484
  This definition is useful, because the equations can directly be used
krauss@28040
   485
  as simplification rules. But the patterns overlap: For example,
krauss@23188
   486
  the expression @{term "And T T"} is matched by both the first and
krauss@23188
   487
  the second equation. By default, Isabelle makes the patterns disjoint by
krauss@22065
   488
  splitting them up, producing instances:
krauss@22065
   489
*}
krauss@22065
   490
krauss@22065
   491
thm And.simps
krauss@22065
   492
krauss@22065
   493
text {*
krauss@22065
   494
  @{thm[indent=4] And.simps}
krauss@22065
   495
  
krauss@22065
   496
  \vspace*{1em}
krauss@23003
   497
  \noindent There are several problems with this:
krauss@22065
   498
krauss@22065
   499
  \begin{enumerate}
krauss@23188
   500
  \item If the datatype has many constructors, there can be an
krauss@22065
   501
  explosion of equations. For @{const "And"}, we get seven instead of
krauss@23003
   502
  five equations, which can be tolerated, but this is just a small
krauss@22065
   503
  example.
krauss@22065
   504
krauss@23188
   505
  \item Since splitting makes the equations \qt{less general}, they
krauss@22065
   506
  do not always match in rewriting. While the term @{term "And x F"}
krauss@23188
   507
  can be simplified to @{term "F"} with the original equations, a
krauss@22065
   508
  (manual) case split on @{term "x"} is now necessary.
krauss@22065
   509
krauss@22065
   510
  \item The splitting also concerns the induction rule @{text
krauss@22065
   511
  "And.induct"}. Instead of five premises it now has seven, which
krauss@22065
   512
  means that our induction proofs will have more cases.
krauss@22065
   513
krauss@22065
   514
  \item In general, it increases clarity if we get the same definition
krauss@22065
   515
  back which we put in.
krauss@22065
   516
  \end{enumerate}
krauss@22065
   517
krauss@23188
   518
  If we do not want the automatic splitting, we can switch it off by
krauss@23188
   519
  leaving out the \cmd{sequential} option. However, we will have to
krauss@23188
   520
  prove that our pattern matching is consistent\footnote{This prevents
krauss@23188
   521
  us from defining something like @{term "f x = True"} and @{term "f x
krauss@23188
   522
  = False"} simultaneously.}:
krauss@22065
   523
*}
krauss@22065
   524
krauss@22065
   525
function And2 :: "P3 \<Rightarrow> P3 \<Rightarrow> P3"
krauss@22065
   526
where
krauss@22065
   527
  "And2 T p = p"
krauss@23003
   528
| "And2 p T = p"
krauss@23003
   529
| "And2 p F = F"
krauss@23003
   530
| "And2 F p = F"
krauss@23003
   531
| "And2 X X = X"
krauss@22065
   532
krauss@22065
   533
txt {*
krauss@23188
   534
  \noindent Now let's look at the proof obligations generated by a
krauss@22065
   535
  function definition. In this case, they are:
krauss@22065
   536
krauss@23188
   537
  @{subgoals[display,indent=0]}\vspace{-1.2em}\hspace{3cm}\vdots\vspace{1.2em}
krauss@22065
   538
krauss@22065
   539
  The first subgoal expresses the completeness of the patterns. It has
krauss@22065
   540
  the form of an elimination rule and states that every @{term x} of
krauss@23188
   541
  the function's input type must match at least one of the patterns\footnote{Completeness could
krauss@22065
   542
  be equivalently stated as a disjunction of existential statements: 
krauss@22065
   543
@{term "(\<exists>p. x = (T, p)) \<or> (\<exists>p. x = (p, T)) \<or> (\<exists>p. x = (p, F)) \<or>
krauss@27026
   544
  (\<exists>p. x = (F, p)) \<or> (x = (X, X))"}, and you can use the method @{text atomize_elim} to get that form instead.}. If the patterns just involve
krauss@23188
   545
  datatypes, we can solve it with the @{text "pat_completeness"}
krauss@23188
   546
  method:
krauss@22065
   547
*}
krauss@22065
   548
krauss@22065
   549
apply pat_completeness
krauss@22065
   550
krauss@22065
   551
txt {*
krauss@22065
   552
  The remaining subgoals express \emph{pattern compatibility}. We do
krauss@23188
   553
  allow that an input value matches multiple patterns, but in this
krauss@22065
   554
  case, the result (i.e.~the right hand sides of the equations) must
krauss@22065
   555
  also be equal. For each pair of two patterns, there is one such
krauss@22065
   556
  subgoal. Usually this needs injectivity of the constructors, which
krauss@22065
   557
  is used automatically by @{text "auto"}.
krauss@22065
   558
*}
krauss@22065
   559
krauss@22065
   560
by auto
krauss@22065
   561
krauss@22065
   562
krauss@22065
   563
subsection {* Non-constructor patterns *}
krauss@21212
   564
krauss@23188
   565
text {*
krauss@23805
   566
  Most of Isabelle's basic types take the form of inductive datatypes,
krauss@23805
   567
  and usually pattern matching works on the constructors of such types. 
krauss@23805
   568
  However, this need not be always the case, and the \cmd{function}
krauss@23805
   569
  command handles other kind of patterns, too.
krauss@21212
   570
krauss@23805
   571
  One well-known instance of non-constructor patterns are
krauss@23188
   572
  so-called \emph{$n+k$-patterns}, which are a little controversial in
krauss@23188
   573
  the functional programming world. Here is the initial fibonacci
krauss@23188
   574
  example with $n+k$-patterns:
krauss@23188
   575
*}
krauss@23188
   576
krauss@23188
   577
function fib2 :: "nat \<Rightarrow> nat"
krauss@23188
   578
where
krauss@23188
   579
  "fib2 0 = 1"
krauss@23188
   580
| "fib2 1 = 1"
krauss@23188
   581
| "fib2 (n + 2) = fib2 n + fib2 (Suc n)"
krauss@23188
   582
krauss@26749
   583
(*<*)ML_val "goals_limit := 1"(*>*)
krauss@23188
   584
txt {*
krauss@23805
   585
  This kind of matching is again justified by the proof of pattern
krauss@23805
   586
  completeness and compatibility. 
krauss@23188
   587
  The proof obligation for pattern completeness states that every natural number is
krauss@23188
   588
  either @{term "0::nat"}, @{term "1::nat"} or @{term "n +
krauss@23188
   589
  (2::nat)"}:
krauss@23188
   590
krauss@23188
   591
  @{subgoals[display,indent=0]}
krauss@23188
   592
krauss@23188
   593
  This is an arithmetic triviality, but unfortunately the
krauss@23188
   594
  @{text arith} method cannot handle this specific form of an
krauss@23805
   595
  elimination rule. However, we can use the method @{text
krauss@26580
   596
  "atomize_elim"} to do an ad-hoc conversion to a disjunction of
krauss@28040
   597
  existentials, which can then be solved by the arithmetic decision procedure.
krauss@23805
   598
  Pattern compatibility and termination are automatic as usual.
krauss@23188
   599
*}
krauss@26749
   600
(*<*)ML_val "goals_limit := 10"(*>*)
krauss@26580
   601
apply atomize_elim
krauss@23805
   602
apply arith
krauss@23188
   603
apply auto
krauss@23188
   604
done
krauss@23188
   605
termination by lexicographic_order
krauss@23188
   606
text {*
krauss@23188
   607
  We can stretch the notion of pattern matching even more. The
krauss@23188
   608
  following function is not a sensible functional program, but a
krauss@23188
   609
  perfectly valid mathematical definition:
krauss@23188
   610
*}
krauss@23188
   611
krauss@23188
   612
function ev :: "nat \<Rightarrow> bool"
krauss@23188
   613
where
krauss@23188
   614
  "ev (2 * n) = True"
krauss@23188
   615
| "ev (2 * n + 1) = False"
krauss@26580
   616
apply atomize_elim
krauss@23805
   617
by arith+
krauss@23188
   618
termination by (relation "{}") simp
krauss@23188
   619
krauss@23188
   620
text {*
krauss@27026
   621
  This general notion of pattern matching gives you a certain freedom
krauss@27026
   622
  in writing down specifications. However, as always, such freedom should
krauss@23188
   623
  be used with care:
krauss@23188
   624
krauss@23188
   625
  If we leave the area of constructor
krauss@23188
   626
  patterns, we have effectively departed from the world of functional
krauss@23188
   627
  programming. This means that it is no longer possible to use the
krauss@23188
   628
  code generator, and expect it to generate ML code for our
krauss@23188
   629
  definitions. Also, such a specification might not work very well together with
krauss@23188
   630
  simplification. Your mileage may vary.
krauss@23188
   631
*}
krauss@23188
   632
krauss@23188
   633
krauss@23188
   634
subsection {* Conditional equations *}
krauss@23188
   635
krauss@23188
   636
text {* 
krauss@23188
   637
  The function package also supports conditional equations, which are
krauss@23188
   638
  similar to guards in a language like Haskell. Here is Euclid's
krauss@23188
   639
  algorithm written with conditional patterns\footnote{Note that the
krauss@23188
   640
  patterns are also overlapping in the base case}:
krauss@23188
   641
*}
krauss@23188
   642
krauss@23188
   643
function gcd :: "nat \<Rightarrow> nat \<Rightarrow> nat"
krauss@23188
   644
where
krauss@23188
   645
  "gcd x 0 = x"
krauss@23188
   646
| "gcd 0 y = y"
krauss@23188
   647
| "x < y \<Longrightarrow> gcd (Suc x) (Suc y) = gcd (Suc x) (y - x)"
krauss@23188
   648
| "\<not> x < y \<Longrightarrow> gcd (Suc x) (Suc y) = gcd (x - y) (Suc y)"
krauss@26580
   649
by (atomize_elim, auto, arith)
krauss@23188
   650
termination by lexicographic_order
krauss@23188
   651
krauss@23188
   652
text {*
krauss@23188
   653
  By now, you can probably guess what the proof obligations for the
krauss@23188
   654
  pattern completeness and compatibility look like. 
krauss@23188
   655
krauss@23188
   656
  Again, functions with conditional patterns are not supported by the
krauss@23188
   657
  code generator.
krauss@23188
   658
*}
krauss@23188
   659
krauss@23188
   660
krauss@23188
   661
subsection {* Pattern matching on strings *}
krauss@23188
   662
krauss@23188
   663
text {*
krauss@23805
   664
  As strings (as lists of characters) are normal datatypes, pattern
krauss@23188
   665
  matching on them is possible, but somewhat problematic. Consider the
krauss@23188
   666
  following definition:
krauss@23188
   667
krauss@23188
   668
\end{isamarkuptext}
krauss@23188
   669
\noindent\cmd{fun} @{text "check :: \"string \<Rightarrow> bool\""}\\%
krauss@23188
   670
\cmd{where}\\%
krauss@23188
   671
\hspace*{2ex}@{text "\"check (''good'') = True\""}\\%
krauss@23188
   672
@{text "| \"check s = False\""}
krauss@23188
   673
\begin{isamarkuptext}
krauss@23188
   674
krauss@23805
   675
  \noindent An invocation of the above \cmd{fun} command does not
krauss@23188
   676
  terminate. What is the problem? Strings are lists of characters, and
krauss@23805
   677
  characters are a datatype with a lot of constructors. Splitting the
krauss@23188
   678
  catch-all pattern thus leads to an explosion of cases, which cannot
krauss@23188
   679
  be handled by Isabelle.
krauss@23188
   680
krauss@23188
   681
  There are two things we can do here. Either we write an explicit
krauss@23188
   682
  @{text "if"} on the right hand side, or we can use conditional patterns:
krauss@23188
   683
*}
krauss@23188
   684
krauss@23188
   685
function check :: "string \<Rightarrow> bool"
krauss@23188
   686
where
krauss@23188
   687
  "check (''good'') = True"
krauss@23188
   688
| "s \<noteq> ''good'' \<Longrightarrow> check s = False"
krauss@23188
   689
by auto
krauss@23188
   690
krauss@23188
   691
krauss@22065
   692
section {* Partiality *}
krauss@22065
   693
krauss@22065
   694
text {* 
krauss@22065
   695
  In HOL, all functions are total. A function @{term "f"} applied to
krauss@23188
   696
  @{term "x"} always has the value @{term "f x"}, and there is no notion
krauss@22065
   697
  of undefinedness. 
krauss@23188
   698
  This is why we have to do termination
krauss@23188
   699
  proofs when defining functions: The proof justifies that the
krauss@23188
   700
  function can be defined by wellfounded recursion.
krauss@22065
   701
krauss@23188
   702
  However, the \cmd{function} package does support partiality to a
krauss@23188
   703
  certain extent. Let's look at the following function which looks
krauss@23188
   704
  for a zero of a given function f. 
krauss@22065
   705
*}
krauss@22065
   706
krauss@23003
   707
function (*<*)(domintros, tailrec)(*>*)findzero :: "(nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat"
krauss@23003
   708
where
krauss@23003
   709
  "findzero f n = (if f n = 0 then n else findzero f (Suc n))"
krauss@23003
   710
by pat_completeness auto
krauss@23003
   711
(*<*)declare findzero.simps[simp del](*>*)
krauss@22065
   712
krauss@23003
   713
text {*
krauss@23805
   714
  \noindent Clearly, any attempt of a termination proof must fail. And without
krauss@29718
   715
  that, we do not get the usual rules @{text "findzero.simps"} and 
krauss@23003
   716
  @{text "findzero.induct"}. So what was the definition good for at all?
krauss@23003
   717
*}
krauss@23003
   718
krauss@23003
   719
subsection {* Domain predicates *}
krauss@23003
   720
krauss@23003
   721
text {*
krauss@23003
   722
  The trick is that Isabelle has not only defined the function @{const findzero}, but also
krauss@23003
   723
  a predicate @{term "findzero_dom"} that characterizes the values where the function
krauss@23188
   724
  terminates: the \emph{domain} of the function. If we treat a
krauss@23188
   725
  partial function just as a total function with an additional domain
krauss@23188
   726
  predicate, we can derive simplification and
krauss@23188
   727
  induction rules as we do for total functions. They are guarded
krauss@23188
   728
  by domain conditions and are called @{text psimps} and @{text
krauss@23188
   729
  pinduct}: 
krauss@23003
   730
*}
krauss@23003
   731
krauss@23805
   732
text {*
krauss@23805
   733
  \noindent\begin{minipage}{0.79\textwidth}@{thm[display,margin=85] findzero.psimps}\end{minipage}
krauss@23805
   734
  \hfill(@{text "findzero.psimps"})
krauss@23805
   735
  \vspace{1em}
krauss@23003
   736
krauss@23805
   737
  \noindent\begin{minipage}{0.79\textwidth}@{thm[display,margin=85] findzero.pinduct}\end{minipage}
krauss@23805
   738
  \hfill(@{text "findzero.pinduct"})
krauss@23003
   739
*}
krauss@23003
   740
krauss@23003
   741
text {*
krauss@23188
   742
  Remember that all we
krauss@23188
   743
  are doing here is use some tricks to make a total function appear
krauss@23003
   744
  as if it was partial. We can still write the term @{term "findzero
krauss@23003
   745
  (\<lambda>x. 1) 0"} and like any other term of type @{typ nat} it is equal
krauss@23003
   746
  to some natural number, although we might not be able to find out
krauss@23188
   747
  which one. The function is \emph{underdefined}.
krauss@23003
   748
krauss@23805
   749
  But it is defined enough to prove something interesting about it. We
krauss@23188
   750
  can prove that if @{term "findzero f n"}
krauss@23805
   751
  terminates, it indeed returns a zero of @{term f}:
krauss@23003
   752
*}
krauss@23003
   753
krauss@23003
   754
lemma findzero_zero: "findzero_dom (f, n) \<Longrightarrow> f (findzero f n) = 0"
krauss@23003
   755
krauss@23805
   756
txt {* \noindent We apply induction as usual, but using the partial induction
krauss@23003
   757
  rule: *}
krauss@23003
   758
krauss@23003
   759
apply (induct f n rule: findzero.pinduct)
krauss@23003
   760
krauss@23805
   761
txt {* \noindent This gives the following subgoals:
krauss@23003
   762
krauss@23003
   763
  @{subgoals[display,indent=0]}
krauss@23003
   764
krauss@23805
   765
  \noindent The hypothesis in our lemma was used to satisfy the first premise in
krauss@23188
   766
  the induction rule. However, we also get @{term
krauss@23188
   767
  "findzero_dom (f, n)"} as a local assumption in the induction step. This
krauss@23003
   768
  allows to unfold @{term "findzero f n"} using the @{text psimps}
krauss@23188
   769
  rule, and the rest is trivial. Since the @{text psimps} rules carry the
krauss@23003
   770
  @{text "[simp]"} attribute by default, we just need a single step:
krauss@23003
   771
 *}
krauss@23003
   772
apply simp
krauss@23003
   773
done
krauss@23003
   774
krauss@23003
   775
text {*
krauss@23003
   776
  Proofs about partial functions are often not harder than for total
krauss@23003
   777
  functions. Fig.~\ref{findzero_isar} shows a slightly more
krauss@23003
   778
  complicated proof written in Isar. It is verbose enough to show how
krauss@23003
   779
  partiality comes into play: From the partial induction, we get an
krauss@23003
   780
  additional domain condition hypothesis. Observe how this condition
krauss@23003
   781
  is applied when calls to @{term findzero} are unfolded.
krauss@23003
   782
*}
krauss@23003
   783
krauss@23003
   784
text_raw {*
krauss@23003
   785
\begin{figure}
krauss@23188
   786
\hrule\vspace{6pt}
krauss@23003
   787
\begin{minipage}{0.8\textwidth}
krauss@23003
   788
\isabellestyle{it}
krauss@23003
   789
\isastyle\isamarkuptrue
krauss@23003
   790
*}
krauss@23003
   791
lemma "\<lbrakk>findzero_dom (f, n); x \<in> {n ..< findzero f n}\<rbrakk> \<Longrightarrow> f x \<noteq> 0"
krauss@23003
   792
proof (induct rule: findzero.pinduct)
krauss@23003
   793
  fix f n assume dom: "findzero_dom (f, n)"
krauss@23188
   794
               and IH: "\<lbrakk>f n \<noteq> 0; x \<in> {Suc n ..< findzero f (Suc n)}\<rbrakk> \<Longrightarrow> f x \<noteq> 0"
krauss@23188
   795
               and x_range: "x \<in> {n ..< findzero f n}"
krauss@23003
   796
  have "f n \<noteq> 0"
krauss@23003
   797
  proof 
krauss@23003
   798
    assume "f n = 0"
krauss@23003
   799
    with dom have "findzero f n = n" by simp
krauss@23003
   800
    with x_range show False by auto
krauss@23003
   801
  qed
krauss@23003
   802
  
krauss@23003
   803
  from x_range have "x = n \<or> x \<in> {Suc n ..< findzero f n}" by auto
krauss@23003
   804
  thus "f x \<noteq> 0"
krauss@23003
   805
  proof
krauss@23003
   806
    assume "x = n"
krauss@23003
   807
    with `f n \<noteq> 0` show ?thesis by simp
krauss@23003
   808
  next
krauss@23188
   809
    assume "x \<in> {Suc n ..< findzero f n}"
krauss@23805
   810
    with dom and `f n \<noteq> 0` have "x \<in> {Suc n ..< findzero f (Suc n)}" by simp
krauss@23003
   811
    with IH and `f n \<noteq> 0`
krauss@23003
   812
    show ?thesis by simp
krauss@23003
   813
  qed
krauss@23003
   814
qed
krauss@23003
   815
text_raw {*
krauss@23003
   816
\isamarkupfalse\isabellestyle{tt}
krauss@23188
   817
\end{minipage}\vspace{6pt}\hrule
krauss@23003
   818
\caption{A proof about a partial function}\label{findzero_isar}
krauss@23003
   819
\end{figure}
krauss@23003
   820
*}
krauss@23003
   821
krauss@23003
   822
subsection {* Partial termination proofs *}
krauss@23003
   823
krauss@23003
   824
text {*
krauss@23003
   825
  Now that we have proved some interesting properties about our
krauss@23003
   826
  function, we should turn to the domain predicate and see if it is
krauss@23003
   827
  actually true for some values. Otherwise we would have just proved
krauss@23003
   828
  lemmas with @{term False} as a premise.
krauss@23003
   829
krauss@23003
   830
  Essentially, we need some introduction rules for @{text
krauss@23003
   831
  findzero_dom}. The function package can prove such domain
krauss@23003
   832
  introduction rules automatically. But since they are not used very
krauss@23188
   833
  often (they are almost never needed if the function is total), this
krauss@23188
   834
  functionality is disabled by default for efficiency reasons. So we have to go
krauss@23003
   835
  back and ask for them explicitly by passing the @{text
krauss@23003
   836
  "(domintros)"} option to the function package:
krauss@23003
   837
krauss@23188
   838
\vspace{1ex}
krauss@23003
   839
\noindent\cmd{function} @{text "(domintros) findzero :: \"(nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat\""}\\%
krauss@23003
   840
\cmd{where}\isanewline%
krauss@23003
   841
\ \ \ldots\\
krauss@23003
   842
krauss@23188
   843
  \noindent Now the package has proved an introduction rule for @{text findzero_dom}:
krauss@23003
   844
*}
krauss@23003
   845
krauss@23003
   846
thm findzero.domintros
krauss@23003
   847
krauss@23003
   848
text {*
krauss@23003
   849
  @{thm[display] findzero.domintros}
krauss@23003
   850
krauss@23003
   851
  Domain introduction rules allow to show that a given value lies in the
krauss@23003
   852
  domain of a function, if the arguments of all recursive calls
krauss@23003
   853
  are in the domain as well. They allow to do a \qt{single step} in a
krauss@23003
   854
  termination proof. Usually, you want to combine them with a suitable
krauss@23003
   855
  induction principle.
krauss@23003
   856
krauss@23003
   857
  Since our function increases its argument at recursive calls, we
krauss@23003
   858
  need an induction principle which works \qt{backwards}. We will use
krauss@23003
   859
  @{text inc_induct}, which allows to do induction from a fixed number
krauss@23003
   860
  \qt{downwards}:
krauss@23003
   861
krauss@23188
   862
  \begin{center}@{thm inc_induct}\hfill(@{text "inc_induct"})\end{center}
krauss@23003
   863
krauss@23188
   864
  Figure \ref{findzero_term} gives a detailed Isar proof of the fact
krauss@23003
   865
  that @{text findzero} terminates if there is a zero which is greater
krauss@23003
   866
  or equal to @{term n}. First we derive two useful rules which will
krauss@23003
   867
  solve the base case and the step case of the induction. The
krauss@23805
   868
  induction is then straightforward, except for the unusual induction
krauss@23003
   869
  principle.
krauss@23003
   870
krauss@23003
   871
*}
krauss@23003
   872
krauss@23003
   873
text_raw {*
krauss@23003
   874
\begin{figure}
krauss@23188
   875
\hrule\vspace{6pt}
krauss@23003
   876
\begin{minipage}{0.8\textwidth}
krauss@23003
   877
\isabellestyle{it}
krauss@23003
   878
\isastyle\isamarkuptrue
krauss@23003
   879
*}
krauss@23003
   880
lemma findzero_termination:
krauss@23188
   881
  assumes "x \<ge> n" and "f x = 0"
krauss@23003
   882
  shows "findzero_dom (f, n)"
krauss@23003
   883
proof - 
krauss@23003
   884
  have base: "findzero_dom (f, x)"
krauss@23003
   885
    by (rule findzero.domintros) (simp add:`f x = 0`)
krauss@23003
   886
krauss@23003
   887
  have step: "\<And>i. findzero_dom (f, Suc i) 
krauss@23003
   888
    \<Longrightarrow> findzero_dom (f, i)"
krauss@23003
   889
    by (rule findzero.domintros) simp
krauss@23003
   890
krauss@23188
   891
  from `x \<ge> n` show ?thesis
krauss@23003
   892
  proof (induct rule:inc_induct)
krauss@23188
   893
    show "findzero_dom (f, x)" by (rule base)
krauss@23003
   894
  next
krauss@23003
   895
    fix i assume "findzero_dom (f, Suc i)"
krauss@23188
   896
    thus "findzero_dom (f, i)" by (rule step)
krauss@23003
   897
  qed
krauss@23003
   898
qed      
krauss@23003
   899
text_raw {*
krauss@23003
   900
\isamarkupfalse\isabellestyle{tt}
krauss@23188
   901
\end{minipage}\vspace{6pt}\hrule
krauss@23003
   902
\caption{Termination proof for @{text findzero}}\label{findzero_term}
krauss@23003
   903
\end{figure}
krauss@23003
   904
*}
krauss@23003
   905
      
krauss@23003
   906
text {*
krauss@23003
   907
  Again, the proof given in Fig.~\ref{findzero_term} has a lot of
krauss@23003
   908
  detail in order to explain the principles. Using more automation, we
krauss@23003
   909
  can also have a short proof:
krauss@23003
   910
*}
krauss@23003
   911
krauss@23003
   912
lemma findzero_termination_short:
krauss@23003
   913
  assumes zero: "x >= n" 
krauss@23003
   914
  assumes [simp]: "f x = 0"
krauss@23003
   915
  shows "findzero_dom (f, n)"
krauss@23805
   916
using zero
krauss@23805
   917
by (induct rule:inc_induct) (auto intro: findzero.domintros)
krauss@23003
   918
    
krauss@23003
   919
text {*
krauss@23188
   920
  \noindent It is simple to combine the partial correctness result with the
krauss@23003
   921
  termination lemma:
krauss@23003
   922
*}
krauss@23003
   923
krauss@23003
   924
lemma findzero_total_correctness:
krauss@23003
   925
  "f x = 0 \<Longrightarrow> f (findzero f 0) = 0"
krauss@23003
   926
by (blast intro: findzero_zero findzero_termination)
krauss@23003
   927
krauss@23003
   928
subsection {* Definition of the domain predicate *}
krauss@23003
   929
krauss@23003
   930
text {*
krauss@23003
   931
  Sometimes it is useful to know what the definition of the domain
krauss@23805
   932
  predicate looks like. Actually, @{text findzero_dom} is just an
krauss@23003
   933
  abbreviation:
krauss@23003
   934
krauss@23003
   935
  @{abbrev[display] findzero_dom}
krauss@23003
   936
krauss@23188
   937
  The domain predicate is the \emph{accessible part} of a relation @{const
krauss@23003
   938
  findzero_rel}, which was also created internally by the function
krauss@23003
   939
  package. @{const findzero_rel} is just a normal
krauss@23188
   940
  inductive predicate, so we can inspect its definition by
krauss@23003
   941
  looking at the introduction rules @{text findzero_rel.intros}.
krauss@23003
   942
  In our case there is just a single rule:
krauss@23003
   943
krauss@23003
   944
  @{thm[display] findzero_rel.intros}
krauss@23003
   945
krauss@23188
   946
  The predicate @{const findzero_rel}
krauss@23003
   947
  describes the \emph{recursion relation} of the function
krauss@23003
   948
  definition. The recursion relation is a binary relation on
krauss@23003
   949
  the arguments of the function that relates each argument to its
krauss@23003
   950
  recursive calls. In general, there is one introduction rule for each
krauss@23003
   951
  recursive call.
krauss@23003
   952
krauss@23805
   953
  The predicate @{term "accp findzero_rel"} is the accessible part of
krauss@23003
   954
  that relation. An argument belongs to the accessible part, if it can
krauss@23188
   955
  be reached in a finite number of steps (cf.~its definition in @{text
krauss@29718
   956
  "Wellfounded.thy"}).
krauss@23003
   957
krauss@23003
   958
  Since the domain predicate is just an abbreviation, you can use
krauss@23805
   959
  lemmas for @{const accp} and @{const findzero_rel} directly. Some
krauss@23805
   960
  lemmas which are occasionally useful are @{text accpI}, @{text
krauss@23805
   961
  accp_downward}, and of course the introduction and elimination rules
krauss@23003
   962
  for the recursion relation @{text "findzero.intros"} and @{text "findzero.cases"}.
krauss@23003
   963
*}
krauss@23003
   964
krauss@23003
   965
(*lemma findzero_nicer_domintros:
krauss@23003
   966
  "f x = 0 \<Longrightarrow> findzero_dom (f, x)"
krauss@23003
   967
  "findzero_dom (f, Suc x) \<Longrightarrow> findzero_dom (f, x)"
krauss@23805
   968
by (rule accpI, erule findzero_rel.cases, auto)+
krauss@23003
   969
*)
krauss@23003
   970
  
krauss@23003
   971
subsection {* A Useful Special Case: Tail recursion *}
krauss@23003
   972
krauss@23003
   973
text {*
krauss@23003
   974
  The domain predicate is our trick that allows us to model partiality
krauss@23003
   975
  in a world of total functions. The downside of this is that we have
krauss@23003
   976
  to carry it around all the time. The termination proof above allowed
krauss@23003
   977
  us to replace the abstract @{term "findzero_dom (f, n)"} by the more
krauss@23188
   978
  concrete @{term "(x \<ge> n \<and> f x = (0::nat))"}, but the condition is still
krauss@23188
   979
  there and can only be discharged for special cases.
krauss@23188
   980
  In particular, the domain predicate guards the unfolding of our
krauss@23003
   981
  function, since it is there as a condition in the @{text psimp}
krauss@23003
   982
  rules. 
krauss@23003
   983
krauss@23003
   984
  Now there is an important special case: We can actually get rid
krauss@23003
   985
  of the condition in the simplification rules, \emph{if the function
krauss@23003
   986
  is tail-recursive}. The reason is that for all tail-recursive
krauss@23003
   987
  equations there is a total function satisfying them, even if they
krauss@23003
   988
  are non-terminating. 
krauss@23003
   989
krauss@23188
   990
%  A function is tail recursive, if each call to the function is either
krauss@23188
   991
%  equal
krauss@23188
   992
%
krauss@23188
   993
%  So the outer form of the 
krauss@23188
   994
%
krauss@23188
   995
%if it can be written in the following
krauss@23188
   996
%  form:
krauss@23188
   997
%  {term[display] "f x = (if COND x then BASE x else f (LOOP x))"}
krauss@23188
   998
krauss@23188
   999
krauss@23003
  1000
  The function package internally does the right construction and can
krauss@23003
  1001
  derive the unconditional simp rules, if we ask it to do so. Luckily,
krauss@23003
  1002
  our @{const "findzero"} function is tail-recursive, so we can just go
krauss@23003
  1003
  back and add another option to the \cmd{function} command:
krauss@23003
  1004
krauss@23188
  1005
\vspace{1ex}
krauss@23003
  1006
\noindent\cmd{function} @{text "(domintros, tailrec) findzero :: \"(nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat\""}\\%
krauss@23003
  1007
\cmd{where}\isanewline%
krauss@23003
  1008
\ \ \ldots\\%
krauss@22065
  1009
krauss@22065
  1010
  
krauss@23188
  1011
  \noindent Now, we actually get unconditional simplification rules, even
krauss@23003
  1012
  though the function is partial:
krauss@23003
  1013
*}
krauss@23003
  1014
krauss@23003
  1015
thm findzero.simps
krauss@23003
  1016
krauss@23003
  1017
text {*
krauss@23003
  1018
  @{thm[display] findzero.simps}
krauss@23003
  1019
krauss@23188
  1020
  \noindent Of course these would make the simplifier loop, so we better remove
krauss@23003
  1021
  them from the simpset:
krauss@23003
  1022
*}
krauss@23003
  1023
krauss@23003
  1024
declare findzero.simps[simp del]
krauss@23003
  1025
krauss@23188
  1026
text {* 
krauss@23188
  1027
  Getting rid of the domain conditions in the simplification rules is
krauss@23188
  1028
  not only useful because it simplifies proofs. It is also required in
krauss@23188
  1029
  order to use Isabelle's code generator to generate ML code
krauss@23188
  1030
  from a function definition.
krauss@23188
  1031
  Since the code generator only works with equations, it cannot be
krauss@23188
  1032
  used with @{text "psimp"} rules. Thus, in order to generate code for
krauss@23188
  1033
  partial functions, they must be defined as a tail recursion.
krauss@23188
  1034
  Luckily, many functions have a relatively natural tail recursive
krauss@23188
  1035
  definition.
krauss@23188
  1036
*}
krauss@23003
  1037
krauss@22065
  1038
section {* Nested recursion *}
krauss@22065
  1039
krauss@22065
  1040
text {*
krauss@23003
  1041
  Recursive calls which are nested in one another frequently cause
krauss@23003
  1042
  complications, since their termination proof can depend on a partial
krauss@23003
  1043
  correctness property of the function itself. 
krauss@23003
  1044
krauss@23003
  1045
  As a small example, we define the \qt{nested zero} function:
krauss@23003
  1046
*}
krauss@23003
  1047
krauss@23003
  1048
function nz :: "nat \<Rightarrow> nat"
krauss@23003
  1049
where
krauss@23003
  1050
  "nz 0 = 0"
krauss@23003
  1051
| "nz (Suc n) = nz (nz n)"
krauss@23003
  1052
by pat_completeness auto
krauss@23003
  1053
krauss@23003
  1054
text {*
krauss@23003
  1055
  If we attempt to prove termination using the identity measure on
krauss@23003
  1056
  naturals, this fails:
krauss@23003
  1057
*}
krauss@23003
  1058
krauss@23003
  1059
termination
krauss@23003
  1060
  apply (relation "measure (\<lambda>n. n)")
krauss@23003
  1061
  apply auto
krauss@23003
  1062
krauss@23003
  1063
txt {*
krauss@23003
  1064
  We get stuck with the subgoal
krauss@23003
  1065
krauss@23003
  1066
  @{subgoals[display]}
krauss@23003
  1067
krauss@23003
  1068
  Of course this statement is true, since we know that @{const nz} is
krauss@23003
  1069
  the zero function. And in fact we have no problem proving this
krauss@23003
  1070
  property by induction.
krauss@23003
  1071
*}
krauss@23805
  1072
(*<*)oops(*>*)
krauss@23003
  1073
lemma nz_is_zero: "nz_dom n \<Longrightarrow> nz n = 0"
krauss@23003
  1074
  by (induct rule:nz.pinduct) auto
krauss@23003
  1075
krauss@23003
  1076
text {*
krauss@23003
  1077
  We formulate this as a partial correctness lemma with the condition
krauss@23003
  1078
  @{term "nz_dom n"}. This allows us to prove it with the @{text
krauss@23003
  1079
  pinduct} rule before we have proved termination. With this lemma,
krauss@23003
  1080
  the termination proof works as expected:
krauss@23003
  1081
*}
krauss@23003
  1082
krauss@23003
  1083
termination
krauss@23003
  1084
  by (relation "measure (\<lambda>n. n)") (auto simp: nz_is_zero)
krauss@23003
  1085
krauss@23003
  1086
text {*
krauss@23003
  1087
  As a general strategy, one should prove the statements needed for
krauss@23003
  1088
  termination as a partial property first. Then they can be used to do
krauss@23003
  1089
  the termination proof. This also works for less trivial
krauss@23188
  1090
  examples. Figure \ref{f91} defines the 91-function, a well-known
krauss@23188
  1091
  challenge problem due to John McCarthy, and proves its termination.
krauss@23003
  1092
*}
krauss@23003
  1093
krauss@23003
  1094
text_raw {*
krauss@23003
  1095
\begin{figure}
krauss@23188
  1096
\hrule\vspace{6pt}
krauss@23003
  1097
\begin{minipage}{0.8\textwidth}
krauss@23003
  1098
\isabellestyle{it}
krauss@23003
  1099
\isastyle\isamarkuptrue
krauss@23003
  1100
*}
krauss@23003
  1101
krauss@23188
  1102
function f91 :: "nat \<Rightarrow> nat"
krauss@23003
  1103
where
krauss@23003
  1104
  "f91 n = (if 100 < n then n - 10 else f91 (f91 (n + 11)))"
krauss@23003
  1105
by pat_completeness auto
krauss@23003
  1106
krauss@23003
  1107
lemma f91_estimate: 
krauss@23003
  1108
  assumes trm: "f91_dom n" 
krauss@23003
  1109
  shows "n < f91 n + 11"
krauss@23003
  1110
using trm by induct auto
krauss@23003
  1111
krauss@23003
  1112
termination
krauss@23003
  1113
proof
krauss@23003
  1114
  let ?R = "measure (\<lambda>x. 101 - x)"
krauss@23003
  1115
  show "wf ?R" ..
krauss@23003
  1116
krauss@23003
  1117
  fix n :: nat assume "\<not> 100 < n" -- "Assumptions for both calls"
krauss@23003
  1118
krauss@23003
  1119
  thus "(n + 11, n) \<in> ?R" by simp -- "Inner call"
krauss@23003
  1120
krauss@23003
  1121
  assume inner_trm: "f91_dom (n + 11)" -- "Outer call"
krauss@23003
  1122
  with f91_estimate have "n + 11 < f91 (n + 11) + 11" .
krauss@23805
  1123
  with `\<not> 100 < n` show "(f91 (n + 11), n) \<in> ?R" by simp
krauss@23003
  1124
qed
krauss@23003
  1125
krauss@23003
  1126
text_raw {*
krauss@23003
  1127
\isamarkupfalse\isabellestyle{tt}
krauss@23188
  1128
\end{minipage}
krauss@23188
  1129
\vspace{6pt}\hrule
krauss@23003
  1130
\caption{McCarthy's 91-function}\label{f91}
krauss@23003
  1131
\end{figure}
krauss@23003
  1132
*}
krauss@23003
  1133
krauss@23003
  1134
krauss@23003
  1135
section {* Higher-Order Recursion *}
krauss@23003
  1136
krauss@23003
  1137
text {*
krauss@23003
  1138
  Higher-order recursion occurs when recursive calls
krauss@29718
  1139
  are passed as arguments to higher-order combinators such as @{const
krauss@23003
  1140
  map}, @{term filter} etc.
krauss@23805
  1141
  As an example, imagine a datatype of n-ary trees:
krauss@23003
  1142
*}
krauss@23003
  1143
krauss@23003
  1144
datatype 'a tree = 
krauss@23003
  1145
  Leaf 'a 
krauss@23003
  1146
| Branch "'a tree list"
krauss@23003
  1147
krauss@23003
  1148
krauss@25278
  1149
text {* \noindent We can define a function which swaps the left and right subtrees recursively, using the 
krauss@25278
  1150
  list functions @{const rev} and @{const map}: *}
krauss@25688
  1151
krauss@27026
  1152
fun mirror :: "'a tree \<Rightarrow> 'a tree"
krauss@23003
  1153
where
krauss@25278
  1154
  "mirror (Leaf n) = Leaf n"
krauss@25278
  1155
| "mirror (Branch l) = Branch (rev (map mirror l))"
krauss@23003
  1156
krauss@23003
  1157
text {*
krauss@27026
  1158
  Although the definition is accepted without problems, let us look at the termination proof:
krauss@23003
  1159
*}
krauss@23003
  1160
krauss@23003
  1161
termination proof
krauss@23003
  1162
  txt {*
krauss@23003
  1163
krauss@23003
  1164
  As usual, we have to give a wellfounded relation, such that the
krauss@23003
  1165
  arguments of the recursive calls get smaller. But what exactly are
krauss@27026
  1166
  the arguments of the recursive calls when mirror is given as an
krauss@29718
  1167
  argument to @{const map}? Isabelle gives us the
krauss@23003
  1168
  subgoals
krauss@23003
  1169
krauss@23003
  1170
  @{subgoals[display,indent=0]} 
krauss@23003
  1171
krauss@27026
  1172
  So the system seems to know that @{const map} only
krauss@25278
  1173
  applies the recursive call @{term "mirror"} to elements
krauss@27026
  1174
  of @{term "l"}, which is essential for the termination proof.
krauss@23003
  1175
krauss@29718
  1176
  This knowledge about @{const map} is encoded in so-called congruence rules,
krauss@23003
  1177
  which are special theorems known to the \cmd{function} command. The
krauss@29718
  1178
  rule for @{const map} is
krauss@22065
  1179
krauss@23003
  1180
  @{thm[display] map_cong}
krauss@22065
  1181
krauss@23003
  1182
  You can read this in the following way: Two applications of @{const
krauss@23003
  1183
  map} are equal, if the list arguments are equal and the functions
krauss@23003
  1184
  coincide on the elements of the list. This means that for the value 
krauss@23003
  1185
  @{term "map f l"} we only have to know how @{term f} behaves on
krauss@27026
  1186
  the elements of @{term l}.
krauss@23003
  1187
krauss@23003
  1188
  Usually, one such congruence rule is
krauss@23003
  1189
  needed for each higher-order construct that is used when defining
krauss@23003
  1190
  new functions. In fact, even basic functions like @{const
krauss@23805
  1191
  If} and @{const Let} are handled by this mechanism. The congruence
krauss@23003
  1192
  rule for @{const If} states that the @{text then} branch is only
krauss@23003
  1193
  relevant if the condition is true, and the @{text else} branch only if it
krauss@23003
  1194
  is false:
krauss@23003
  1195
krauss@23003
  1196
  @{thm[display] if_cong}
krauss@23003
  1197
  
krauss@23003
  1198
  Congruence rules can be added to the
krauss@23003
  1199
  function package by giving them the @{term fundef_cong} attribute.
krauss@23003
  1200
krauss@23805
  1201
  The constructs that are predefined in Isabelle, usually
krauss@23805
  1202
  come with the respective congruence rules.
krauss@27026
  1203
  But if you define your own higher-order functions, you may have to
krauss@27026
  1204
  state and prove the required congruence rules yourself, if you want to use your
krauss@23805
  1205
  functions in recursive definitions. 
krauss@23805
  1206
*}
krauss@27026
  1207
(*<*)oops(*>*)
krauss@23805
  1208
krauss@23805
  1209
subsection {* Congruence Rules and Evaluation Order *}
krauss@23805
  1210
krauss@23805
  1211
text {* 
krauss@23805
  1212
  Higher order logic differs from functional programming languages in
krauss@23805
  1213
  that it has no built-in notion of evaluation order. A program is
krauss@23805
  1214
  just a set of equations, and it is not specified how they must be
krauss@23805
  1215
  evaluated. 
krauss@23805
  1216
krauss@23805
  1217
  However for the purpose of function definition, we must talk about
krauss@23805
  1218
  evaluation order implicitly, when we reason about termination.
krauss@23805
  1219
  Congruence rules express that a certain evaluation order is
krauss@23805
  1220
  consistent with the logical definition. 
krauss@23805
  1221
krauss@23805
  1222
  Consider the following function.
krauss@23805
  1223
*}
krauss@23805
  1224
krauss@23805
  1225
function f :: "nat \<Rightarrow> bool"
krauss@23805
  1226
where
krauss@23805
  1227
  "f n = (n = 0 \<or> f (n - 1))"
krauss@23805
  1228
(*<*)by pat_completeness auto(*>*)
krauss@23805
  1229
krauss@23805
  1230
text {*
krauss@27026
  1231
  For this definition, the termination proof fails. The default configuration
krauss@23805
  1232
  specifies no congruence rule for disjunction. We have to add a
krauss@23805
  1233
  congruence rule that specifies left-to-right evaluation order:
krauss@23805
  1234
krauss@23805
  1235
  \vspace{1ex}
krauss@23805
  1236
  \noindent @{thm disj_cong}\hfill(@{text "disj_cong"})
krauss@23805
  1237
  \vspace{1ex}
krauss@23805
  1238
krauss@23805
  1239
  Now the definition works without problems. Note how the termination
krauss@23805
  1240
  proof depends on the extra condition that we get from the congruence
krauss@23805
  1241
  rule.
krauss@23805
  1242
krauss@23805
  1243
  However, as evaluation is not a hard-wired concept, we
krauss@23805
  1244
  could just turn everything around by declaring a different
krauss@23805
  1245
  congruence rule. Then we can make the reverse definition:
krauss@23805
  1246
*}
krauss@23805
  1247
krauss@23805
  1248
lemma disj_cong2[fundef_cong]: 
krauss@23805
  1249
  "(\<not> Q' \<Longrightarrow> P = P') \<Longrightarrow> (Q = Q') \<Longrightarrow> (P \<or> Q) = (P' \<or> Q')"
krauss@23805
  1250
  by blast
krauss@23805
  1251
krauss@23805
  1252
fun f' :: "nat \<Rightarrow> bool"
krauss@23805
  1253
where
krauss@23805
  1254
  "f' n = (f' (n - 1) \<or> n = 0)"
krauss@23805
  1255
krauss@23805
  1256
text {*
krauss@23805
  1257
  \noindent These examples show that, in general, there is no \qt{best} set of
krauss@23805
  1258
  congruence rules.
krauss@23805
  1259
krauss@23805
  1260
  However, such tweaking should rarely be necessary in
krauss@23805
  1261
  practice, as most of the time, the default set of congruence rules
krauss@23805
  1262
  works well.
krauss@23805
  1263
*}
krauss@23805
  1264
krauss@23003
  1265
end