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