47269
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{Types{\isaliteral{5F}{\isacharunderscore}}and{\isaliteral{5F}{\isacharunderscore}}funs}%
|
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
%
|
|
7 |
\endisadelimtheory
|
|
8 |
%
|
|
9 |
\isatagtheory
|
|
10 |
%
|
|
11 |
\endisatagtheory
|
|
12 |
{\isafoldtheory}%
|
|
13 |
%
|
|
14 |
\isadelimtheory
|
|
15 |
%
|
|
16 |
\endisadelimtheory
|
|
17 |
%
|
|
18 |
\begin{isamarkuptext}%
|
|
19 |
\vspace{-5ex}
|
|
20 |
\section{Type and function definitions}
|
|
21 |
|
|
22 |
Type synonyms are abbreviations for existing types, for example%
|
|
23 |
\end{isamarkuptext}%
|
|
24 |
\isamarkuptrue%
|
|
25 |
\isacommand{type{\isaliteral{5F}{\isacharunderscore}}synonym}\isamarkupfalse%
|
|
26 |
\ string\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{22}{\isachardoublequoteopen}}char\ list{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
27 |
\begin{isamarkuptext}%
|
|
28 |
Type synonyms are expanded after parsing and are not present in internal representation and output. They are mere conveniences for the reader.
|
|
29 |
|
|
30 |
\subsection{Datatypes}
|
|
31 |
|
|
32 |
The general form of a datatype definition looks like this:
|
|
33 |
\begin{quote}
|
|
34 |
\begin{tabular}{@ {}rclcll}
|
|
35 |
\isacom{datatype} \isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{5C3C646F74733E}{\isasymdots}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub n{\isaliteral{29}{\isacharparenright}}t}
|
|
36 |
& = & $C_1 \ \isa{{\isaliteral{22}{\isachardoublequote}}}\tau_{1,1}\isa{{\isaliteral{22}{\isachardoublequote}}} \dots \isa{{\isaliteral{22}{\isachardoublequote}}}\tau_{1,n_1}\isa{{\isaliteral{22}{\isachardoublequote}}}$ \\
|
|
37 |
& $|$ & \dots \\
|
|
38 |
& $|$ & $C_k \ \isa{{\isaliteral{22}{\isachardoublequote}}}\tau_{k,1}\isa{{\isaliteral{22}{\isachardoublequote}}} \dots \isa{{\isaliteral{22}{\isachardoublequote}}}\tau_{k,n_k}\isa{{\isaliteral{22}{\isachardoublequote}}}$
|
|
39 |
\end{tabular}
|
|
40 |
\end{quote}
|
|
41 |
It introduces the constructors \
|
|
42 |
$C_i :: \tau_{i,1}\Rightarrow \cdots \Rightarrow \tau_{i,n_i} \Rightarrow$~\isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{5C3C646F74733E}{\isasymdots}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub n{\isaliteral{29}{\isacharparenright}}t} \ and expresses that any value of this type is built from these constructors in a unique manner. Uniqueness is implied by the following
|
|
43 |
properties of the constructors:
|
|
44 |
\begin{itemize}
|
|
45 |
\item \emph{Distinctness:} $C_i\ \ldots \neq C_j\ \dots$ \quad if $i \neq j$
|
|
46 |
\item \emph{Injectivity:}
|
|
47 |
\begin{tabular}[t]{l}
|
|
48 |
$(C_i \ x_1 \dots x_{n_i} = C_i \ y_1 \dots y_{n_i}) =$\\
|
|
49 |
$(x_1 = y_1 \land \dots \land x_{n_i} = y_{n_i})$
|
|
50 |
\end{tabular}
|
|
51 |
\end{itemize}
|
|
52 |
The fact that any value of the datatype is built from the constructors implies
|
|
53 |
the structural induction rule: to show
|
|
54 |
$P~x$ for all $x$ of type \isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{5C3C646F74733E}{\isasymdots}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub n{\isaliteral{29}{\isacharparenright}}t},
|
|
55 |
one needs to show $P(C_i\ x_1 \dots x_{n_i})$ (for each $i$) assuming
|
|
56 |
$P(x_j)$ for all $j$ where $\tau_{i,j} =$~\isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{5C3C646F74733E}{\isasymdots}}{\isaliteral{2C}{\isacharcomma}}{\isaliteral{27}{\isacharprime}}a\isaliteral{5C3C5E697375623E}{}\isactrlisub n{\isaliteral{29}{\isacharparenright}}t}.
|
|
57 |
Distinctness and injectivity are applied automatically by \isa{auto}
|
|
58 |
and other proof methods. Induction must be applied explicitly.
|
|
59 |
|
|
60 |
Datatype values can be taken apart with case-expressions, for example
|
|
61 |
\begin{quote}
|
|
62 |
\noquotes{\isa{{\isaliteral{22}{\isachardoublequote}}{\isaliteral{28}{\isacharparenleft}}case\ xs\ of\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isadigit{0}}\ {\isaliteral{7C}{\isacharbar}}\ x\ {\isaliteral{23}{\isacharhash}}\ {\isaliteral{5F}{\isacharunderscore}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ Suc\ x{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequote}}}}
|
|
63 |
\end{quote}
|
|
64 |
just like in functional programming languages. Case expressions
|
|
65 |
must be enclosed in parentheses.
|
|
66 |
|
|
67 |
As an example, consider binary trees:%
|
|
68 |
\end{isamarkuptext}%
|
|
69 |
\isamarkuptrue%
|
|
70 |
\isacommand{datatype}\isamarkupfalse%
|
47306
|
71 |
\ {\isaliteral{27}{\isacharprime}}a\ tree\ {\isaliteral{3D}{\isacharequal}}\ Tip\ {\isaliteral{7C}{\isacharbar}}\ Node\ \ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ tree{\isaliteral{22}{\isachardoublequoteclose}}\ \ {\isaliteral{27}{\isacharprime}}a\ \ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ tree{\isaliteral{22}{\isachardoublequoteclose}}%
|
47269
|
72 |
\begin{isamarkuptext}%
|
|
73 |
with a mirror function:%
|
|
74 |
\end{isamarkuptext}%
|
|
75 |
\isamarkuptrue%
|
|
76 |
\isacommand{fun}\isamarkupfalse%
|
|
77 |
\ mirror\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ tree\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ tree{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline
|
|
78 |
{\isaliteral{22}{\isachardoublequoteopen}}mirror\ Tip\ {\isaliteral{3D}{\isacharequal}}\ Tip{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline
|
|
79 |
{\isaliteral{22}{\isachardoublequoteopen}}mirror\ {\isaliteral{28}{\isacharparenleft}}Node\ l\ a\ r{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Node\ {\isaliteral{28}{\isacharparenleft}}mirror\ r{\isaliteral{29}{\isacharparenright}}\ a\ {\isaliteral{28}{\isacharparenleft}}mirror\ l{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
80 |
\begin{isamarkuptext}%
|
|
81 |
The following lemma illustrates induction:%
|
|
82 |
\end{isamarkuptext}%
|
|
83 |
\isamarkuptrue%
|
|
84 |
\isacommand{lemma}\isamarkupfalse%
|
|
85 |
\ {\isaliteral{22}{\isachardoublequoteopen}}mirror{\isaliteral{28}{\isacharparenleft}}mirror\ t{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ t{\isaliteral{22}{\isachardoublequoteclose}}\isanewline
|
|
86 |
%
|
|
87 |
\isadelimproof
|
|
88 |
%
|
|
89 |
\endisadelimproof
|
|
90 |
%
|
|
91 |
\isatagproof
|
|
92 |
\isacommand{apply}\isamarkupfalse%
|
|
93 |
{\isaliteral{28}{\isacharparenleft}}induction\ t{\isaliteral{29}{\isacharparenright}}%
|
|
94 |
\begin{isamarkuptxt}%
|
|
95 |
yields
|
|
96 |
\begin{isabelle}%
|
|
97 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ mirror\ {\isaliteral{28}{\isacharparenleft}}mirror\ Tip{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Tip\isanewline
|
|
98 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}t{\isadigit{1}}\ a\ t{\isadigit{2}}{\isaliteral{2E}{\isachardot}}\isanewline
|
|
99 |
\isaindent{\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}mirror\ {\isaliteral{28}{\isacharparenleft}}mirror\ t{\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ t{\isadigit{1}}{\isaliteral{3B}{\isacharsemicolon}}\ mirror\ {\isaliteral{28}{\isacharparenleft}}mirror\ t{\isadigit{2}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ t{\isadigit{2}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\isanewline
|
|
100 |
\isaindent{\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ mirror\ {\isaliteral{28}{\isacharparenleft}}mirror\ {\isaliteral{28}{\isacharparenleft}}Node\ t{\isadigit{1}}\ a\ t{\isadigit{2}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Node\ t{\isadigit{1}}\ a\ t{\isadigit{2}}%
|
|
101 |
\end{isabelle}
|
|
102 |
The induction step contains two induction hypotheses, one for each subtree.
|
|
103 |
An application of \isa{auto} finishes the proof.
|
|
104 |
|
|
105 |
A very simple but also very useful datatype is the predefined
|
|
106 |
\begin{isabelle}%
|
|
107 |
\isacommand{datatype}\ {\isaliteral{27}{\isacharprime}}a\ option\ {\isaliteral{3D}{\isacharequal}}\ None\ {\isaliteral{7C}{\isacharbar}}\ Some\ {\isaliteral{27}{\isacharprime}}a%
|
|
108 |
\end{isabelle}
|
|
109 |
Its sole purpose is to add a new element \isa{None} to an existing
|
|
110 |
type \isa{{\isaliteral{27}{\isacharprime}}a}. To make sure that \isa{None} is distinct from all the
|
|
111 |
elements of \isa{{\isaliteral{27}{\isacharprime}}a}, you wrap them up in \isa{Some} and call
|
|
112 |
the new type \isa{{\isaliteral{27}{\isacharprime}}a\ option}. A typical application is a lookup function
|
|
113 |
on a list of key-value pairs, often called an association list:%
|
|
114 |
\end{isamarkuptxt}%
|
|
115 |
\isamarkuptrue%
|
|
116 |
%
|
|
117 |
\endisatagproof
|
|
118 |
{\isafoldproof}%
|
|
119 |
%
|
|
120 |
\isadelimproof
|
|
121 |
%
|
|
122 |
\endisadelimproof
|
|
123 |
\isacommand{fun}\isamarkupfalse%
|
|
124 |
\ lookup\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\ {\isaliteral{2A}{\isacharasterisk}}\ {\isaliteral{27}{\isacharprime}}b{\isaliteral{29}{\isacharparenright}}\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}b\ option{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline
|
47306
|
125 |
{\isaliteral{22}{\isachardoublequoteopen}}lookup\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ x\ {\isaliteral{3D}{\isacharequal}}\ None{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline
|
|
126 |
{\isaliteral{22}{\isachardoublequoteopen}}lookup\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{28}{\isacharparenleft}}a{\isaliteral{2C}{\isacharcomma}}b{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{23}{\isacharhash}}\ ps{\isaliteral{29}{\isacharparenright}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ a\ {\isaliteral{3D}{\isacharequal}}\ x\ then\ Some\ b\ else\ lookup\ ps\ x{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}%
|
47269
|
127 |
\begin{isamarkuptext}%
|
|
128 |
Note that \isa{{\isaliteral{5C3C7461753E}{\isasymtau}}\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{2A}{\isacharasterisk}}\ {\isaliteral{5C3C7461753E}{\isasymtau}}\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{2}}} is the type of pairs, also written \isa{{\isaliteral{5C3C7461753E}{\isasymtau}}\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C74696D65733E}{\isasymtimes}}\ {\isaliteral{5C3C7461753E}{\isasymtau}}\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{2}}}.
|
|
129 |
|
|
130 |
\subsection{Definitions}
|
|
131 |
|
|
132 |
Non recursive functions can be defined as in the following example:%
|
|
133 |
\end{isamarkuptext}%
|
|
134 |
\isamarkuptrue%
|
|
135 |
\isacommand{definition}\isamarkupfalse%
|
|
136 |
\ sq\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline
|
|
137 |
{\isaliteral{22}{\isachardoublequoteopen}}sq\ n\ {\isaliteral{3D}{\isacharequal}}\ n\ {\isaliteral{2A}{\isacharasterisk}}\ n{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
138 |
\begin{isamarkuptext}%
|
|
139 |
Such definitions do not allow pattern matching but only
|
|
140 |
\isa{f\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub n\ {\isaliteral{3D}{\isacharequal}}\ t}, where \isa{f} does not occur in \isa{t}.
|
|
141 |
|
|
142 |
\subsection{Abbreviations}
|
|
143 |
|
|
144 |
Abbreviations are similar to definitions:%
|
|
145 |
\end{isamarkuptext}%
|
|
146 |
\isamarkuptrue%
|
|
147 |
\isacommand{abbreviation}\isamarkupfalse%
|
|
148 |
\ sq{\isaliteral{27}{\isacharprime}}\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline
|
|
149 |
{\isaliteral{22}{\isachardoublequoteopen}}sq{\isaliteral{27}{\isacharprime}}\ n\ {\isaliteral{3D}{\isacharequal}}{\isaliteral{3D}{\isacharequal}}\ n\ {\isaliteral{2A}{\isacharasterisk}}\ n{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
150 |
\begin{isamarkuptext}%
|
|
151 |
The key difference is that \isa{sq{\isaliteral{27}{\isacharprime}}} is only syntactic sugar:
|
|
152 |
\isa{sq{\isaliteral{27}{\isacharprime}}\ t} is replaced by \mbox{\isa{t\ {\isaliteral{2A}{\isacharasterisk}}\ t}} after parsing, and every
|
|
153 |
occurrence of a term \isa{u\ {\isaliteral{2A}{\isacharasterisk}}\ u} is replaced by \mbox{\isa{sq{\isaliteral{27}{\isacharprime}}\ u}} before
|
|
154 |
printing. Internally, \isa{sq{\isaliteral{27}{\isacharprime}}} does not exist. This is also the
|
|
155 |
advantage of abbreviations over definitions: definitions need to be expanded
|
|
156 |
explicitly (see \autoref{sec:rewr-defs}) whereas abbreviations are already
|
|
157 |
expanded upon parsing. However, abbreviations should be introduced sparingly:
|
|
158 |
if abused, they can lead to a confusing discrepancy between the internal and
|
|
159 |
external view of a term.
|
|
160 |
|
|
161 |
\subsection{Recursive functions}
|
|
162 |
\label{sec:recursive-funs}
|
|
163 |
|
|
164 |
Recursive functions are defined with \isacom{fun} by pattern matching
|
|
165 |
over datatype constructors. The order of equations matters. Just as in
|
|
166 |
functional programming languages. However, all HOL functions must be
|
|
167 |
total. This simplifies the logic---terms are always defined---but means
|
|
168 |
that recursive functions must terminate. Otherwise one could define a
|
|
169 |
function \isa{f\ n\ {\isaliteral{3D}{\isacharequal}}\ f\ n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}} and conclude \mbox{\isa{{\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}}} by
|
|
170 |
subtracting \isa{f\ n} on both sides.
|
|
171 |
|
47711
|
172 |
Isabelle's automatic termination checker requires that the arguments of
|
47269
|
173 |
recursive calls on the right-hand side must be strictly smaller than the
|
|
174 |
arguments on the left-hand side. In the simplest case, this means that one
|
|
175 |
fixed argument position decreases in size with each recursive call. The size
|
47711
|
176 |
is measured as the number of constructors (excluding 0-ary ones, e.g.\ \isa{Nil}). Lexicographic combinations are also recognized. In more complicated
|
47269
|
177 |
situations, the user may have to prove termination by hand. For details
|
|
178 |
see~\cite{Krauss}.
|
|
179 |
|
|
180 |
Functions defined with \isacom{fun} come with their own induction schema
|
|
181 |
that mirrors the recursion schema and is derived from the termination
|
|
182 |
order. For example,%
|
|
183 |
\end{isamarkuptext}%
|
|
184 |
\isamarkuptrue%
|
|
185 |
\isacommand{fun}\isamarkupfalse%
|
|
186 |
\ div{\isadigit{2}}\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline
|
|
187 |
{\isaliteral{22}{\isachardoublequoteopen}}div{\isadigit{2}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline
|
|
188 |
{\isaliteral{22}{\isachardoublequoteopen}}div{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline
|
|
189 |
{\isaliteral{22}{\isachardoublequoteopen}}div{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}Suc{\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Suc{\isaliteral{28}{\isacharparenleft}}div{\isadigit{2}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
190 |
\begin{isamarkuptext}%
|
|
191 |
does not just define \isa{div{\isadigit{2}}} but also proves a
|
47711
|
192 |
customized induction rule:
|
47269
|
193 |
\[
|
|
194 |
\inferrule{
|
|
195 |
\mbox{\isa{P\ {\isadigit{0}}}}\\
|
|
196 |
\mbox{\isa{P\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}}}\\
|
|
197 |
\mbox{\isa{{\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ P\ n\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}}}}
|
|
198 |
{\mbox{\isa{P\ m}}}
|
|
199 |
\]
|
47711
|
200 |
This customized induction rule can simplify inductive proofs. For example,%
|
47269
|
201 |
\end{isamarkuptext}%
|
|
202 |
\isamarkuptrue%
|
|
203 |
\isacommand{lemma}\isamarkupfalse%
|
|
204 |
\ {\isaliteral{22}{\isachardoublequoteopen}}div{\isadigit{2}}{\isaliteral{28}{\isacharparenleft}}n{\isaliteral{2B}{\isacharplus}}n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ n{\isaliteral{22}{\isachardoublequoteclose}}\isanewline
|
|
205 |
%
|
|
206 |
\isadelimproof
|
|
207 |
%
|
|
208 |
\endisadelimproof
|
|
209 |
%
|
|
210 |
\isatagproof
|
|
211 |
\isacommand{apply}\isamarkupfalse%
|
|
212 |
{\isaliteral{28}{\isacharparenleft}}induction\ n\ rule{\isaliteral{3A}{\isacharcolon}}\ div{\isadigit{2}}{\isaliteral{2E}{\isachardot}}induct{\isaliteral{29}{\isacharparenright}}%
|
|
213 |
\begin{isamarkuptxt}%
|
|
214 |
yields the 3 subgoals
|
|
215 |
\begin{isabelle}%
|
|
216 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ div{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\isanewline
|
|
217 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ div{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ {\isadigit{0}}\isanewline
|
|
218 |
\ {\isadigit{3}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ div{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2B}{\isacharplus}}\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ n\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline
|
|
219 |
\isaindent{\ {\isadigit{3}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ }div{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{2B}{\isacharplus}}\ Suc\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}%
|
|
220 |
\end{isabelle}
|
|
221 |
An application of \isa{auto} finishes the proof.
|
|
222 |
Had we used ordinary structural induction on \isa{n},
|
|
223 |
the proof would have needed an additional
|
47711
|
224 |
case analysis in the induction step.
|
47269
|
225 |
|
|
226 |
The general case is often called \concept{computation induction},
|
|
227 |
because the induction follows the (terminating!) computation.
|
|
228 |
For every defining equation
|
|
229 |
\begin{quote}
|
|
230 |
\isa{f{\isaliteral{28}{\isacharparenleft}}e{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ f{\isaliteral{28}{\isacharparenleft}}r\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ f{\isaliteral{28}{\isacharparenleft}}r\isaliteral{5C3C5E697375623E}{}\isactrlisub k{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}}
|
|
231 |
\end{quote}
|
|
232 |
where \isa{f{\isaliteral{28}{\isacharparenleft}}r\isaliteral{5C3C5E697375623E}{}\isactrlisub i{\isaliteral{29}{\isacharparenright}}}, \isa{i{\isaliteral{3D}{\isacharequal}}{\isadigit{1}}{\isaliteral{5C3C646F74733E}{\isasymdots}}k}, are all the recursive calls,
|
|
233 |
the induction rule \isa{f{\isaliteral{2E}{\isachardot}}induct} contains one premise of the form
|
|
234 |
\begin{quote}
|
|
235 |
\isa{P{\isaliteral{28}{\isacharparenleft}}r\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{28}{\isacharparenleft}}r\isaliteral{5C3C5E697375623E}{}\isactrlisub k{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{28}{\isacharparenleft}}e{\isaliteral{29}{\isacharparenright}}}
|
|
236 |
\end{quote}
|
|
237 |
If \isa{f\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{5C3C7461753E}{\isasymtau}}\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{5C3C7461753E}{\isasymtau}}\isaliteral{5C3C5E697375623E}{}\isactrlisub n\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{5C3C7461753E}{\isasymtau}}} then \isa{f{\isaliteral{2E}{\isachardot}}induct} is applied like this:
|
|
238 |
\begin{quote}
|
|
239 |
\isacom{apply}\isa{{\isaliteral{28}{\isacharparenleft}}induction\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub n\ rule{\isaliteral{3A}{\isacharcolon}}\ f{\isaliteral{2E}{\isachardot}}induct{\isaliteral{29}{\isacharparenright}}}
|
|
240 |
\end{quote}
|
|
241 |
where typically there is a call \isa{f\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub n} in the goal.
|
|
242 |
But note that the induction rule does not mention \isa{f} at all,
|
|
243 |
except in its name, and is applicable independently of \isa{f}.
|
|
244 |
|
|
245 |
\section{Induction heuristics}
|
|
246 |
|
|
247 |
We have already noted that theorems about recursive functions are proved by
|
|
248 |
induction. In case the function has more than one argument, we have followed
|
|
249 |
the following heuristic in the proofs about the append function:
|
|
250 |
\begin{quote}
|
|
251 |
\emph{Perform induction on argument number $i$\\
|
|
252 |
if the function is defined by recursion on argument number $i$.}
|
|
253 |
\end{quote}
|
|
254 |
The key heuristic, and the main point of this section, is to
|
47711
|
255 |
\emph{generalize the goal before induction}.
|
47269
|
256 |
The reason is simple: if the goal is
|
|
257 |
too specific, the induction hypothesis is too weak to allow the induction
|
|
258 |
step to go through. Let us illustrate the idea with an example.
|
|
259 |
|
|
260 |
Function \isa{rev} has quadratic worst-case running time
|
|
261 |
because it calls append for each element of the list and
|
|
262 |
append is linear in its first argument. A linear time version of
|
|
263 |
\isa{rev} requires an extra argument where the result is accumulated
|
|
264 |
gradually, using only~\isa{{\isaliteral{23}{\isacharhash}}}:%
|
|
265 |
\end{isamarkuptxt}%
|
|
266 |
\isamarkuptrue%
|
|
267 |
%
|
|
268 |
\endisatagproof
|
|
269 |
{\isafoldproof}%
|
|
270 |
%
|
|
271 |
\isadelimproof
|
|
272 |
%
|
|
273 |
\endisadelimproof
|
|
274 |
\isacommand{fun}\isamarkupfalse%
|
|
275 |
\ itrev\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline
|
47711
|
276 |
{\isaliteral{22}{\isachardoublequoteopen}}itrev\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ \ \ \ \ \ \ \ ys\ {\isaliteral{3D}{\isacharequal}}\ ys{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline
|
47269
|
277 |
{\isaliteral{22}{\isachardoublequoteopen}}itrev\ {\isaliteral{28}{\isacharparenleft}}x{\isaliteral{23}{\isacharhash}}xs{\isaliteral{29}{\isacharparenright}}\ ys\ {\isaliteral{3D}{\isacharequal}}\ itrev\ xs\ {\isaliteral{28}{\isacharparenleft}}x{\isaliteral{23}{\isacharhash}}ys{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
278 |
\begin{isamarkuptext}%
|
|
279 |
The behaviour of \isa{itrev} is simple: it reverses
|
|
280 |
its first argument by stacking its elements onto the second argument,
|
47711
|
281 |
and it returns that second argument when the first one becomes
|
47269
|
282 |
empty. Note that \isa{itrev} is tail-recursive: it can be
|
|
283 |
compiled into a loop, no stack is necessary for executing it.
|
|
284 |
|
|
285 |
Naturally, we would like to show that \isa{itrev} does indeed reverse
|
|
286 |
its first argument provided the second one is empty:%
|
|
287 |
\end{isamarkuptext}%
|
|
288 |
\isamarkuptrue%
|
|
289 |
\isacommand{lemma}\isamarkupfalse%
|
|
290 |
\ {\isaliteral{22}{\isachardoublequoteopen}}itrev\ xs\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
291 |
\isadelimproof
|
|
292 |
%
|
|
293 |
\endisadelimproof
|
|
294 |
%
|
|
295 |
\isatagproof
|
|
296 |
%
|
|
297 |
\begin{isamarkuptxt}%
|
|
298 |
There is no choice as to the induction variable:%
|
|
299 |
\end{isamarkuptxt}%
|
|
300 |
\isamarkuptrue%
|
|
301 |
\isacommand{apply}\isamarkupfalse%
|
|
302 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}\isanewline
|
|
303 |
\isacommand{apply}\isamarkupfalse%
|
|
304 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}%
|
|
305 |
\begin{isamarkuptxt}%
|
|
306 |
Unfortunately, this attempt does not prove
|
|
307 |
the induction step:
|
|
308 |
\begin{isabelle}%
|
|
309 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs{\isaliteral{2E}{\isachardot}}\ itrev\ xs\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ itrev\ xs\ {\isaliteral{5B}{\isacharbrackleft}}a{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs\ {\isaliteral{40}{\isacharat}}\ {\isaliteral{5B}{\isacharbrackleft}}a{\isaliteral{5D}{\isacharbrackright}}%
|
|
310 |
\end{isabelle}
|
|
311 |
The induction hypothesis is too weak. The fixed
|
|
312 |
argument,~\isa{{\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}}, prevents it from rewriting the conclusion.
|
|
313 |
This example suggests a heuristic:
|
|
314 |
\begin{quote}
|
47711
|
315 |
\emph{Generalize goals for induction by replacing constants by variables.}
|
47269
|
316 |
\end{quote}
|
|
317 |
Of course one cannot do this na\"{\i}vely: \isa{itrev\ xs\ ys\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs} is
|
47711
|
318 |
just not true. The correct generalization is%
|
47269
|
319 |
\end{isamarkuptxt}%
|
|
320 |
\isamarkuptrue%
|
|
321 |
%
|
|
322 |
\endisatagproof
|
|
323 |
{\isafoldproof}%
|
|
324 |
%
|
|
325 |
\isadelimproof
|
|
326 |
%
|
|
327 |
\endisadelimproof
|
|
328 |
\isacommand{lemma}\isamarkupfalse%
|
|
329 |
\ {\isaliteral{22}{\isachardoublequoteopen}}itrev\ xs\ ys\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs\ {\isaliteral{40}{\isacharat}}\ ys{\isaliteral{22}{\isachardoublequoteclose}}%
|
|
330 |
\isadelimproof
|
|
331 |
%
|
|
332 |
\endisadelimproof
|
|
333 |
%
|
|
334 |
\isatagproof
|
|
335 |
%
|
|
336 |
\begin{isamarkuptxt}%
|
|
337 |
If \isa{ys} is replaced by \isa{{\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}}, the right-hand side simplifies to
|
|
338 |
\isa{rev\ xs}, as required.
|
47711
|
339 |
In this instance it was easy to guess the right generalization.
|
47269
|
340 |
Other situations can require a good deal of creativity.
|
|
341 |
|
|
342 |
Although we now have two variables, only \isa{xs} is suitable for
|
|
343 |
induction, and we repeat our proof attempt. Unfortunately, we are still
|
|
344 |
not there:
|
|
345 |
\begin{isabelle}%
|
|
346 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs{\isaliteral{2E}{\isachardot}}\isanewline
|
|
347 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }itrev\ xs\ ys\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs\ {\isaliteral{40}{\isacharat}}\ ys\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline
|
|
348 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }itrev\ xs\ {\isaliteral{28}{\isacharparenleft}}a\ {\isaliteral{23}{\isacharhash}}\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs\ {\isaliteral{40}{\isacharat}}\ a\ {\isaliteral{23}{\isacharhash}}\ ys%
|
|
349 |
\end{isabelle}
|
|
350 |
The induction hypothesis is still too weak, but this time it takes no
|
47711
|
351 |
intuition to generalize: the problem is that the \isa{ys}
|
47269
|
352 |
in the induction hypothesis is fixed,
|
|
353 |
but the induction hypothesis needs to be applied with
|
|
354 |
\isa{a\ {\isaliteral{23}{\isacharhash}}\ ys} instead of \isa{ys}. Hence we prove the theorem
|
|
355 |
for all \isa{ys} instead of a fixed one. We can instruct induction
|
47711
|
356 |
to perform this generalization for us by adding \isa{arbitrary{\isaliteral{3A}{\isacharcolon}}\ ys}.%
|
47269
|
357 |
\end{isamarkuptxt}%
|
|
358 |
\isamarkuptrue%
|
|
359 |
%
|
|
360 |
\endisatagproof
|
|
361 |
{\isafoldproof}%
|
|
362 |
%
|
|
363 |
\isadelimproof
|
|
364 |
%
|
|
365 |
\endisadelimproof
|
|
366 |
%
|
|
367 |
\isadelimproof
|
|
368 |
%
|
|
369 |
\endisadelimproof
|
|
370 |
%
|
|
371 |
\isatagproof
|
|
372 |
\isacommand{apply}\isamarkupfalse%
|
|
373 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs\ arbitrary{\isaliteral{3A}{\isacharcolon}}\ ys{\isaliteral{29}{\isacharparenright}}%
|
|
374 |
\begin{isamarkuptxt}%
|
|
375 |
The induction hypothesis in the induction step is now universally quantified over \isa{ys}:
|
|
376 |
\begin{isabelle}%
|
|
377 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}ys{\isaliteral{2E}{\isachardot}}\ itrev\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ ys\ {\isaliteral{3D}{\isacharequal}}\ rev\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{40}{\isacharat}}\ ys\isanewline
|
|
378 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs\ ys{\isaliteral{2E}{\isachardot}}\isanewline
|
|
379 |
\isaindent{\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }{\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C416E643E}{\isasymAnd}}ys{\isaliteral{2E}{\isachardot}}\ itrev\ xs\ ys\ {\isaliteral{3D}{\isacharequal}}\ rev\ xs\ {\isaliteral{40}{\isacharat}}\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline
|
|
380 |
\isaindent{\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }itrev\ {\isaliteral{28}{\isacharparenleft}}a\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ ys\ {\isaliteral{3D}{\isacharequal}}\ rev\ {\isaliteral{28}{\isacharparenleft}}a\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{40}{\isacharat}}\ ys%
|
|
381 |
\end{isabelle}
|
|
382 |
Thus the proof succeeds:%
|
|
383 |
\end{isamarkuptxt}%
|
|
384 |
\isamarkuptrue%
|
|
385 |
\isacommand{apply}\isamarkupfalse%
|
|
386 |
\ auto\isanewline
|
|
387 |
\isacommand{done}\isamarkupfalse%
|
|
388 |
%
|
|
389 |
\endisatagproof
|
|
390 |
{\isafoldproof}%
|
|
391 |
%
|
|
392 |
\isadelimproof
|
|
393 |
%
|
|
394 |
\endisadelimproof
|
|
395 |
%
|
|
396 |
\begin{isamarkuptext}%
|
47711
|
397 |
This leads to another heuristic for generalization:
|
47269
|
398 |
\begin{quote}
|
47711
|
399 |
\emph{Generalize induction by generalizing all free
|
47269
|
400 |
variables\\ {\em(except the induction variable itself)}.}
|
|
401 |
\end{quote}
|
47711
|
402 |
Generalization is best performed with \isa{arbitrary{\isaliteral{3A}{\isacharcolon}}\ y\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ y\isaliteral{5C3C5E697375623E}{}\isactrlisub k}.
|
47269
|
403 |
This heuristic prevents trivial failures like the one above.
|
|
404 |
However, it should not be applied blindly.
|
|
405 |
It is not always required, and the additional quantifiers can complicate
|
|
406 |
matters in some cases. The variables that need to be quantified are typically
|
|
407 |
those that change in recursive calls.
|
|
408 |
|
|
409 |
\section{Simplification}
|
|
410 |
|
|
411 |
So far we have talked a lot about simplifying terms without explaining the concept. \concept{Simplification} means
|
|
412 |
\begin{itemize}
|
|
413 |
\item using equations $l = r$ from left to right (only),
|
|
414 |
\item as long as possible.
|
|
415 |
\end{itemize}
|
47711
|
416 |
To emphasize the directionality, equations that have been given the
|
47269
|
417 |
\isa{simp} attribute are called \concept{simplification}
|
|
418 |
rules. Logically, they are still symmetric, but proofs by
|
|
419 |
simplification use them only in the left-to-right direction. The proof tool
|
|
420 |
that performs simplifications is called the \concept{simplifier}. It is the
|
|
421 |
basis of \isa{auto} and other related proof methods.
|
|
422 |
|
|
423 |
The idea of simplification is best explained by an example. Given the
|
|
424 |
simplification rules
|
|
425 |
\[
|
|
426 |
\begin{array}{rcl@ {\quad}l}
|
|
427 |
\isa{{\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ n} &\isa{{\isaliteral{3D}{\isacharequal}}}& \isa{n} & (1) \\
|
|
428 |
\isa{Suc\ m\ {\isaliteral{2B}{\isacharplus}}\ n} &\isa{{\isaliteral{3D}{\isacharequal}}}& \isa{Suc\ {\isaliteral{28}{\isacharparenleft}}m\ {\isaliteral{2B}{\isacharplus}}\ n{\isaliteral{29}{\isacharparenright}}} & (2) \\
|
|
429 |
\isa{{\isaliteral{28}{\isacharparenleft}}Suc\ m\ {\isaliteral{5C3C6C653E}{\isasymle}}\ Suc\ n{\isaliteral{29}{\isacharparenright}}} &\isa{{\isaliteral{3D}{\isacharequal}}}& \isa{{\isaliteral{28}{\isacharparenleft}}m\ {\isaliteral{5C3C6C653E}{\isasymle}}\ n{\isaliteral{29}{\isacharparenright}}} & (3)\\
|
|
430 |
\isa{{\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ {\isaliteral{5C3C6C653E}{\isasymle}}\ m{\isaliteral{29}{\isacharparenright}}} &\isa{{\isaliteral{3D}{\isacharequal}}}& \isa{True} & (4)
|
|
431 |
\end{array}
|
|
432 |
\]
|
|
433 |
the formula \isa{{\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ Suc\ {\isadigit{0}}\ {\isaliteral{5C3C6C653E}{\isasymle}}\ Suc\ {\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ x} is simplified to \isa{True}
|
|
434 |
as follows:
|
|
435 |
\[
|
|
436 |
\begin{array}{r@ {}c@ {}l@ {\quad}l}
|
|
437 |
\isa{{\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ Suc\ {\isadigit{0}}} & \leq & \isa{Suc\ {\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ x{\isaliteral{29}{\isacharparenright}}} & \stackrel{(1)}{=} \\
|
|
438 |
\isa{{\isaliteral{28}{\isacharparenleft}}Suc\ {\isadigit{0}}} & \leq & \isa{Suc\ {\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ x{\isaliteral{29}{\isacharparenright}}} & \stackrel{(2)}{=} \\
|
|
439 |
\isa{{\isaliteral{28}{\isacharparenleft}}Suc\ {\isadigit{0}}} & \leq & \isa{Suc\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ x{\isaliteral{29}{\isacharparenright}}} & \stackrel{(3)}{=} \\
|
|
440 |
\isa{{\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}} & \leq & \isa{{\isadigit{0}}\ {\isaliteral{2B}{\isacharplus}}\ x{\isaliteral{29}{\isacharparenright}}} & \stackrel{(4)}{=} \\[1ex]
|
|
441 |
& \isa{True}
|
|
442 |
\end{array}
|
|
443 |
\]
|
|
444 |
Simplification is often also called \concept{rewriting}
|
|
445 |
and simplification rules \concept{rewrite rules}.
|
|
446 |
|
|
447 |
\subsection{Simplification rules}
|
|
448 |
|
|
449 |
The attribute \isa{simp} declares theorems to be simplification rules,
|
|
450 |
which the simplifier will use automatically. In addition,
|
|
451 |
\isacom{datatype} and \isacom{fun} commands implicitly declare some
|
|
452 |
simplification rules: \isacom{datatype} the distinctness and injectivity
|
|
453 |
rules, \isacom{fun} the defining equations. Definitions are not declared
|
|
454 |
as simplification rules automatically! Nearly any theorem can become a
|
|
455 |
simplification rule. The simplifier will try to transform it into an
|
|
456 |
equation. For example, the theorem \isa{{\isaliteral{5C3C6E6F743E}{\isasymnot}}\ P} is turned into \isa{P\ {\isaliteral{3D}{\isacharequal}}\ False}.
|
|
457 |
|
|
458 |
Only equations that really simplify, like \isa{rev\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs} and
|
|
459 |
\isa{xs\ {\isaliteral{40}{\isacharat}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ xs}, should be declared as simplification
|
|
460 |
rules. Equations that may be counterproductive as simplification rules
|
|
461 |
should only be used in specific proof steps (see \S\ref{sec:simp} below).
|
|
462 |
Distributivity laws, for example, alter the structure of terms
|
|
463 |
and can produce an exponential blow-up.
|
|
464 |
|
|
465 |
\subsection{Conditional simplification rules}
|
|
466 |
|
|
467 |
Simplification rules can be conditional. Before applying such a rule, the
|
|
468 |
simplifier will first try to prove the preconditions, again by
|
|
469 |
simplification. For example, given the simplification rules
|
|
470 |
\begin{quote}
|
|
471 |
\isa{p\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ True}\\
|
|
472 |
\isa{p\ x\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ f\ x\ {\isaliteral{3D}{\isacharequal}}\ g\ x},
|
|
473 |
\end{quote}
|
|
474 |
the term \isa{f\ {\isadigit{0}}} simplifies to \isa{g\ {\isadigit{0}}}
|
|
475 |
but \isa{f\ {\isadigit{1}}} does not simplify because \isa{p\ {\isadigit{1}}}
|
|
476 |
is not provable.
|
|
477 |
|
|
478 |
\subsection{Termination}
|
|
479 |
|
|
480 |
Simplification can run forever, for example if both \isa{f\ x\ {\isaliteral{3D}{\isacharequal}}\ g\ x} and
|
|
481 |
\isa{g\ x\ {\isaliteral{3D}{\isacharequal}}\ f\ x} are simplification rules. It is the user's
|
|
482 |
responsibility not to include simplification rules that can lead to
|
|
483 |
nontermination, either on their own or in combination with other
|
|
484 |
simplification rules. The right-hand side of a simplification rule should
|
|
485 |
always be ``simpler'' than the left-hand side---in some sense. But since
|
|
486 |
termination is undecidable, such a check cannot be automated completely
|
|
487 |
and Isabelle makes little attempt to detect nontermination.
|
|
488 |
|
|
489 |
When conditional simplification rules are applied, their preconditions are
|
|
490 |
proved first. Hence all preconditions need to be
|
|
491 |
simpler than the left-hand side of the conclusion. For example
|
|
492 |
\begin{quote}
|
|
493 |
\isa{n\ {\isaliteral{3C}{\isacharless}}\ m\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{3C}{\isacharless}}\ Suc\ m{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ True}
|
|
494 |
\end{quote}
|
|
495 |
is suitable as a simplification rule: both \isa{n\ {\isaliteral{3C}{\isacharless}}\ m} and \isa{True}
|
|
496 |
are simpler than \mbox{\isa{n\ {\isaliteral{3C}{\isacharless}}\ Suc\ m}}. But
|
|
497 |
\begin{quote}
|
|
498 |
\isa{Suc\ n\ {\isaliteral{3C}{\isacharless}}\ m\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{3C}{\isacharless}}\ m{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ True}
|
|
499 |
\end{quote}
|
|
500 |
leads to nontermination: when trying to rewrite \isa{n\ {\isaliteral{3C}{\isacharless}}\ m} to \isa{True}
|
|
501 |
one first has to prove \mbox{\isa{Suc\ n\ {\isaliteral{3C}{\isacharless}}\ m}}, which can be rewritten to \isa{True} provided \isa{Suc\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3C}{\isacharless}}\ m}, \emph{ad infinitum}.
|
|
502 |
|
|
503 |
\subsection{The \isa{simp} proof method}
|
|
504 |
\label{sec:simp}
|
|
505 |
|
|
506 |
So far we have only used the proof method \isa{auto}. Method \isa{simp}
|
|
507 |
is the key component of \isa{auto}, but \isa{auto} can do much more. In
|
|
508 |
some cases, \isa{auto} is overeager and modifies the proof state too much.
|
|
509 |
In such cases the more predictable \isa{simp} method should be used.
|
|
510 |
Given a goal
|
|
511 |
\begin{quote}
|
|
512 |
\isa{{\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}\ P\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}{\isaliteral{3B}{\isacharsemicolon}}\ P\isaliteral{5C3C5E697375623E}{}\isactrlisub m\ {\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ C}
|
|
513 |
\end{quote}
|
|
514 |
the command
|
|
515 |
\begin{quote}
|
|
516 |
\isacom{apply}\isa{{\isaliteral{28}{\isacharparenleft}}simp\ add{\isaliteral{3A}{\isacharcolon}}\ th\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ th\isaliteral{5C3C5E697375623E}{}\isactrlisub n{\isaliteral{29}{\isacharparenright}}}
|
|
517 |
\end{quote}
|
|
518 |
simplifies the assumptions \isa{P\isaliteral{5C3C5E697375623E}{}\isactrlisub i} and the conclusion \isa{C} using
|
|
519 |
\begin{itemize}
|
|
520 |
\item all simplification rules, including the ones coming from \isacom{datatype} and \isacom{fun},
|
|
521 |
\item the additional lemmas \isa{th\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ th\isaliteral{5C3C5E697375623E}{}\isactrlisub n}, and
|
|
522 |
\item the assumptions.
|
|
523 |
\end{itemize}
|
|
524 |
In addition to or instead of \isa{add} there is also \isa{del} for removing
|
|
525 |
simplification rules temporarily. Both are optional. Method \isa{auto}
|
|
526 |
can be modified similarly:
|
|
527 |
\begin{quote}
|
|
528 |
\isacom{apply}\isa{{\isaliteral{28}{\isacharparenleft}}auto\ simp\ add{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ simp\ del{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}{\isaliteral{29}{\isacharparenright}}}
|
|
529 |
\end{quote}
|
|
530 |
Here the modifiers are \isa{simp\ add} and \isa{simp\ del}
|
|
531 |
instead of just \isa{add} and \isa{del} because \isa{auto}
|
|
532 |
does not just perform simplification.
|
|
533 |
|
|
534 |
Note that \isa{simp} acts only on subgoal 1, \isa{auto} acts on all
|
|
535 |
subgoals. There is also \isa{simp{\isaliteral{5F}{\isacharunderscore}}all}, which applies \isa{simp} to
|
|
536 |
all subgoals.
|
|
537 |
|
|
538 |
\subsection{Rewriting with definitions}
|
|
539 |
\label{sec:rewr-defs}
|
|
540 |
|
|
541 |
Definitions introduced by the command \isacom{definition}
|
|
542 |
can also be used as simplification rules,
|
|
543 |
but by default they are not: the simplifier does not expand them
|
|
544 |
automatically. Definitions are intended for introducing abstract concepts and
|
|
545 |
not merely as abbreviations. Of course, we need to expand the definition
|
|
546 |
initially, but once we have proved enough abstract properties of the new
|
|
547 |
constant, we can forget its original definition. This style makes proofs more
|
|
548 |
robust: if the definition has to be changed, only the proofs of the abstract
|
|
549 |
properties will be affected.
|
|
550 |
|
|
551 |
The definition of a function \isa{f} is a theorem named \isa{f{\isaliteral{5F}{\isacharunderscore}}def}
|
|
552 |
and can be added to a call of \isa{simp} just like any other theorem:
|
|
553 |
\begin{quote}
|
|
554 |
\isacom{apply}\isa{{\isaliteral{28}{\isacharparenleft}}simp\ add{\isaliteral{3A}{\isacharcolon}}\ f{\isaliteral{5F}{\isacharunderscore}}def{\isaliteral{29}{\isacharparenright}}}
|
|
555 |
\end{quote}
|
|
556 |
In particular, let-expressions can be unfolded by
|
|
557 |
making \isa{Let{\isaliteral{5F}{\isacharunderscore}}def} a simplification rule.
|
|
558 |
|
|
559 |
\subsection{Case splitting with \isa{simp}}
|
|
560 |
|
|
561 |
Goals containing if-expressions are automatically split into two cases by
|
|
562 |
\isa{simp} using the rule
|
|
563 |
\begin{quote}
|
|
564 |
\isa{P\ {\isaliteral{28}{\isacharparenleft}}if\ A\ then\ s\ else\ t{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{28}{\isacharparenleft}}A\ {\isaliteral{5C3C6C6F6E6772696768746172726F773E}{\isasymlongrightarrow}}\ P\ s{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C616E643E}{\isasymand}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6E6F743E}{\isasymnot}}\ A\ {\isaliteral{5C3C6C6F6E6772696768746172726F773E}{\isasymlongrightarrow}}\ P\ t{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}}
|
|
565 |
\end{quote}
|
|
566 |
For example, \isa{simp} can prove
|
|
567 |
\begin{quote}
|
|
568 |
\isa{{\isaliteral{28}{\isacharparenleft}}A\ {\isaliteral{5C3C616E643E}{\isasymand}}\ B{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ A\ then\ B\ else\ False{\isaliteral{29}{\isacharparenright}}}
|
|
569 |
\end{quote}
|
|
570 |
because both \isa{A\ {\isaliteral{5C3C6C6F6E6772696768746172726F773E}{\isasymlongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}A\ {\isaliteral{5C3C616E643E}{\isasymand}}\ B{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ B} and \isa{{\isaliteral{5C3C6E6F743E}{\isasymnot}}\ A\ {\isaliteral{5C3C6C6F6E6772696768746172726F773E}{\isasymlongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}A\ {\isaliteral{5C3C616E643E}{\isasymand}}\ B{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ False}
|
|
571 |
simplify to \isa{True}.
|
|
572 |
|
|
573 |
We can split case-expressions similarly. For \isa{nat} the rule looks like this:
|
|
574 |
\begin{isabelle}%
|
|
575 |
\ \ \ \ P\ {\isaliteral{28}{\isacharparenleft}}case\ e\ of\ {\isadigit{0}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ a\ {\isaliteral{7C}{\isacharbar}}\ Suc\ n\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ b\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\isanewline
|
|
576 |
\isaindent{\ \ \ \ }{\isaliteral{28}{\isacharparenleft}}{\isaliteral{28}{\isacharparenleft}}e\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ {\isaliteral{5C3C6C6F6E6772696768746172726F773E}{\isasymlongrightarrow}}\ P\ a{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C616E643E}{\isasymand}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C666F72616C6C3E}{\isasymforall}}n{\isaliteral{2E}{\isachardot}}\ e\ {\isaliteral{3D}{\isacharequal}}\ Suc\ n\ {\isaliteral{5C3C6C6F6E6772696768746172726F773E}{\isasymlongrightarrow}}\ P\ {\isaliteral{28}{\isacharparenleft}}b\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}%
|
|
577 |
\end{isabelle}
|
|
578 |
Case expressions are not split automatically by \isa{simp}, but \isa{simp}
|
|
579 |
can be instructed to do so:
|
|
580 |
\begin{quote}
|
|
581 |
\isacom{apply}\isa{{\isaliteral{28}{\isacharparenleft}}simp\ split{\isaliteral{3A}{\isacharcolon}}\ nat{\isaliteral{2E}{\isachardot}}split{\isaliteral{29}{\isacharparenright}}}
|
|
582 |
\end{quote}
|
|
583 |
splits all case-expressions over natural numbers. For an arbitrary
|
|
584 |
datatype \isa{t} it is \isa{t{\isaliteral{2E}{\isachardot}}split} instead of \isa{nat{\isaliteral{2E}{\isachardot}}split}.
|
|
585 |
Method \isa{auto} can be modified in exactly the same way.%
|
|
586 |
\end{isamarkuptext}%
|
|
587 |
\isamarkuptrue%
|
|
588 |
%
|
|
589 |
\isadelimtheory
|
|
590 |
%
|
|
591 |
\endisadelimtheory
|
|
592 |
%
|
|
593 |
\isatagtheory
|
|
594 |
%
|
|
595 |
\endisatagtheory
|
|
596 |
{\isafoldtheory}%
|
|
597 |
%
|
|
598 |
\isadelimtheory
|
|
599 |
%
|
|
600 |
\endisadelimtheory
|
|
601 |
\end{isabellebody}%
|
|
602 |
%%% Local Variables:
|
|
603 |
%%% mode: latex
|
|
604 |
%%% TeX-master: "root"
|
|
605 |
%%% End:
|