src/Doc/ProgProve/Types_and_funs.thy
author nipkow
Sat, 16 Nov 2013 07:45:53 +0100
changeset 54436 0e1c576bbc19
parent 54195 1e685123926d
child 54467 663a927fdc88
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     1
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     2
theory Types_and_funs
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     3
imports Main
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     4
begin
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     5
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     6
text{*
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     7
\vspace{-5ex}
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
     8
\section{Type and Function Definitions}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
     9
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    10
Type synonyms are abbreviations for existing types, for example
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    11
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    12
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    13
type_synonym string = "char list"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    14
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    15
text{*
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    16
Type synonyms are expanded after parsing and are not present in internal representation and output. They are mere conveniences for the reader.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    17
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    18
\subsection{Datatypes}
51465
c5c466706549 add label for referencing in semantics book
kleing
parents: 51393
diff changeset
    19
\label{sec:datatypes}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    20
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    21
The general form of a datatype definition looks like this:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    22
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    23
\begin{tabular}{@ {}rclcll}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
    24
\isacom{datatype} @{text "('a\<^sub>1,\<dots>,'a\<^sub>n)t"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    25
     & = & $C_1 \ @{text"\""}\tau_{1,1}@{text"\""} \dots @{text"\""}\tau_{1,n_1}@{text"\""}$ \\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    26
     & $|$ & \dots \\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    27
     & $|$ & $C_k \ @{text"\""}\tau_{k,1}@{text"\""} \dots @{text"\""}\tau_{k,n_k}@{text"\""}$
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    28
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    29
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    30
It introduces the constructors \
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
    31
$C_i :: \tau_{i,1}\Rightarrow \cdots \Rightarrow \tau_{i,n_i} \Rightarrow$~@{text "('a\<^sub>1,\<dots>,'a\<^sub>n)t"} \ and expresses that any value of this type is built from these constructors in a unique manner. Uniqueness is implied by the following
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    32
properties of the constructors:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    33
\begin{itemize}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    34
\item \emph{Distinctness:} $C_i\ \ldots \neq C_j\ \dots$ \quad if $i \neq j$
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    35
\item \emph{Injectivity:}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    36
\begin{tabular}[t]{l}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    37
 $(C_i \ x_1 \dots x_{n_i} = C_i \ y_1 \dots y_{n_i}) =$\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    38
 $(x_1 = y_1 \land \dots \land x_{n_i} = y_{n_i})$
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    39
\end{tabular}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    40
\end{itemize}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    41
The fact that any value of the datatype is built from the constructors implies
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    42
the structural induction rule: to show
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
    43
$P~x$ for all $x$ of type @{text "('a\<^sub>1,\<dots>,'a\<^sub>n)t"},
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    44
one needs to show $P(C_i\ x_1 \dots x_{n_i})$ (for each $i$) assuming
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
    45
$P(x_j)$ for all $j$ where $\tau_{i,j} =$~@{text "('a\<^sub>1,\<dots>,'a\<^sub>n)t"}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    46
Distinctness and injectivity are applied automatically by @{text auto}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    47
and other proof methods. Induction must be applied explicitly.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    48
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    49
Datatype values can be taken apart with case-expressions, for example
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    50
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    51
\noquotes{@{term[source] "(case xs of [] \<Rightarrow> 0 | x # _ \<Rightarrow> Suc x)"}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    52
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    53
just like in functional programming languages. Case expressions
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    54
must be enclosed in parentheses.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    55
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    56
As an example, consider binary trees:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    57
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    58
47306
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
    59
datatype 'a tree = Tip | Node  "'a tree"  'a  "'a tree"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    60
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    61
text{* with a mirror function: *}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    62
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    63
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    64
"mirror Tip = Tip" |
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    65
"mirror (Node l a r) = Node (mirror r) a (mirror l)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    66
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    67
text{* The following lemma illustrates induction: *}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    68
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    69
lemma "mirror(mirror t) = t"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    70
apply(induction t)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    71
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    72
txt{* yields
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    73
@{subgoals[display]}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    74
The induction step contains two induction hypotheses, one for each subtree.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    75
An application of @{text auto} finishes the proof.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    76
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    77
A very simple but also very useful datatype is the predefined
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    78
@{datatype[display] option}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    79
Its sole purpose is to add a new element @{const None} to an existing
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    80
type @{typ 'a}. To make sure that @{const None} is distinct from all the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    81
elements of @{typ 'a}, you wrap them up in @{const Some} and call
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    82
the new type @{typ"'a option"}. A typical application is a lookup function
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    83
on a list of key-value pairs, often called an association list:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    84
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    85
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    86
apply auto
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    87
done
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    88
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    89
fun lookup :: "('a * 'b) list \<Rightarrow> 'a \<Rightarrow> 'b option" where
47306
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
    90
"lookup [] x = None" |
56d72c923281 made sure that " is shown in tutorial text
nipkow
parents: 47269
diff changeset
    91
"lookup ((a,b) # ps) x = (if a = x then Some b else lookup ps x)"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    92
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    93
text{*
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
    94
Note that @{text"\<tau>\<^sub>1 * \<tau>\<^sub>2"} is the type of pairs, also written @{text"\<tau>\<^sub>1 \<times> \<tau>\<^sub>2"}.
51393
df0f306f030f added pairs
nipkow
parents: 48985
diff changeset
    95
Pairs can be taken apart either by pattern matching (as above) or with the
df0f306f030f added pairs
nipkow
parents: 48985
diff changeset
    96
projection functions @{const fst} and @{const snd}: @{thm fst_conv} and @{thm snd_conv}. Tuples are simulated by pairs nested to the right: @{term"(a,b,c)"}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
    97
abbreviates @{text"(a, (b, c))"} and @{text "\<tau>\<^sub>1 \<times> \<tau>\<^sub>2 \<times> \<tau>\<^sub>3"} abbreviates
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
    98
@{text "\<tau>\<^sub>1 \<times> (\<tau>\<^sub>2 \<times> \<tau>\<^sub>3)"}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
    99
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   100
\subsection{Definitions}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   101
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   102
Non recursive functions can be defined as in the following example:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   103
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   104
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   105
definition sq :: "nat \<Rightarrow> nat" where
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   106
"sq n = n * n"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   107
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   108
text{* Such definitions do not allow pattern matching but only
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   109
@{text"f x\<^sub>1 \<dots> x\<^sub>n = t"}, where @{text f} does not occur in @{text t}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   110
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   111
\subsection{Abbreviations}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   112
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   113
Abbreviations are similar to definitions:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   114
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   115
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   116
abbreviation sq' :: "nat \<Rightarrow> nat" where
52045
nipkow
parents: 51465
diff changeset
   117
"sq' n \<equiv> n * n"
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   118
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   119
text{* The key difference is that @{const sq'} is only syntactic sugar:
52045
nipkow
parents: 51465
diff changeset
   120
after parsing, @{term"sq' t"} is replaced by \mbox{@{term"t*t"}}, and
nipkow
parents: 51465
diff changeset
   121
before printing, every occurrence of @{term"u*u"} is replaced by
nipkow
parents: 51465
diff changeset
   122
\mbox{@{term"sq' u"}}.  Internally, @{const sq'} does not exist.
nipkow
parents: 51465
diff changeset
   123
This is the
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   124
advantage of abbreviations over definitions: definitions need to be expanded
52045
nipkow
parents: 51465
diff changeset
   125
explicitly (\autoref{sec:rewr-defs}) whereas abbreviations are already
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   126
expanded upon parsing. However, abbreviations should be introduced sparingly:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   127
if abused, they can lead to a confusing discrepancy between the internal and
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   128
external view of a term.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   129
52045
nipkow
parents: 51465
diff changeset
   130
The ASCII representation of @{text"\<equiv>"} is \texttt{==} or \xsymbol{equiv}.
nipkow
parents: 51465
diff changeset
   131
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
   132
\subsection{Recursive Functions}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   133
\label{sec:recursive-funs}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   134
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   135
Recursive functions are defined with \isacom{fun} by pattern matching
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   136
over datatype constructors. The order of equations matters. Just as in
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   137
functional programming languages. However, all HOL functions must be
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   138
total. This simplifies the logic---terms are always defined---but means
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   139
that recursive functions must terminate. Otherwise one could define a
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   140
function @{prop"f n = f n + (1::nat)"} and conclude \mbox{@{prop"(0::nat) = 1"}} by
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   141
subtracting @{term"f n"} on both sides.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   142
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   143
Isabelle's automatic termination checker requires that the arguments of
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   144
recursive calls on the right-hand side must be strictly smaller than the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   145
arguments on the left-hand side. In the simplest case, this means that one
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   146
fixed argument position decreases in size with each recursive call. The size
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   147
is measured as the number of constructors (excluding 0-ary ones, e.g.\ @{text
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   148
Nil}). Lexicographic combinations are also recognized. In more complicated
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   149
situations, the user may have to prove termination by hand. For details
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   150
see~\cite{Krauss}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   151
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   152
Functions defined with \isacom{fun} come with their own induction schema
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   153
that mirrors the recursion schema and is derived from the termination
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   154
order. For example,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   155
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   156
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   157
fun div2 :: "nat \<Rightarrow> nat" where
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   158
"div2 0 = 0" |
54136
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   159
"div2 (Suc 0) = 0" |
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   160
"div2 (Suc(Suc n)) = Suc(div2 n)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   161
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   162
text{* does not just define @{const div2} but also proves a
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   163
customized induction rule:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   164
\[
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   165
\inferrule{
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   166
\mbox{@{thm (prem 1) div2.induct}}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   167
\mbox{@{thm (prem 2) div2.induct}}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   168
\mbox{@{thm (prem 3) div2.induct}}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   169
{\mbox{@{thm (concl) div2.induct[of _ "m"]}}}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   170
\]
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   171
This customized induction rule can simplify inductive proofs. For example,
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   172
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   173
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   174
lemma "div2(n+n) = n"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   175
apply(induction n rule: div2.induct)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   176
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   177
txt{* yields the 3 subgoals
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   178
@{subgoals[display,margin=65]}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   179
An application of @{text auto} finishes the proof.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   180
Had we used ordinary structural induction on @{text n},
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   181
the proof would have needed an additional
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   182
case analysis in the induction step.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   183
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   184
The general case is often called \concept{computation induction},
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   185
because the induction follows the (terminating!) computation.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   186
For every defining equation
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   187
\begin{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   188
@{text "f(e) = \<dots> f(r\<^sub>1) \<dots> f(r\<^sub>k) \<dots>"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   189
\end{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   190
where @{text"f(r\<^sub>i)"}, @{text"i=1\<dots>k"}, are all the recursive calls,
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   191
the induction rule @{text"f.induct"} contains one premise of the form
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   192
\begin{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   193
@{text"P(r\<^sub>1) \<Longrightarrow> \<dots> \<Longrightarrow> P(r\<^sub>k) \<Longrightarrow> P(e)"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   194
\end{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   195
If @{text "f :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<tau>\<^sub>n \<Rightarrow> \<tau>"} then @{text"f.induct"} is applied like this:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   196
\begin{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   197
\isacom{apply}@{text"(induction x\<^sub>1 \<dots> x\<^sub>n rule: f.induct)"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   198
\end{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   199
where typically there is a call @{text"f x\<^sub>1 \<dots> x\<^sub>n"} in the goal.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   200
But note that the induction rule does not mention @{text f} at all,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   201
except in its name, and is applicable independently of @{text f}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   202
54136
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   203
54436
nipkow
parents: 54195
diff changeset
   204
\subsection*{Exercises}
54136
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   205
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   206
\begin{exercise}
54194
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   207
Starting from the type @{text "'a tree"} defined in the text, define
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   208
a function @{text "contents ::"} @{typ "'a tree \<Rightarrow> 'a list"}
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   209
that collects all values in a tree in a list, in any order,
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   210
without removing duplicates.
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   211
Then define a function @{text "treesum ::"} @{typ "nat tree \<Rightarrow> nat"}
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   212
that sums up all values in a tree of natural numbers
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   213
and prove @{prop "treesum t = listsum(contents t)"}.
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   214
\end{exercise}
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   215
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   216
\begin{exercise}
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   217
Define a new type @{text "'a tree2"} of binary trees where values are also
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   218
stored in the leaves of the tree.  Also reformulate the
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   219
@{const mirror} function accordingly. Define two functions
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   220
@{text "pre_order"} and @{text "post_order"} of type @{text "'a tree2 \<Rightarrow> 'a list"}
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   221
that traverse a tree and collect all stored values in the respective order in
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   222
a list. Prove @{prop "pre_order (mirror t) = rev (post_order t)"}.
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   223
\end{exercise}
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   224
12c9ad3142be added exercises
nipkow
parents: 54136
diff changeset
   225
\begin{exercise}
54136
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   226
Prove that @{const div2} defined above divides every number by @{text 2},
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   227
not just those of the form @{text"n+n"}: @{prop "div2 n = n div 2"}.
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   228
\end{exercise}
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   229
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   230
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
   231
\section{Induction Heuristics}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   232
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   233
We have already noted that theorems about recursive functions are proved by
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   234
induction. In case the function has more than one argument, we have followed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   235
the following heuristic in the proofs about the append function:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   236
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   237
\emph{Perform induction on argument number $i$\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   238
 if the function is defined by recursion on argument number $i$.}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   239
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   240
The key heuristic, and the main point of this section, is to
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   241
\emph{generalize the goal before induction}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   242
The reason is simple: if the goal is
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   243
too specific, the induction hypothesis is too weak to allow the induction
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   244
step to go through. Let us illustrate the idea with an example.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   245
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   246
Function @{const rev} has quadratic worst-case running time
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   247
because it calls append for each element of the list and
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   248
append is linear in its first argument.  A linear time version of
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   249
@{const rev} requires an extra argument where the result is accumulated
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   250
gradually, using only~@{text"#"}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   251
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   252
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   253
apply auto
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   254
done
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   255
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   256
fun itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   257
"itrev []        ys = ys" |
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   258
"itrev (x#xs) ys = itrev xs (x#ys)"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   259
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   260
text{* The behaviour of @{const itrev} is simple: it reverses
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   261
its first argument by stacking its elements onto the second argument,
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   262
and it returns that second argument when the first one becomes
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   263
empty. Note that @{const itrev} is tail-recursive: it can be
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   264
compiled into a loop, no stack is necessary for executing it.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   265
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   266
Naturally, we would like to show that @{const itrev} does indeed reverse
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   267
its first argument provided the second one is empty:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   268
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   270
lemma "itrev xs [] = rev xs"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   271
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   272
txt{* There is no choice as to the induction variable:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   273
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   274
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   275
apply(induction xs)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   276
apply(auto)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   277
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   278
txt{*
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   279
Unfortunately, this attempt does not prove
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   280
the induction step:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   281
@{subgoals[display,margin=70]}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   282
The induction hypothesis is too weak.  The fixed
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   283
argument,~@{term"[]"}, prevents it from rewriting the conclusion.  
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   284
This example suggests a heuristic:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   285
\begin{quote}
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   286
\emph{Generalize goals for induction by replacing constants by variables.}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   287
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   288
Of course one cannot do this na\"{\i}vely: @{prop"itrev xs ys = rev xs"} is
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   289
just not true.  The correct generalization is
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   290
*};
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   291
(*<*)oops;(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   292
lemma "itrev xs ys = rev xs @ ys"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   293
(*<*)apply(induction xs, auto)(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   294
txt{*
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   295
If @{text ys} is replaced by @{term"[]"}, the right-hand side simplifies to
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   296
@{term"rev xs"}, as required.
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   297
In this instance it was easy to guess the right generalization.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   298
Other situations can require a good deal of creativity.  
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   299
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   300
Although we now have two variables, only @{text xs} is suitable for
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   301
induction, and we repeat our proof attempt. Unfortunately, we are still
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   302
not there:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   303
@{subgoals[display,margin=65]}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   304
The induction hypothesis is still too weak, but this time it takes no
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   305
intuition to generalize: the problem is that the @{text ys}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   306
in the induction hypothesis is fixed,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   307
but the induction hypothesis needs to be applied with
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   308
@{term"a # ys"} instead of @{text ys}. Hence we prove the theorem
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   309
for all @{text ys} instead of a fixed one. We can instruct induction
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   310
to perform this generalization for us by adding @{text "arbitrary: ys"}.
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   311
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   312
(*<*)oops
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   313
lemma "itrev xs ys = rev xs @ ys"
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   314
(*>*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   315
apply(induction xs arbitrary: ys)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   316
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   317
txt{* The induction hypothesis in the induction step is now universally quantified over @{text ys}:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   318
@{subgoals[display,margin=65]}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   319
Thus the proof succeeds:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   320
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   321
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   322
apply auto
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   323
done
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   324
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   325
text{*
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   326
This leads to another heuristic for generalization:
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   327
\begin{quote}
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   328
\emph{Generalize induction by generalizing all free
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   329
variables\\ {\em(except the induction variable itself)}.}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   330
\end{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   331
Generalization is best performed with @{text"arbitrary: y\<^sub>1 \<dots> y\<^sub>k"}. 
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   332
This heuristic prevents trivial failures like the one above.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   333
However, it should not be applied blindly.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   334
It is not always required, and the additional quantifiers can complicate
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   335
matters in some cases. The variables that need to be quantified are typically
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   336
those that change in recursive calls.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   337
54136
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   338
54436
nipkow
parents: 54195
diff changeset
   339
\subsection*{Exercises}
54136
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   340
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   341
\begin{exercise}
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   342
Write a tail-recursive variant of the @{text add} function on @{typ nat}:
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   343
@{term "itadd :: nat \<Rightarrow> nat \<Rightarrow> nat"}.
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   344
Tail-recursive means that in the recursive case, @{text itadd} needs to call
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   345
itself directly: \mbox{@{term"itadd (Suc m) n"}} @{text"= itadd \<dots>"}.
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   346
Prove @{prop "itadd m n = add m n"}.
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   347
\end{exercise}
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   348
c206cb2a3afe more exercises
nipkow
parents: 53015
diff changeset
   349
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   350
\section{Simplification}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   351
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   352
So far we have talked a lot about simplifying terms without explaining the concept. \concept{Simplification} means
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   353
\begin{itemize}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   354
\item using equations $l = r$ from left to right (only),
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   355
\item as long as possible.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   356
\end{itemize}
47711
c1cca2a052e4 doc update
nipkow
parents: 47306
diff changeset
   357
To emphasize the directionality, equations that have been given the
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   358
@{text"simp"} attribute are called \concept{simplification}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   359
rules. Logically, they are still symmetric, but proofs by
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   360
simplification use them only in the left-to-right direction.  The proof tool
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   361
that performs simplifications is called the \concept{simplifier}. It is the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   362
basis of @{text auto} and other related proof methods.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   363
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   364
The idea of simplification is best explained by an example. Given the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   365
simplification rules
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   366
\[
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   367
\begin{array}{rcl@ {\quad}l}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   368
@{term"0 + n::nat"} &@{text"="}& @{text n} & (1) \\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   369
@{term"(Suc m) + n"} &@{text"="}& @{term"Suc (m + n)"} & (2) \\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   370
@{text"(Suc m \<le> Suc n)"} &@{text"="}& @{text"(m \<le> n)"} & (3)\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   371
@{text"(0 \<le> m)"} &@{text"="}& @{const True} & (4)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   372
\end{array}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   373
\]
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   374
the formula @{prop"0 + Suc 0 \<le> Suc 0 + x"} is simplified to @{const True}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   375
as follows:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   376
\[
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   377
\begin{array}{r@ {}c@ {}l@ {\quad}l}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   378
@{text"(0 + Suc 0"} & \leq & @{text"Suc 0 + x)"}  & \stackrel{(1)}{=} \\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   379
@{text"(Suc 0"}     & \leq & @{text"Suc 0 + x)"}  & \stackrel{(2)}{=} \\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   380
@{text"(Suc 0"}     & \leq & @{text"Suc (0 + x)"} & \stackrel{(3)}{=} \\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   381
@{text"(0"}         & \leq & @{text"0 + x)"}      & \stackrel{(4)}{=} \\[1ex]
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   382
 & @{const True}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   383
\end{array}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   384
\]
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   385
Simplification is often also called \concept{rewriting}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   386
and simplification rules \concept{rewrite rules}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   387
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
   388
\subsection{Simplification Rules}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   389
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   390
The attribute @{text"simp"} declares theorems to be simplification rules,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   391
which the simplifier will use automatically. In addition,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   392
\isacom{datatype} and \isacom{fun} commands implicitly declare some
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   393
simplification rules: \isacom{datatype} the distinctness and injectivity
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   394
rules, \isacom{fun} the defining equations. Definitions are not declared
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   395
as simplification rules automatically! Nearly any theorem can become a
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   396
simplification rule. The simplifier will try to transform it into an
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   397
equation. For example, the theorem @{prop"\<not> P"} is turned into @{prop"P = False"}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   398
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   399
Only equations that really simplify, like @{prop"rev (rev xs) = xs"} and
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   400
@{prop"xs @ [] = xs"}, should be declared as simplification
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   401
rules. Equations that may be counterproductive as simplification rules
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   402
should only be used in specific proof steps (see \S\ref{sec:simp} below).
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   403
Distributivity laws, for example, alter the structure of terms
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   404
and can produce an exponential blow-up.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   405
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
   406
\subsection{Conditional Simplification Rules}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   407
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   408
Simplification rules can be conditional.  Before applying such a rule, the
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   409
simplifier will first try to prove the preconditions, again by
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   410
simplification. For example, given the simplification rules
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   411
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   412
@{prop"p(0::nat) = True"}\\
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   413
@{prop"p(x) \<Longrightarrow> f(x) = g(x)"},
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   414
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   415
the term @{term"f(0::nat)"} simplifies to @{term"g(0::nat)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   416
but @{term"f(1::nat)"} does not simplify because @{prop"p(1::nat)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   417
is not provable.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   418
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   419
\subsection{Termination}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   420
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   421
Simplification can run forever, for example if both @{prop"f x = g x"} and
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   422
@{prop"g(x) = f(x)"} are simplification rules. It is the user's
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   423
responsibility not to include simplification rules that can lead to
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   424
nontermination, either on their own or in combination with other
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   425
simplification rules. The right-hand side of a simplification rule should
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   426
always be ``simpler'' than the left-hand side---in some sense. But since
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   427
termination is undecidable, such a check cannot be automated completely
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   428
and Isabelle makes little attempt to detect nontermination.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   429
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   430
When conditional simplification rules are applied, their preconditions are
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   431
proved first. Hence all preconditions need to be
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   432
simpler than the left-hand side of the conclusion. For example
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   433
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   434
@{prop "n < m \<Longrightarrow> (n < Suc m) = True"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   435
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   436
is suitable as a simplification rule: both @{prop"n<m"} and @{const True}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   437
are simpler than \mbox{@{prop"n < Suc m"}}. But
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   438
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   439
@{prop "Suc n < m \<Longrightarrow> (n < m) = True"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   440
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   441
leads to nontermination: when trying to rewrite @{prop"n<m"} to @{const True}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   442
one first has to prove \mbox{@{prop"Suc n < m"}}, which can be rewritten to @{const True} provided @{prop"Suc(Suc n) < m"}, \emph{ad infinitum}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   443
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
   444
\subsection{The @{text simp} Proof Method}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   445
\label{sec:simp}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   446
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   447
So far we have only used the proof method @{text auto}.  Method @{text simp}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   448
is the key component of @{text auto}, but @{text auto} can do much more. In
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   449
some cases, @{text auto} is overeager and modifies the proof state too much.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   450
In such cases the more predictable @{text simp} method should be used.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   451
Given a goal
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   452
\begin{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   453
@{text"1. \<lbrakk> P\<^sub>1; \<dots>; P\<^sub>m \<rbrakk> \<Longrightarrow> C"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   454
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   455
the command
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   456
\begin{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   457
\isacom{apply}@{text"(simp add: th\<^sub>1 \<dots> th\<^sub>n)"}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   458
\end{quote}
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   459
simplifies the assumptions @{text "P\<^sub>i"} and the conclusion @{text C} using
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   460
\begin{itemize}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   461
\item all simplification rules, including the ones coming from \isacom{datatype} and \isacom{fun},
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52718
diff changeset
   462
\item the additional lemmas @{text"th\<^sub>1 \<dots> th\<^sub>n"}, and
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   463
\item the assumptions.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   464
\end{itemize}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   465
In addition to or instead of @{text add} there is also @{text del} for removing
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   466
simplification rules temporarily. Both are optional. Method @{text auto}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   467
can be modified similarly:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   468
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   469
\isacom{apply}@{text"(auto simp add: \<dots> simp del: \<dots>)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   470
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   471
Here the modifiers are @{text"simp add"} and @{text"simp del"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   472
instead of just @{text add} and @{text del} because @{text auto}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   473
does not just perform simplification.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   474
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   475
Note that @{text simp} acts only on subgoal 1, @{text auto} acts on all
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   476
subgoals. There is also @{text simp_all}, which applies @{text simp} to
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   477
all subgoals.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   478
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
   479
\subsection{Rewriting With Definitions}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   480
\label{sec:rewr-defs}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   481
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   482
Definitions introduced by the command \isacom{definition}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   483
can also be used as simplification rules,
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   484
but by default they are not: the simplifier does not expand them
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   485
automatically. Definitions are intended for introducing abstract concepts and
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   486
not merely as abbreviations. Of course, we need to expand the definition
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   487
initially, but once we have proved enough abstract properties of the new
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   488
constant, we can forget its original definition. This style makes proofs more
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   489
robust: if the definition has to be changed, only the proofs of the abstract
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   490
properties will be affected.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   491
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   492
The definition of a function @{text f} is a theorem named @{text f_def}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   493
and can be added to a call of @{text simp} just like any other theorem:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   494
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   495
\isacom{apply}@{text"(simp add: f_def)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   496
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   497
In particular, let-expressions can be unfolded by
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   498
making @{thm[source] Let_def} a simplification rule.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   499
52361
7d5ad23b8245 all headings in upper case
nipkow
parents: 52045
diff changeset
   500
\subsection{Case Splitting With @{text simp}}
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   501
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   502
Goals containing if-expressions are automatically split into two cases by
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   503
@{text simp} using the rule
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   504
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   505
@{prop"P(if A then s else t) = ((A \<longrightarrow> P(s)) \<and> (~A \<longrightarrow> P(t)))"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   506
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   507
For example, @{text simp} can prove
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   508
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   509
@{prop"(A \<and> B) = (if A then B else False)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   510
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   511
because both @{prop"A \<longrightarrow> (A & B) = B"} and @{prop"~A \<longrightarrow> (A & B) = False"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   512
simplify to @{const True}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   513
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   514
We can split case-expressions similarly. For @{text nat} the rule looks like this:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   515
@{prop[display,margin=65,indent=4]"P(case e of 0 \<Rightarrow> a | Suc n \<Rightarrow> b n) = ((e = 0 \<longrightarrow> P a) & (ALL n. e = Suc n \<longrightarrow> P(b n)))"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   516
Case expressions are not split automatically by @{text simp}, but @{text simp}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   517
can be instructed to do so:
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   518
\begin{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   519
\isacom{apply}@{text"(simp split: nat.split)"}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   520
\end{quote}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   521
splits all case-expressions over natural numbers. For an arbitrary
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   522
datatype @{text t} it is @{text "t.split"} instead of @{thm[source] nat.split}.
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   523
Method @{text auto} can be modified in exactly the same way.
52593
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   524
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   525
54436
nipkow
parents: 54195
diff changeset
   526
\subsection*{Exercises}
52593
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   527
54195
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   528
\exercise\label{exe:tree0}
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   529
Define a datatype @{text tree0} of binary tree skeletons which do not store
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   530
any information, neither in the inner nodes nor in the leaves.
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   531
Define a function @{text "nodes :: tree0 \<Rightarrow> nat"} that counts the total number
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   532
all nodes (inner nodes and leaves) in such a tree.
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   533
Consider the following recursive function:
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   534
*}
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   535
(*<*)
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   536
datatype tree0 = Tip | Node tree0 tree0
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   537
(*>*)
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   538
fun explode :: "nat \<Rightarrow> tree0 \<Rightarrow> tree0" where
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   539
"explode 0 t = t" |
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   540
"explode (Suc n) t = explode n (Node t t)"
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   541
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   542
text {*
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   543
Find an equation expressing the size of a tree after exploding it
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   544
(\noquotes{@{term [source] "nodes (explode n t)"}}) as a function
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   545
of @{term "nodes t"} and @{text n}. Prove your equation.
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   546
You may use the usual arithmetic operators including the exponentiation
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   547
operator ``@{text"^"}''. For example, \noquotes{@{prop [source] "2 ^ 2 = 4"}}.
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   548
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   549
Hint: simplifying with the list of theorems @{thm[source] algebra_simps}
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   550
takes care of common algebraic properties of the arithmetic operators.
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   551
\endexercise
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   552
52593
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   553
\exercise
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   554
Define arithmetic expressions in one variable over integers (type @{typ int})
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   555
as a data type:
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   556
*}
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   557
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   558
datatype exp = Var | Const int | Add exp exp | Mult exp exp
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   559
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   560
text{*
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   561
Define a function \noquotes{@{term [source]"eval :: exp \<Rightarrow> int \<Rightarrow> int"}}
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   562
such that @{term"eval e x"} evaluates @{text e} at the value
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   563
@{text x}.
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   564
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   565
A polynomial can be represented as a list of coefficients, starting with
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   566
the constant. For example, @{term "[4, 2, -1, 3::int]"} represents the
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   567
polynomial $4 + 2x - x^2 + 3x^3$.
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   568
Define a function \noquotes{@{term [source] "evalp :: int list \<Rightarrow> int \<Rightarrow> int"}}
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   569
that evaluates a polynomial at the given value.
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   570
Define a function \noquotes{@{term[source] "coeffs :: exp \<Rightarrow> int list"}}
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   571
that transforms an expression into a polynomial. This may require auxiliary
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   572
functions. Prove that @{text coeffs} preserves the value of the expression:
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   573
\mbox{@{prop"evalp (coeffs e) x = eval e x"}.}
54195
1e685123926d more exercises
nipkow
parents: 54194
diff changeset
   574
Hint: consider the hint in \autoref{exe:tree0}.
52593
aedf7b01c6e4 added exercises
nipkow
parents: 52361
diff changeset
   575
\endexercise
47269
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   576
*}
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   577
(*<*)
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   578
end
29aa0c071875 New manual Programming and Proving in Isabelle/HOL
nipkow
parents:
diff changeset
   579
(*>*)