author | nipkow |
Wed, 01 Jun 2011 22:42:37 +0200 | |
changeset 43143 | 1aeafba76f21 |
parent 43042 | 0f9534b7ea75 |
permissions | -rw-r--r-- |
21212 | 1 |
% |
2 |
\begin{isabellebody}% |
|
3 |
\def\isabellecontext{Functions}% |
|
4 |
% |
|
5 |
\isadelimtheory |
|
6 |
\isanewline |
|
7 |
\isanewline |
|
8 |
% |
|
9 |
\endisadelimtheory |
|
10 |
% |
|
11 |
\isatagtheory |
|
12 |
\isacommand{theory}\isamarkupfalse% |
|
13 |
\ Functions\isanewline |
|
14 |
\isakeyword{imports}\ Main\isanewline |
|
15 |
\isakeyword{begin}% |
|
16 |
\endisatagtheory |
|
17 |
{\isafoldtheory}% |
|
18 |
% |
|
19 |
\isadelimtheory |
|
20 |
% |
|
21 |
\endisadelimtheory |
|
22 |
% |
|
23188 | 23 |
\isamarkupsection{Function Definitions for Dummies% |
21212 | 24 |
} |
25 |
\isamarkuptrue% |
|
26 |
% |
|
27 |
\begin{isamarkuptext}% |
|
23188 | 28 |
In most cases, defining a recursive function is just as simple as other definitions:% |
21212 | 29 |
\end{isamarkuptext}% |
30 |
\isamarkuptrue% |
|
31 |
\isacommand{fun}\isamarkupfalse% |
|
40406 | 32 |
\ fib\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
21212 | 33 |
\isakeyword{where}\isanewline |
40406 | 34 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}fib\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
35 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}fib\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
36 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}fib\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ fib\ n\ {\isaliteral{2B}{\isacharplus}}\ fib\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
21212 | 37 |
\begin{isamarkuptext}% |
23003 | 38 |
The syntax is rather self-explanatory: We introduce a function by |
25091
a2ae7f71613d
Updated function tutorial: Types can be inferred and need not be given anymore
krauss
parents:
23805
diff
changeset
|
39 |
giving its name, its type, |
a2ae7f71613d
Updated function tutorial: Types can be inferred and need not be given anymore
krauss
parents:
23805
diff
changeset
|
40 |
and a set of defining recursive equations. |
a2ae7f71613d
Updated function tutorial: Types can be inferred and need not be given anymore
krauss
parents:
23805
diff
changeset
|
41 |
If we leave out the type, the most general type will be |
40406 | 42 |
inferred, which can sometimes lead to surprises: Since both \isa{{\isadigit{1}}} and \isa{{\isaliteral{2B}{\isacharplus}}} are overloaded, we would end up |
43 |
with \isa{fib\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}{\isaliteral{7B}{\isacharbraceleft}}one{\isaliteral{2C}{\isacharcomma}}plus{\isaliteral{7D}{\isacharbraceright}}}.% |
|
23003 | 44 |
\end{isamarkuptext}% |
45 |
\isamarkuptrue% |
|
46 |
% |
|
47 |
\begin{isamarkuptext}% |
|
48 |
The function always terminates, since its argument gets smaller in |
|
23188 | 49 |
every recursive call. |
50 |
Since HOL is a logic of total functions, termination is a |
|
51 |
fundamental requirement to prevent inconsistencies\footnote{From the |
|
40406 | 52 |
\qt{definition} \isa{f{\isaliteral{28}{\isacharparenleft}}n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ f{\isaliteral{28}{\isacharparenleft}}n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}} we could prove |
53 |
\isa{{\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}} by subtracting \isa{f{\isaliteral{28}{\isacharparenleft}}n{\isaliteral{29}{\isacharparenright}}} on both sides.}. |
|
23188 | 54 |
Isabelle tries to prove termination automatically when a definition |
55 |
is made. In \S\ref{termination}, we will look at cases where this |
|
56 |
fails and see what to do then.% |
|
21212 | 57 |
\end{isamarkuptext}% |
58 |
\isamarkuptrue% |
|
59 |
% |
|
60 |
\isamarkupsubsection{Pattern matching% |
|
61 |
} |
|
62 |
\isamarkuptrue% |
|
63 |
% |
|
64 |
\begin{isamarkuptext}% |
|
22065 | 65 |
\label{patmatch} |
23003 | 66 |
Like in functional programming, we can use pattern matching to |
67 |
define functions. At the moment we will only consider \emph{constructor |
|
21212 | 68 |
patterns}, which only consist of datatype constructors and |
23805 | 69 |
variables. Furthermore, patterns must be linear, i.e.\ all variables |
70 |
on the left hand side of an equation must be distinct. In |
|
71 |
\S\ref{genpats} we discuss more general pattern matching. |
|
21212 | 72 |
|
73 |
If patterns overlap, the order of the equations is taken into |
|
74 |
account. The following function inserts a fixed element between any |
|
75 |
two elements of a list:% |
|
76 |
\end{isamarkuptext}% |
|
77 |
\isamarkuptrue% |
|
78 |
\isacommand{fun}\isamarkupfalse% |
|
40406 | 79 |
\ sep\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
21212 | 80 |
\isakeyword{where}\isanewline |
40406 | 81 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}sep\ a\ {\isaliteral{28}{\isacharparenleft}}x{\isaliteral{23}{\isacharhash}}y{\isaliteral{23}{\isacharhash}}xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ x\ {\isaliteral{23}{\isacharhash}}\ a\ {\isaliteral{23}{\isacharhash}}\ sep\ a\ {\isaliteral{28}{\isacharparenleft}}y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
82 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}sep\ a\ xs\ \ \ \ \ \ \ {\isaliteral{3D}{\isacharequal}}\ xs{\isaliteral{22}{\isachardoublequoteclose}}% |
|
21212 | 83 |
\begin{isamarkuptext}% |
23188 | 84 |
Overlapping patterns are interpreted as \qt{increments} to what is |
21212 | 85 |
already there: The second equation is only meant for the cases where |
86 |
the first one does not match. Consequently, Isabelle replaces it |
|
22065 | 87 |
internally by the remaining cases, making the patterns disjoint:% |
88 |
\end{isamarkuptext}% |
|
89 |
\isamarkuptrue% |
|
90 |
\isacommand{thm}\isamarkupfalse% |
|
40406 | 91 |
\ sep{\isaliteral{2E}{\isachardot}}simps% |
22065 | 92 |
\begin{isamarkuptext}% |
93 |
\begin{isabelle}% |
|
40406 | 94 |
sep\ a\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ x\ {\isaliteral{23}{\isacharhash}}\ a\ {\isaliteral{23}{\isacharhash}}\ sep\ a\ {\isaliteral{28}{\isacharparenleft}}y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\isasep\isanewline% |
95 |
sep\ a\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\isasep\isanewline% |
|
96 |
sep\ a\ {\isaliteral{5B}{\isacharbrackleft}}v{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}v{\isaliteral{5D}{\isacharbrackright}}% |
|
21212 | 97 |
\end{isabelle}% |
98 |
\end{isamarkuptext}% |
|
99 |
\isamarkuptrue% |
|
100 |
% |
|
101 |
\begin{isamarkuptext}% |
|
23805 | 102 |
\noindent The equations from function definitions are automatically used in |
21212 | 103 |
simplification:% |
104 |
\end{isamarkuptext}% |
|
105 |
\isamarkuptrue% |
|
106 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 107 |
\ {\isaliteral{22}{\isachardoublequoteopen}}sep\ {\isadigit{0}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{3}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{2}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isadigit{3}}{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
21212 | 108 |
% |
109 |
\isadelimproof |
|
110 |
% |
|
111 |
\endisadelimproof |
|
112 |
% |
|
113 |
\isatagproof |
|
114 |
\isacommand{by}\isamarkupfalse% |
|
115 |
\ simp% |
|
116 |
\endisatagproof |
|
117 |
{\isafoldproof}% |
|
118 |
% |
|
119 |
\isadelimproof |
|
120 |
% |
|
121 |
\endisadelimproof |
|
122 |
% |
|
123 |
\isamarkupsubsection{Induction% |
|
124 |
} |
|
125 |
\isamarkuptrue% |
|
126 |
% |
|
127 |
\begin{isamarkuptext}% |
|
23805 | 128 |
Isabelle provides customized induction rules for recursive |
129 |
functions. These rules follow the recursive structure of the |
|
40406 | 130 |
definition. Here is the rule \isa{sep{\isaliteral{2E}{\isachardot}}induct} arising from the |
23805 | 131 |
above definition of \isa{sep}: |
23188 | 132 |
|
23805 | 133 |
\begin{isabelle}% |
40406 | 134 |
{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}{\isaliteral{5C3C416E643E}{\isasymAnd}}a\ x\ y\ xs{\isaliteral{2E}{\isachardot}}\ {\isaliteral{3F}{\isacharquery}}P\ a\ {\isaliteral{28}{\isacharparenleft}}y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}P\ a\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a{\isaliteral{2E}{\isachardot}}\ {\isaliteral{3F}{\isacharquery}}P\ a\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ v{\isaliteral{2E}{\isachardot}}\ {\isaliteral{3F}{\isacharquery}}P\ a\ {\isaliteral{5B}{\isacharbrackleft}}v{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\isanewline |
135 |
{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}P\ {\isaliteral{3F}{\isacharquery}}a{\isadigit{0}}{\isaliteral{2E}{\isachardot}}{\isadigit{0}}\ {\isaliteral{3F}{\isacharquery}}a{\isadigit{1}}{\isaliteral{2E}{\isachardot}}{\isadigit{0}}% |
|
23805 | 136 |
\end{isabelle} |
137 |
||
138 |
We have a step case for list with at least two elements, and two |
|
139 |
base cases for the zero- and the one-element list. Here is a simple |
|
140 |
proof about \isa{sep} and \isa{map}% |
|
141 |
\end{isamarkuptext}% |
|
142 |
\isamarkuptrue% |
|
143 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 144 |
\ {\isaliteral{22}{\isachardoublequoteopen}}map\ f\ {\isaliteral{28}{\isacharparenleft}}sep\ x\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ sep\ {\isaliteral{28}{\isacharparenleft}}f\ x{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}map\ f\ ys{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23805 | 145 |
% |
146 |
\isadelimproof |
|
147 |
% |
|
148 |
\endisadelimproof |
|
149 |
% |
|
150 |
\isatagproof |
|
151 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 152 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ x\ ys\ rule{\isaliteral{3A}{\isacharcolon}}\ sep{\isaliteral{2E}{\isachardot}}induct{\isaliteral{29}{\isacharparenright}}% |
23805 | 153 |
\begin{isamarkuptxt}% |
154 |
We get three cases, like in the definition. |
|
23188 | 155 |
|
23805 | 156 |
\begin{isabelle}% |
40406 | 157 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ x\ y\ xs{\isaliteral{2E}{\isachardot}}\isanewline |
158 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }map\ f\ {\isaliteral{28}{\isacharparenleft}}sep\ a\ {\isaliteral{28}{\isacharparenleft}}y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ sep\ {\isaliteral{28}{\isacharparenleft}}f\ a{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}map\ f\ {\isaliteral{28}{\isacharparenleft}}y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline |
|
159 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }map\ f\ {\isaliteral{28}{\isacharparenleft}}sep\ a\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ sep\ {\isaliteral{28}{\isacharparenleft}}f\ a{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}map\ f\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ y\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\isanewline |
|
160 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a{\isaliteral{2E}{\isachardot}}\ map\ f\ {\isaliteral{28}{\isacharparenleft}}sep\ a\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ sep\ {\isaliteral{28}{\isacharparenleft}}f\ a{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}map\ f\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{29}{\isacharparenright}}\isanewline |
|
161 |
\ {\isadigit{3}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ v{\isaliteral{2E}{\isachardot}}\ map\ f\ {\isaliteral{28}{\isacharparenleft}}sep\ a\ {\isaliteral{5B}{\isacharbrackleft}}v{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ sep\ {\isaliteral{28}{\isacharparenleft}}f\ a{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}map\ f\ {\isaliteral{5B}{\isacharbrackleft}}v{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{29}{\isacharparenright}}% |
|
23805 | 162 |
\end{isabelle}% |
163 |
\end{isamarkuptxt}% |
|
164 |
\isamarkuptrue% |
|
165 |
\isacommand{apply}\isamarkupfalse% |
|
166 |
\ auto\ \isanewline |
|
167 |
\isacommand{done}\isamarkupfalse% |
|
168 |
% |
|
169 |
\endisatagproof |
|
170 |
{\isafoldproof}% |
|
171 |
% |
|
172 |
\isadelimproof |
|
173 |
% |
|
174 |
\endisadelimproof |
|
175 |
% |
|
176 |
\begin{isamarkuptext}% |
|
177 |
With the \cmd{fun} command, you can define about 80\% of the |
|
23188 | 178 |
functions that occur in practice. The rest of this tutorial explains |
179 |
the remaining 20\%.% |
|
21212 | 180 |
\end{isamarkuptext}% |
181 |
\isamarkuptrue% |
|
182 |
% |
|
23188 | 183 |
\isamarkupsection{fun vs.\ function% |
21212 | 184 |
} |
185 |
\isamarkuptrue% |
|
186 |
% |
|
187 |
\begin{isamarkuptext}% |
|
23188 | 188 |
The \cmd{fun} command provides a |
21212 | 189 |
convenient shorthand notation for simple function definitions. In |
190 |
this mode, Isabelle tries to solve all the necessary proof obligations |
|
27026 | 191 |
automatically. If any proof fails, the definition is |
22065 | 192 |
rejected. This can either mean that the definition is indeed faulty, |
193 |
or that the default proof procedures are just not smart enough (or |
|
194 |
rather: not designed) to handle the definition. |
|
195 |
||
23188 | 196 |
By expanding the abbreviation to the more verbose \cmd{function} command, these proof obligations become visible and can be analyzed or |
197 |
solved manually. The expansion from \cmd{fun} to \cmd{function} is as follows: |
|
22065 | 198 |
|
199 |
\end{isamarkuptext} |
|
200 |
||
201 |
||
23188 | 202 |
\[\left[\;\begin{minipage}{0.25\textwidth}\vspace{6pt} |
40406 | 203 |
\cmd{fun} \isa{f\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{5C3C7461753E}{\isasymtau}}}\\% |
23188 | 204 |
\cmd{where}\\% |
205 |
\hspace*{2ex}{\it equations}\\% |
|
206 |
\hspace*{2ex}\vdots\vspace*{6pt} |
|
207 |
\end{minipage}\right] |
|
208 |
\quad\equiv\quad |
|
27026 | 209 |
\left[\;\begin{minipage}{0.48\textwidth}\vspace{6pt} |
40406 | 210 |
\cmd{function} \isa{{\isaliteral{28}{\isacharparenleft}}}\cmd{sequential}\isa{{\isaliteral{29}{\isacharparenright}}\ f\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{5C3C7461753E}{\isasymtau}}}\\% |
23188 | 211 |
\cmd{where}\\% |
212 |
\hspace*{2ex}{\it equations}\\% |
|
213 |
\hspace*{2ex}\vdots\\% |
|
40406 | 214 |
\cmd{by} \isa{pat{\isaliteral{5F}{\isacharunderscore}}completeness\ auto}\\% |
215 |
\cmd{termination by} \isa{lexicographic{\isaliteral{5F}{\isacharunderscore}}order}\vspace{6pt} |
|
23188 | 216 |
\end{minipage} |
217 |
\right]\] |
|
22065 | 218 |
|
219 |
\begin{isamarkuptext} |
|
220 |
\vspace*{1em} |
|
23188 | 221 |
\noindent Some details have now become explicit: |
21212 | 222 |
|
223 |
\begin{enumerate} |
|
22065 | 224 |
\item The \cmd{sequential} option enables the preprocessing of |
23805 | 225 |
pattern overlaps which we already saw. Without this option, the equations |
21212 | 226 |
must already be disjoint and complete. The automatic completion only |
23188 | 227 |
works with constructor patterns. |
21212 | 228 |
|
23188 | 229 |
\item A function definition produces a proof obligation which |
230 |
expresses completeness and compatibility of patterns (we talk about |
|
40406 | 231 |
this later). The combination of the methods \isa{pat{\isaliteral{5F}{\isacharunderscore}}completeness} and |
22065 | 232 |
\isa{auto} is used to solve this proof obligation. |
21212 | 233 |
|
234 |
\item A termination proof follows the definition, started by the |
|
23188 | 235 |
\cmd{termination} command. This will be explained in \S\ref{termination}. |
22065 | 236 |
\end{enumerate} |
237 |
Whenever a \cmd{fun} command fails, it is usually a good idea to |
|
238 |
expand the syntax to the more verbose \cmd{function} form, to see |
|
239 |
what is actually going on.% |
|
21212 | 240 |
\end{isamarkuptext}% |
241 |
\isamarkuptrue% |
|
242 |
% |
|
23188 | 243 |
\isamarkupsection{Termination% |
244 |
} |
|
245 |
\isamarkuptrue% |
|
246 |
% |
|
247 |
\begin{isamarkuptext}% |
|
248 |
\label{termination} |
|
40406 | 249 |
The method \isa{lexicographic{\isaliteral{5F}{\isacharunderscore}}order} is the default method for |
23805 | 250 |
termination proofs. It can prove termination of a |
23188 | 251 |
certain class of functions by searching for a suitable lexicographic |
252 |
combination of size measures. Of course, not all functions have such |
|
23805 | 253 |
a simple termination argument. For them, we can specify the termination |
254 |
relation manually.% |
|
23188 | 255 |
\end{isamarkuptext}% |
256 |
\isamarkuptrue% |
|
257 |
% |
|
258 |
\isamarkupsubsection{The {\tt relation} method% |
|
21212 | 259 |
} |
260 |
\isamarkuptrue% |
|
261 |
% |
|
262 |
\begin{isamarkuptext}% |
|
263 |
Consider the following function, which sums up natural numbers up to |
|
22065 | 264 |
\isa{N}, using a counter \isa{i}:% |
21212 | 265 |
\end{isamarkuptext}% |
266 |
\isamarkuptrue% |
|
267 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 268 |
\ sum\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
21212 | 269 |
\isakeyword{where}\isanewline |
40406 | 270 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}sum\ i\ N\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ i\ {\isaliteral{3E}{\isachargreater}}\ N\ then\ {\isadigit{0}}\ else\ i\ {\isaliteral{2B}{\isacharplus}}\ sum\ {\isaliteral{28}{\isacharparenleft}}Suc\ i{\isaliteral{29}{\isacharparenright}}\ N{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
21212 | 271 |
% |
272 |
\isadelimproof |
|
22065 | 273 |
% |
21212 | 274 |
\endisadelimproof |
275 |
% |
|
276 |
\isatagproof |
|
277 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 278 |
\ pat{\isaliteral{5F}{\isacharunderscore}}completeness\ auto% |
21212 | 279 |
\endisatagproof |
280 |
{\isafoldproof}% |
|
281 |
% |
|
282 |
\isadelimproof |
|
283 |
% |
|
284 |
\endisadelimproof |
|
285 |
% |
|
286 |
\begin{isamarkuptext}% |
|
40406 | 287 |
\noindent The \isa{lexicographic{\isaliteral{5F}{\isacharunderscore}}order} method fails on this example, because none of the |
23805 | 288 |
arguments decreases in the recursive call, with respect to the standard size ordering. |
289 |
To prove termination manually, we must provide a custom wellfounded relation. |
|
21212 | 290 |
|
291 |
The termination argument for \isa{sum} is based on the fact that |
|
292 |
the \emph{difference} between \isa{i} and \isa{N} gets |
|
293 |
smaller in every step, and that the recursion stops when \isa{i} |
|
23805 | 294 |
is greater than \isa{N}. Phrased differently, the expression |
40406 | 295 |
\isa{N\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}\ {\isaliteral{2D}{\isacharminus}}\ i} always decreases. |
21212 | 296 |
|
22065 | 297 |
We can use this expression as a measure function suitable to prove termination.% |
21212 | 298 |
\end{isamarkuptext}% |
299 |
\isamarkuptrue% |
|
300 |
\isacommand{termination}\isamarkupfalse% |
|
27026 | 301 |
\ sum\isanewline |
21212 | 302 |
% |
303 |
\isadelimproof |
|
22065 | 304 |
% |
21212 | 305 |
\endisadelimproof |
306 |
% |
|
307 |
\isatagproof |
|
23188 | 308 |
\isacommand{apply}\isamarkupfalse% |
40406 | 309 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}measure\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}{\isaliteral{28}{\isacharparenleft}}i{\isaliteral{2C}{\isacharcomma}}N{\isaliteral{29}{\isacharparenright}}{\isaliteral{2E}{\isachardot}}\ N\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}\ {\isaliteral{2D}{\isacharminus}}\ i{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}% |
23188 | 310 |
\begin{isamarkuptxt}% |
311 |
The \cmd{termination} command sets up the termination goal for the |
|
312 |
specified function \isa{sum}. If the function name is omitted, it |
|
313 |
implicitly refers to the last function definition. |
|
314 |
||
315 |
The \isa{relation} method takes a relation of |
|
40406 | 316 |
type \isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\ {\isaliteral{5C3C74696D65733E}{\isasymtimes}}\ {\isaliteral{27}{\isacharprime}}a{\isaliteral{29}{\isacharparenright}}\ set}, where \isa{{\isaliteral{27}{\isacharprime}}a} is the argument type of |
23188 | 317 |
the function. If the function has multiple curried arguments, then |
318 |
these are packed together into a tuple, as it happened in the above |
|
319 |
example. |
|
320 |
||
40406 | 321 |
The predefined function \isa{{\isaliteral{22}{\isachardoublequote}}measure\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\ {\isaliteral{5C3C74696D65733E}{\isasymtimes}}\ {\isaliteral{27}{\isacharprime}}a{\isaliteral{29}{\isacharparenright}}\ set{\isaliteral{22}{\isachardoublequote}}} constructs a |
23188 | 322 |
wellfounded relation from a mapping into the natural numbers (a |
323 |
\emph{measure function}). |
|
324 |
||
325 |
After the invocation of \isa{relation}, we must prove that (a) |
|
326 |
the relation we supplied is wellfounded, and (b) that the arguments |
|
327 |
of recursive calls indeed decrease with respect to the |
|
328 |
relation: |
|
329 |
||
330 |
\begin{isabelle}% |
|
40406 | 331 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ wf\ {\isaliteral{28}{\isacharparenleft}}measure\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}{\isaliteral{28}{\isacharparenleft}}i{\isaliteral{2C}{\isacharcomma}}\ N{\isaliteral{29}{\isacharparenright}}{\isaliteral{2E}{\isachardot}}\ N\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}\ {\isaliteral{2D}{\isacharminus}}\ i{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\isanewline |
332 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}i\ N{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6E6F743E}{\isasymnot}}\ N\ {\isaliteral{3C}{\isacharless}}\ i\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{28}{\isacharparenleft}}Suc\ i{\isaliteral{2C}{\isacharcomma}}\ N{\isaliteral{29}{\isacharparenright}}{\isaliteral{2C}{\isacharcomma}}\ i{\isaliteral{2C}{\isacharcomma}}\ N{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C696E3E}{\isasymin}}\ measure\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}{\isaliteral{28}{\isacharparenleft}}i{\isaliteral{2C}{\isacharcomma}}\ N{\isaliteral{29}{\isacharparenright}}{\isaliteral{2E}{\isachardot}}\ N\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}\ {\isaliteral{2D}{\isacharminus}}\ i{\isaliteral{29}{\isacharparenright}}% |
|
23188 | 333 |
\end{isabelle} |
334 |
||
335 |
These goals are all solved by \isa{auto}:% |
|
336 |
\end{isamarkuptxt}% |
|
337 |
\isamarkuptrue% |
|
338 |
\isacommand{apply}\isamarkupfalse% |
|
339 |
\ auto\isanewline |
|
340 |
\isacommand{done}\isamarkupfalse% |
|
341 |
% |
|
21212 | 342 |
\endisatagproof |
343 |
{\isafoldproof}% |
|
344 |
% |
|
345 |
\isadelimproof |
|
346 |
% |
|
347 |
\endisadelimproof |
|
348 |
% |
|
349 |
\begin{isamarkuptext}% |
|
23188 | 350 |
Let us complicate the function a little, by adding some more |
22065 | 351 |
recursive calls:% |
21212 | 352 |
\end{isamarkuptext}% |
353 |
\isamarkuptrue% |
|
354 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 355 |
\ foo\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
21212 | 356 |
\isakeyword{where}\isanewline |
40406 | 357 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}foo\ i\ N\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ i\ {\isaliteral{3E}{\isachargreater}}\ N\ \isanewline |
358 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ then\ {\isaliteral{28}{\isacharparenleft}}if\ N\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ then\ {\isadigit{0}}\ else\ foo\ {\isadigit{0}}\ {\isaliteral{28}{\isacharparenleft}}N\ {\isaliteral{2D}{\isacharminus}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\isanewline |
|
359 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ else\ i\ {\isaliteral{2B}{\isacharplus}}\ foo\ {\isaliteral{28}{\isacharparenleft}}Suc\ i{\isaliteral{29}{\isacharparenright}}\ N{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
21212 | 360 |
% |
361 |
\isadelimproof |
|
362 |
% |
|
363 |
\endisadelimproof |
|
364 |
% |
|
365 |
\isatagproof |
|
366 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 367 |
\ pat{\isaliteral{5F}{\isacharunderscore}}completeness\ auto% |
21212 | 368 |
\endisatagproof |
369 |
{\isafoldproof}% |
|
370 |
% |
|
371 |
\isadelimproof |
|
372 |
% |
|
373 |
\endisadelimproof |
|
374 |
% |
|
375 |
\begin{isamarkuptext}% |
|
376 |
When \isa{i} has reached \isa{N}, it starts at zero again |
|
377 |
and \isa{N} is decremented. |
|
378 |
This corresponds to a nested |
|
379 |
loop where one index counts up and the other down. Termination can |
|
380 |
be proved using a lexicographic combination of two measures, namely |
|
22065 | 381 |
the value of \isa{N} and the above difference. The \isa{measures} combinator generalizes \isa{measure} by taking a |
382 |
list of measure functions.% |
|
21212 | 383 |
\end{isamarkuptext}% |
384 |
\isamarkuptrue% |
|
385 |
\isacommand{termination}\isamarkupfalse% |
|
386 |
\ \isanewline |
|
387 |
% |
|
388 |
\isadelimproof |
|
22065 | 389 |
% |
21212 | 390 |
\endisadelimproof |
391 |
% |
|
392 |
\isatagproof |
|
393 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 394 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}measures\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}{\isaliteral{28}{\isacharparenleft}}i{\isaliteral{2C}{\isacharcomma}}\ N{\isaliteral{29}{\isacharparenright}}{\isaliteral{2E}{\isachardot}}\ N{\isaliteral{2C}{\isacharcomma}}\ {\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}{\isaliteral{28}{\isacharparenleft}}i{\isaliteral{2C}{\isacharcomma}}N{\isaliteral{29}{\isacharparenright}}{\isaliteral{2E}{\isachardot}}\ N\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}\ {\isaliteral{2D}{\isacharminus}}\ i{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}\ auto% |
21212 | 395 |
\endisatagproof |
396 |
{\isafoldproof}% |
|
397 |
% |
|
398 |
\isadelimproof |
|
399 |
% |
|
400 |
\endisadelimproof |
|
401 |
% |
|
40406 | 402 |
\isamarkupsubsection{How \isa{lexicographic{\isaliteral{5F}{\isacharunderscore}}order} works% |
23003 | 403 |
} |
404 |
\isamarkuptrue% |
|
405 |
% |
|
406 |
\begin{isamarkuptext}% |
|
23188 | 407 |
To see how the automatic termination proofs work, let's look at an |
408 |
example where it fails\footnote{For a detailed discussion of the |
|
409 |
termination prover, see \cite{bulwahnKN07}}: |
|
410 |
||
411 |
\end{isamarkuptext} |
|
40406 | 412 |
\cmd{fun} \isa{fails\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequote}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequote}}}\\% |
23188 | 413 |
\cmd{where}\\% |
40406 | 414 |
\hspace*{2ex}\isa{{\isaliteral{22}{\isachardoublequote}}fails\ a\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ a{\isaliteral{22}{\isachardoublequote}}}\\% |
415 |
|\hspace*{1.5ex}\isa{{\isaliteral{22}{\isachardoublequote}}fails\ a\ {\isaliteral{28}{\isacharparenleft}}x{\isaliteral{23}{\isacharhash}}xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ fails\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{2B}{\isacharplus}}\ a{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}x{\isaliteral{23}{\isacharhash}}xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequote}}}\\ |
|
23188 | 416 |
\begin{isamarkuptext} |
417 |
||
418 |
\noindent Isabelle responds with the following error: |
|
419 |
||
420 |
\begin{isabelle} |
|
23805 | 421 |
*** Unfinished subgoals:\newline |
422 |
*** (a, 1, <):\newline |
|
40406 | 423 |
*** \ 1.~\isa{{\isaliteral{5C3C416E643E}{\isasymAnd}}x{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}}\newline |
23805 | 424 |
*** (a, 1, <=):\newline |
425 |
*** \ 1.~False\newline |
|
426 |
*** (a, 2, <):\newline |
|
427 |
*** \ 1.~False\newline |
|
23188 | 428 |
*** Calls:\newline |
40406 | 429 |
*** a) \isa{{\isaliteral{28}{\isacharparenleft}}a{\isaliteral{2C}{\isacharcomma}}\ x\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{2D}{\isacharminus}}{\isaliteral{2D}{\isacharminus}}{\isaliteral{3E}{\isachargreater}}{\isaliteral{3E}{\isachargreater}}\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{2B}{\isacharplus}}\ a{\isaliteral{2C}{\isacharcomma}}\ x\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}}\newline |
23188 | 430 |
*** Measures:\newline |
40406 | 431 |
*** 1) \isa{{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}x{\isaliteral{2E}{\isachardot}}\ size\ {\isaliteral{28}{\isacharparenleft}}fst\ x{\isaliteral{29}{\isacharparenright}}}\newline |
432 |
*** 2) \isa{{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}x{\isaliteral{2E}{\isachardot}}\ size\ {\isaliteral{28}{\isacharparenleft}}snd\ x{\isaliteral{29}{\isacharparenright}}}\newline |
|
23805 | 433 |
*** Result matrix:\newline |
434 |
*** \ \ \ \ 1\ \ 2 \newline |
|
435 |
*** a: ? <= \newline |
|
436 |
*** Could not find lexicographic termination order.\newline |
|
23188 | 437 |
*** At command "fun".\newline |
438 |
\end{isabelle}% |
|
23003 | 439 |
\end{isamarkuptext}% |
440 |
\isamarkuptrue% |
|
441 |
% |
|
442 |
\begin{isamarkuptext}% |
|
29297 | 443 |
The key to this error message is the matrix at the bottom. The rows |
23188 | 444 |
of that matrix correspond to the different recursive calls (In our |
445 |
case, there is just one). The columns are the function's arguments |
|
446 |
(expressed through different measure functions, which map the |
|
447 |
argument tuple to a natural number). |
|
448 |
||
449 |
The contents of the matrix summarize what is known about argument |
|
40406 | 450 |
descents: The second argument has a weak descent (\isa{{\isaliteral{3C}{\isacharless}}{\isaliteral{3D}{\isacharequal}}}) at the |
23188 | 451 |
recursive call, and for the first argument nothing could be proved, |
40406 | 452 |
which is expressed by \isa{{\isaliteral{3F}{\isacharquery}}}. In general, there are the values |
453 |
\isa{{\isaliteral{3C}{\isacharless}}}, \isa{{\isaliteral{3C}{\isacharless}}{\isaliteral{3D}{\isacharequal}}} and \isa{{\isaliteral{3F}{\isacharquery}}}. |
|
23188 | 454 |
|
455 |
For the failed proof attempts, the unfinished subgoals are also |
|
33856 | 456 |
printed. Looking at these will often point to a missing lemma.% |
457 |
\end{isamarkuptext}% |
|
458 |
\isamarkuptrue% |
|
459 |
% |
|
40406 | 460 |
\isamarkupsubsection{The \isa{size{\isaliteral{5F}{\isacharunderscore}}change} method% |
33856 | 461 |
} |
462 |
\isamarkuptrue% |
|
463 |
% |
|
464 |
\begin{isamarkuptext}% |
|
465 |
Some termination goals that are beyond the powers of |
|
40406 | 466 |
\isa{lexicographic{\isaliteral{5F}{\isacharunderscore}}order} can be solved automatically by the |
467 |
more powerful \isa{size{\isaliteral{5F}{\isacharunderscore}}change} method, which uses a variant of |
|
33856 | 468 |
the size-change principle, together with some other |
469 |
techniques. While the details are discussed |
|
470 |
elsewhere\cite{krauss_phd}, |
|
471 |
here are a few typical situations where |
|
40406 | 472 |
\isa{lexicographic{\isaliteral{5F}{\isacharunderscore}}order} has difficulties and \isa{size{\isaliteral{5F}{\isacharunderscore}}change} |
33856 | 473 |
may be worth a try: |
474 |
\begin{itemize} |
|
475 |
\item Arguments are permuted in a recursive call. |
|
476 |
\item Several mutually recursive functions with multiple arguments. |
|
477 |
\item Unusual control flow (e.g., when some recursive calls cannot |
|
478 |
occur in sequence). |
|
479 |
\end{itemize} |
|
23188 | 480 |
|
40406 | 481 |
Loading the theory \isa{Multiset} makes the \isa{size{\isaliteral{5F}{\isacharunderscore}}change} |
33856 | 482 |
method a bit stronger: it can then use multiset orders internally.% |
23003 | 483 |
\end{isamarkuptext}% |
484 |
\isamarkuptrue% |
|
485 |
% |
|
21212 | 486 |
\isamarkupsection{Mutual Recursion% |
487 |
} |
|
488 |
\isamarkuptrue% |
|
489 |
% |
|
490 |
\begin{isamarkuptext}% |
|
491 |
If two or more functions call one another mutually, they have to be defined |
|
23188 | 492 |
in one step. Here are \isa{even} and \isa{odd}:% |
21212 | 493 |
\end{isamarkuptext}% |
494 |
\isamarkuptrue% |
|
495 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 496 |
\ even\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
497 |
\ \ \ \ \isakeyword{and}\ odd\ \ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
21212 | 498 |
\isakeyword{where}\isanewline |
40406 | 499 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}even\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ True{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
500 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}odd\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ False{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
501 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}even\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ odd\ n{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
502 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}odd\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ even\ n{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
21212 | 503 |
% |
504 |
\isadelimproof |
|
22065 | 505 |
% |
21212 | 506 |
\endisadelimproof |
507 |
% |
|
508 |
\isatagproof |
|
509 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 510 |
\ pat{\isaliteral{5F}{\isacharunderscore}}completeness\ auto% |
21212 | 511 |
\endisatagproof |
512 |
{\isafoldproof}% |
|
513 |
% |
|
514 |
\isadelimproof |
|
515 |
% |
|
516 |
\endisadelimproof |
|
517 |
% |
|
518 |
\begin{isamarkuptext}% |
|
23188 | 519 |
To eliminate the mutual dependencies, Isabelle internally |
21212 | 520 |
creates a single function operating on the sum |
40406 | 521 |
type \isa{nat\ {\isaliteral{2B}{\isacharplus}}\ nat}. Then, \isa{even} and \isa{odd} are |
23188 | 522 |
defined as projections. Consequently, termination has to be proved |
21212 | 523 |
simultaneously for both functions, by specifying a measure on the |
524 |
sum type:% |
|
525 |
\end{isamarkuptext}% |
|
526 |
\isamarkuptrue% |
|
527 |
\isacommand{termination}\isamarkupfalse% |
|
528 |
\ \isanewline |
|
529 |
% |
|
530 |
\isadelimproof |
|
22065 | 531 |
% |
21212 | 532 |
\endisadelimproof |
533 |
% |
|
534 |
\isatagproof |
|
535 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 536 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}measure\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}x{\isaliteral{2E}{\isachardot}}\ case\ x\ of\ Inl\ n\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ n\ {\isaliteral{7C}{\isacharbar}}\ Inr\ n\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}\ auto% |
22065 | 537 |
\endisatagproof |
538 |
{\isafoldproof}% |
|
539 |
% |
|
540 |
\isadelimproof |
|
541 |
% |
|
542 |
\endisadelimproof |
|
543 |
% |
|
23188 | 544 |
\begin{isamarkuptext}% |
40406 | 545 |
We could also have used \isa{lexicographic{\isaliteral{5F}{\isacharunderscore}}order}, which |
23188 | 546 |
supports mutual recursive termination proofs to a certain extent.% |
547 |
\end{isamarkuptext}% |
|
548 |
\isamarkuptrue% |
|
549 |
% |
|
22065 | 550 |
\isamarkupsubsection{Induction for mutual recursion% |
551 |
} |
|
552 |
\isamarkuptrue% |
|
553 |
% |
|
554 |
\begin{isamarkuptext}% |
|
555 |
When functions are mutually recursive, proving properties about them |
|
40406 | 556 |
generally requires simultaneous induction. The induction rule \isa{even{\isaliteral{5F}{\isacharunderscore}}odd{\isaliteral{2E}{\isachardot}}induct} |
23188 | 557 |
generated from the above definition reflects this. |
22065 | 558 |
|
559 |
Let us prove something about \isa{even} and \isa{odd}:% |
|
560 |
\end{isamarkuptext}% |
|
561 |
\isamarkuptrue% |
|
562 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 563 |
\ even{\isaliteral{5F}{\isacharunderscore}}odd{\isaliteral{5F}{\isacharunderscore}}mod{\isadigit{2}}{\isaliteral{3A}{\isacharcolon}}\isanewline |
564 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}even\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
565 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}odd\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
22065 | 566 |
\isadelimproof |
567 |
% |
|
568 |
\endisadelimproof |
|
569 |
% |
|
570 |
\isatagproof |
|
571 |
% |
|
572 |
\begin{isamarkuptxt}% |
|
573 |
We apply simultaneous induction, specifying the induction variable |
|
574 |
for both goals, separated by \cmd{and}:% |
|
575 |
\end{isamarkuptxt}% |
|
576 |
\isamarkuptrue% |
|
577 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 578 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ n\ \isakeyword{and}\ n\ rule{\isaliteral{3A}{\isacharcolon}}\ even{\isaliteral{5F}{\isacharunderscore}}odd{\isaliteral{2E}{\isachardot}}induct{\isaliteral{29}{\isacharparenright}}% |
22065 | 579 |
\begin{isamarkuptxt}% |
580 |
We get four subgoals, which correspond to the clauses in the |
|
581 |
definition of \isa{even} and \isa{odd}: |
|
582 |
\begin{isabelle}% |
|
40406 | 583 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ even\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\isanewline |
584 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ odd\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\isanewline |
|
585 |
\ {\isadigit{3}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ odd\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ even\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\isanewline |
|
586 |
\ {\isadigit{4}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ even\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ odd\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}% |
|
22065 | 587 |
\end{isabelle} |
588 |
Simplification solves the first two goals, leaving us with two |
|
589 |
statements about the \isa{mod} operation to prove:% |
|
590 |
\end{isamarkuptxt}% |
|
591 |
\isamarkuptrue% |
|
592 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 593 |
\ simp{\isaliteral{5F}{\isacharunderscore}}all% |
22065 | 594 |
\begin{isamarkuptxt}% |
595 |
\begin{isabelle}% |
|
40406 | 596 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ odd\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\isanewline |
597 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ even\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}% |
|
22065 | 598 |
\end{isabelle} |
599 |
||
23805 | 600 |
\noindent These can be handled by Isabelle's arithmetic decision procedures.% |
22065 | 601 |
\end{isamarkuptxt}% |
602 |
\isamarkuptrue% |
|
603 |
\isacommand{apply}\isamarkupfalse% |
|
23805 | 604 |
\ arith\isanewline |
22065 | 605 |
\isacommand{apply}\isamarkupfalse% |
23805 | 606 |
\ arith\isanewline |
22065 | 607 |
\isacommand{done}\isamarkupfalse% |
608 |
% |
|
609 |
\endisatagproof |
|
610 |
{\isafoldproof}% |
|
611 |
% |
|
612 |
\isadelimproof |
|
613 |
% |
|
614 |
\endisadelimproof |
|
615 |
% |
|
616 |
\begin{isamarkuptext}% |
|
23188 | 617 |
In proofs like this, the simultaneous induction is really essential: |
618 |
Even if we are just interested in one of the results, the other |
|
619 |
one is necessary to strengthen the induction hypothesis. If we leave |
|
27026 | 620 |
out the statement about \isa{odd} and just write \isa{True} instead, |
621 |
the same proof fails:% |
|
22065 | 622 |
\end{isamarkuptext}% |
623 |
\isamarkuptrue% |
|
624 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 625 |
\ failed{\isaliteral{5F}{\isacharunderscore}}attempt{\isaliteral{3A}{\isacharcolon}}\isanewline |
626 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}even\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
627 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}True{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
22065 | 628 |
% |
629 |
\isadelimproof |
|
630 |
% |
|
631 |
\endisadelimproof |
|
632 |
% |
|
633 |
\isatagproof |
|
634 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 635 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ n\ rule{\isaliteral{3A}{\isacharcolon}}\ even{\isaliteral{5F}{\isacharunderscore}}odd{\isaliteral{2E}{\isachardot}}induct{\isaliteral{29}{\isacharparenright}}% |
22065 | 636 |
\begin{isamarkuptxt}% |
637 |
\noindent Now the third subgoal is a dead end, since we have no |
|
23188 | 638 |
useful induction hypothesis available: |
22065 | 639 |
|
640 |
\begin{isabelle}% |
|
40406 | 641 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ even\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\isanewline |
642 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ True\isanewline |
|
643 |
\ {\isadigit{3}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ True\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ even\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\isanewline |
|
644 |
\ {\isadigit{4}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ even\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ mod\ {\isadigit{2}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ True% |
|
22065 | 645 |
\end{isabelle}% |
646 |
\end{isamarkuptxt}% |
|
647 |
\isamarkuptrue% |
|
648 |
\isacommand{oops}\isamarkupfalse% |
|
649 |
% |
|
21212 | 650 |
\endisatagproof |
651 |
{\isafoldproof}% |
|
652 |
% |
|
653 |
\isadelimproof |
|
654 |
% |
|
655 |
\endisadelimproof |
|
656 |
% |
|
23188 | 657 |
\isamarkupsection{General pattern matching% |
22065 | 658 |
} |
659 |
\isamarkuptrue% |
|
660 |
% |
|
23805 | 661 |
\begin{isamarkuptext}% |
662 |
\label{genpats}% |
|
663 |
\end{isamarkuptext}% |
|
664 |
\isamarkuptrue% |
|
665 |
% |
|
23188 | 666 |
\isamarkupsubsection{Avoiding automatic pattern splitting% |
22065 | 667 |
} |
668 |
\isamarkuptrue% |
|
669 |
% |
|
670 |
\begin{isamarkuptext}% |
|
671 |
Up to now, we used pattern matching only on datatypes, and the |
|
672 |
patterns were always disjoint and complete, and if they weren't, |
|
673 |
they were made disjoint automatically like in the definition of |
|
674 |
\isa{sep} in \S\ref{patmatch}. |
|
675 |
||
23188 | 676 |
This automatic splitting can significantly increase the number of |
677 |
equations involved, and this is not always desirable. The following |
|
678 |
example shows the problem: |
|
22065 | 679 |
|
23805 | 680 |
Suppose we are modeling incomplete knowledge about the world by a |
23003 | 681 |
three-valued datatype, which has values \isa{T}, \isa{F} |
682 |
and \isa{X} for true, false and uncertain propositions, respectively.% |
|
22065 | 683 |
\end{isamarkuptext}% |
684 |
\isamarkuptrue% |
|
685 |
\isacommand{datatype}\isamarkupfalse% |
|
40406 | 686 |
\ P{\isadigit{3}}\ {\isaliteral{3D}{\isacharequal}}\ T\ {\isaliteral{7C}{\isacharbar}}\ F\ {\isaliteral{7C}{\isacharbar}}\ X% |
22065 | 687 |
\begin{isamarkuptext}% |
23188 | 688 |
\noindent Then the conjunction of such values can be defined as follows:% |
22065 | 689 |
\end{isamarkuptext}% |
690 |
\isamarkuptrue% |
|
691 |
\isacommand{fun}\isamarkupfalse% |
|
40406 | 692 |
\ And\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}P{\isadigit{3}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ P{\isadigit{3}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ P{\isadigit{3}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
22065 | 693 |
\isakeyword{where}\isanewline |
40406 | 694 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}And\ T\ p\ {\isaliteral{3D}{\isacharequal}}\ p{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
695 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And\ p\ T\ {\isaliteral{3D}{\isacharequal}}\ p{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
696 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And\ p\ F\ {\isaliteral{3D}{\isacharequal}}\ F{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
697 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And\ F\ p\ {\isaliteral{3D}{\isacharequal}}\ F{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
698 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And\ X\ X\ {\isaliteral{3D}{\isacharequal}}\ X{\isaliteral{22}{\isachardoublequoteclose}}% |
|
22065 | 699 |
\begin{isamarkuptext}% |
700 |
This definition is useful, because the equations can directly be used |
|
29297 | 701 |
as simplification rules. But the patterns overlap: For example, |
23188 | 702 |
the expression \isa{And\ T\ T} is matched by both the first and |
703 |
the second equation. By default, Isabelle makes the patterns disjoint by |
|
22065 | 704 |
splitting them up, producing instances:% |
705 |
\end{isamarkuptext}% |
|
706 |
\isamarkuptrue% |
|
707 |
\isacommand{thm}\isamarkupfalse% |
|
40406 | 708 |
\ And{\isaliteral{2E}{\isachardot}}simps% |
22065 | 709 |
\begin{isamarkuptext}% |
40406 | 710 |
\isa{And\ T\ {\isaliteral{3F}{\isacharquery}}p\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}p\isasep\isanewline% |
711 |
And\ F\ T\ {\isaliteral{3D}{\isacharequal}}\ F\isasep\isanewline% |
|
712 |
And\ X\ T\ {\isaliteral{3D}{\isacharequal}}\ X\isasep\isanewline% |
|
713 |
And\ F\ F\ {\isaliteral{3D}{\isacharequal}}\ F\isasep\isanewline% |
|
714 |
And\ X\ F\ {\isaliteral{3D}{\isacharequal}}\ F\isasep\isanewline% |
|
715 |
And\ F\ X\ {\isaliteral{3D}{\isacharequal}}\ F\isasep\isanewline% |
|
716 |
And\ X\ X\ {\isaliteral{3D}{\isacharequal}}\ X} |
|
22065 | 717 |
|
718 |
\vspace*{1em} |
|
23003 | 719 |
\noindent There are several problems with this: |
22065 | 720 |
|
721 |
\begin{enumerate} |
|
23188 | 722 |
\item If the datatype has many constructors, there can be an |
22065 | 723 |
explosion of equations. For \isa{And}, we get seven instead of |
23003 | 724 |
five equations, which can be tolerated, but this is just a small |
22065 | 725 |
example. |
726 |
||
23188 | 727 |
\item Since splitting makes the equations \qt{less general}, they |
22065 | 728 |
do not always match in rewriting. While the term \isa{And\ x\ F} |
23188 | 729 |
can be simplified to \isa{F} with the original equations, a |
22065 | 730 |
(manual) case split on \isa{x} is now necessary. |
731 |
||
40406 | 732 |
\item The splitting also concerns the induction rule \isa{And{\isaliteral{2E}{\isachardot}}induct}. Instead of five premises it now has seven, which |
22065 | 733 |
means that our induction proofs will have more cases. |
734 |
||
735 |
\item In general, it increases clarity if we get the same definition |
|
736 |
back which we put in. |
|
737 |
\end{enumerate} |
|
738 |
||
23188 | 739 |
If we do not want the automatic splitting, we can switch it off by |
740 |
leaving out the \cmd{sequential} option. However, we will have to |
|
741 |
prove that our pattern matching is consistent\footnote{This prevents |
|
40406 | 742 |
us from defining something like \isa{f\ x\ {\isaliteral{3D}{\isacharequal}}\ True} and \isa{f\ x\ {\isaliteral{3D}{\isacharequal}}\ False} simultaneously.}:% |
22065 | 743 |
\end{isamarkuptext}% |
744 |
\isamarkuptrue% |
|
745 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 746 |
\ And{\isadigit{2}}\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}P{\isadigit{3}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ P{\isadigit{3}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ P{\isadigit{3}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
22065 | 747 |
\isakeyword{where}\isanewline |
40406 | 748 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}And{\isadigit{2}}\ T\ p\ {\isaliteral{3D}{\isacharequal}}\ p{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
749 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And{\isadigit{2}}\ p\ T\ {\isaliteral{3D}{\isacharequal}}\ p{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
750 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And{\isadigit{2}}\ p\ F\ {\isaliteral{3D}{\isacharequal}}\ F{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
751 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And{\isadigit{2}}\ F\ p\ {\isaliteral{3D}{\isacharequal}}\ F{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
752 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}And{\isadigit{2}}\ X\ X\ {\isaliteral{3D}{\isacharequal}}\ X{\isaliteral{22}{\isachardoublequoteclose}}% |
|
22065 | 753 |
\isadelimproof |
754 |
% |
|
755 |
\endisadelimproof |
|
756 |
% |
|
757 |
\isatagproof |
|
758 |
% |
|
759 |
\begin{isamarkuptxt}% |
|
23188 | 760 |
\noindent Now let's look at the proof obligations generated by a |
22065 | 761 |
function definition. In this case, they are: |
762 |
||
763 |
\begin{isabelle}% |
|
40406 | 764 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}P\ x{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}{\isaliteral{5C3C416E643E}{\isasymAnd}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ F{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{3B}{\isacharsemicolon}}\isanewline |
765 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}P\ x{\isaliteral{2E}{\isachardot}}\ \ }{\isaliteral{5C3C416E643E}{\isasymAnd}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}F{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{3B}{\isacharsemicolon}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}X{\isaliteral{2C}{\isacharcomma}}\ X{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\isanewline |
|
766 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}P\ x{\isaliteral{2E}{\isachardot}}\ }{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\isanewline |
|
767 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p\ pa{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ pa{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ pa\isanewline |
|
768 |
\ {\isadigit{3}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p\ pa{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}pa{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ pa\isanewline |
|
769 |
\ {\isadigit{4}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p\ pa{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}pa{\isaliteral{2C}{\isacharcomma}}\ F{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ F\isanewline |
|
770 |
\ {\isadigit{5}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p\ pa{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}F{\isaliteral{2C}{\isacharcomma}}\ pa{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ F\isanewline |
|
771 |
\ {\isadigit{6}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}X{\isaliteral{2C}{\isacharcomma}}\ X{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ X\isanewline |
|
772 |
\ {\isadigit{7}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p\ pa{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}pa{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ pa\isanewline |
|
773 |
\ {\isadigit{8}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p\ pa{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}pa{\isaliteral{2C}{\isacharcomma}}\ F{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ F\isanewline |
|
774 |
\ {\isadigit{9}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p\ pa{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}F{\isaliteral{2C}{\isacharcomma}}\ pa{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ F\isanewline |
|
775 |
\ {\isadigit{1}}{\isadigit{0}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}p{\isaliteral{2E}{\isachardot}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}X{\isaliteral{2C}{\isacharcomma}}\ X{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ p\ {\isaliteral{3D}{\isacharequal}}\ X% |
|
23188 | 776 |
\end{isabelle}\vspace{-1.2em}\hspace{3cm}\vdots\vspace{1.2em} |
22065 | 777 |
|
778 |
The first subgoal expresses the completeness of the patterns. It has |
|
779 |
the form of an elimination rule and states that every \isa{x} of |
|
23188 | 780 |
the function's input type must match at least one of the patterns\footnote{Completeness could |
22065 | 781 |
be equivalently stated as a disjunction of existential statements: |
40406 | 782 |
\isa{{\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6578697374733E}{\isasymexists}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}T{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6578697374733E}{\isasymexists}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ T{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6578697374733E}{\isasymexists}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}p{\isaliteral{2C}{\isacharcomma}}\ F{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6578697374733E}{\isasymexists}}p{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}F{\isaliteral{2C}{\isacharcomma}}\ p{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}X{\isaliteral{2C}{\isacharcomma}}\ X{\isaliteral{29}{\isacharparenright}}}, and you can use the method \isa{atomize{\isaliteral{5F}{\isacharunderscore}}elim} to get that form instead.}. If the patterns just involve |
783 |
datatypes, we can solve it with the \isa{pat{\isaliteral{5F}{\isacharunderscore}}completeness} |
|
23188 | 784 |
method:% |
22065 | 785 |
\end{isamarkuptxt}% |
786 |
\isamarkuptrue% |
|
787 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 788 |
\ pat{\isaliteral{5F}{\isacharunderscore}}completeness% |
22065 | 789 |
\begin{isamarkuptxt}% |
790 |
The remaining subgoals express \emph{pattern compatibility}. We do |
|
23188 | 791 |
allow that an input value matches multiple patterns, but in this |
22065 | 792 |
case, the result (i.e.~the right hand sides of the equations) must |
793 |
also be equal. For each pair of two patterns, there is one such |
|
794 |
subgoal. Usually this needs injectivity of the constructors, which |
|
795 |
is used automatically by \isa{auto}.% |
|
796 |
\end{isamarkuptxt}% |
|
797 |
\isamarkuptrue% |
|
798 |
\isacommand{by}\isamarkupfalse% |
|
799 |
\ auto% |
|
800 |
\endisatagproof |
|
801 |
{\isafoldproof}% |
|
802 |
% |
|
803 |
\isadelimproof |
|
804 |
% |
|
805 |
\endisadelimproof |
|
43042
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
806 |
\isanewline |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
807 |
\isacommand{termination}\isamarkupfalse% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
808 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
809 |
\isadelimproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
810 |
\ % |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
811 |
\endisadelimproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
812 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
813 |
\isatagproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
814 |
\isacommand{by}\isamarkupfalse% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
815 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{7B}{\isacharbraceleft}}{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}\ simp% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
816 |
\endisatagproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
817 |
{\isafoldproof}% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
818 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
819 |
\isadelimproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
820 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
821 |
\endisadelimproof |
22065 | 822 |
% |
823 |
\isamarkupsubsection{Non-constructor patterns% |
|
21212 | 824 |
} |
825 |
\isamarkuptrue% |
|
826 |
% |
|
827 |
\begin{isamarkuptext}% |
|
23805 | 828 |
Most of Isabelle's basic types take the form of inductive datatypes, |
829 |
and usually pattern matching works on the constructors of such types. |
|
830 |
However, this need not be always the case, and the \cmd{function} |
|
831 |
command handles other kind of patterns, too. |
|
23188 | 832 |
|
23805 | 833 |
One well-known instance of non-constructor patterns are |
23188 | 834 |
so-called \emph{$n+k$-patterns}, which are a little controversial in |
835 |
the functional programming world. Here is the initial fibonacci |
|
836 |
example with $n+k$-patterns:% |
|
21212 | 837 |
\end{isamarkuptext}% |
838 |
\isamarkuptrue% |
|
23188 | 839 |
\isacommand{function}\isamarkupfalse% |
40406 | 840 |
\ fib{\isadigit{2}}\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23188 | 841 |
\isakeyword{where}\isanewline |
40406 | 842 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}fib{\isadigit{2}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
843 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}fib{\isadigit{2}}\ {\isadigit{1}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
844 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}fib{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{2}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ fib{\isadigit{2}}\ n\ {\isaliteral{2B}{\isacharplus}}\ fib{\isadigit{2}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
23188 | 845 |
\isadelimproof |
846 |
% |
|
847 |
\endisadelimproof |
|
848 |
% |
|
849 |
\isatagproof |
|
850 |
% |
|
851 |
\begin{isamarkuptxt}% |
|
23805 | 852 |
This kind of matching is again justified by the proof of pattern |
853 |
completeness and compatibility. |
|
854 |
The proof obligation for pattern completeness states that every natural number is |
|
40406 | 855 |
either \isa{{\isadigit{0}}}, \isa{{\isadigit{1}}} or \isa{n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{2}}}: |
23188 | 856 |
|
857 |
\begin{isabelle}% |
|
40406 | 858 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}P\ x{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{3B}{\isacharsemicolon}}\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{1}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{3D}{\isacharequal}}\ n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{2}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P% |
23188 | 859 |
\end{isabelle} |
860 |
||
861 |
This is an arithmetic triviality, but unfortunately the |
|
862 |
\isa{arith} method cannot handle this specific form of an |
|
40406 | 863 |
elimination rule. However, we can use the method \isa{atomize{\isaliteral{5F}{\isacharunderscore}}elim} to do an ad-hoc conversion to a disjunction of |
29297 | 864 |
existentials, which can then be solved by the arithmetic decision procedure. |
23805 | 865 |
Pattern compatibility and termination are automatic as usual.% |
23188 | 866 |
\end{isamarkuptxt}% |
867 |
\isamarkuptrue% |
|
868 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 869 |
\ atomize{\isaliteral{5F}{\isacharunderscore}}elim\isanewline |
23805 | 870 |
\isacommand{apply}\isamarkupfalse% |
871 |
\ arith\isanewline |
|
23188 | 872 |
\isacommand{apply}\isamarkupfalse% |
873 |
\ auto\isanewline |
|
874 |
\isacommand{done}\isamarkupfalse% |
|
875 |
% |
|
876 |
\endisatagproof |
|
877 |
{\isafoldproof}% |
|
878 |
% |
|
879 |
\isadelimproof |
|
880 |
% |
|
881 |
\endisadelimproof |
|
882 |
\isanewline |
|
883 |
\isacommand{termination}\isamarkupfalse% |
|
884 |
% |
|
885 |
\isadelimproof |
|
886 |
\ % |
|
887 |
\endisadelimproof |
|
888 |
% |
|
889 |
\isatagproof |
|
890 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 891 |
\ lexicographic{\isaliteral{5F}{\isacharunderscore}}order% |
23188 | 892 |
\endisatagproof |
893 |
{\isafoldproof}% |
|
894 |
% |
|
895 |
\isadelimproof |
|
896 |
% |
|
897 |
\endisadelimproof |
|
898 |
% |
|
899 |
\begin{isamarkuptext}% |
|
900 |
We can stretch the notion of pattern matching even more. The |
|
901 |
following function is not a sensible functional program, but a |
|
902 |
perfectly valid mathematical definition:% |
|
903 |
\end{isamarkuptext}% |
|
904 |
\isamarkuptrue% |
|
905 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 906 |
\ ev\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23188 | 907 |
\isakeyword{where}\isanewline |
40406 | 908 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}ev\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{2}}\ {\isaliteral{2A}{\isacharasterisk}}\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ True{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
909 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}ev\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{2}}\ {\isaliteral{2A}{\isacharasterisk}}\ n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ False{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23188 | 910 |
% |
911 |
\isadelimproof |
|
912 |
% |
|
913 |
\endisadelimproof |
|
914 |
% |
|
915 |
\isatagproof |
|
23805 | 916 |
\isacommand{apply}\isamarkupfalse% |
40406 | 917 |
\ atomize{\isaliteral{5F}{\isacharunderscore}}elim\isanewline |
23188 | 918 |
\isacommand{by}\isamarkupfalse% |
40406 | 919 |
\ arith{\isaliteral{2B}{\isacharplus}}% |
23188 | 920 |
\endisatagproof |
921 |
{\isafoldproof}% |
|
922 |
% |
|
923 |
\isadelimproof |
|
924 |
\isanewline |
|
925 |
% |
|
926 |
\endisadelimproof |
|
927 |
\isacommand{termination}\isamarkupfalse% |
|
928 |
% |
|
929 |
\isadelimproof |
|
930 |
\ % |
|
931 |
\endisadelimproof |
|
932 |
% |
|
933 |
\isatagproof |
|
934 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 935 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{7B}{\isacharbraceleft}}{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}\ simp% |
23188 | 936 |
\endisatagproof |
937 |
{\isafoldproof}% |
|
938 |
% |
|
939 |
\isadelimproof |
|
940 |
% |
|
941 |
\endisadelimproof |
|
942 |
% |
|
943 |
\begin{isamarkuptext}% |
|
27026 | 944 |
This general notion of pattern matching gives you a certain freedom |
945 |
in writing down specifications. However, as always, such freedom should |
|
23188 | 946 |
be used with care: |
947 |
||
948 |
If we leave the area of constructor |
|
949 |
patterns, we have effectively departed from the world of functional |
|
950 |
programming. This means that it is no longer possible to use the |
|
951 |
code generator, and expect it to generate ML code for our |
|
952 |
definitions. Also, such a specification might not work very well together with |
|
953 |
simplification. Your mileage may vary.% |
|
954 |
\end{isamarkuptext}% |
|
955 |
\isamarkuptrue% |
|
956 |
% |
|
957 |
\isamarkupsubsection{Conditional equations% |
|
958 |
} |
|
959 |
\isamarkuptrue% |
|
960 |
% |
|
961 |
\begin{isamarkuptext}% |
|
962 |
The function package also supports conditional equations, which are |
|
963 |
similar to guards in a language like Haskell. Here is Euclid's |
|
964 |
algorithm written with conditional patterns\footnote{Note that the |
|
965 |
patterns are also overlapping in the base case}:% |
|
966 |
\end{isamarkuptext}% |
|
967 |
\isamarkuptrue% |
|
968 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 969 |
\ gcd\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23188 | 970 |
\isakeyword{where}\isanewline |
40406 | 971 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}gcd\ x\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ x{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
972 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}gcd\ {\isadigit{0}}\ y\ {\isaliteral{3D}{\isacharequal}}\ y{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
973 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{3C}{\isacharless}}\ y\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ gcd\ {\isaliteral{28}{\isacharparenleft}}Suc\ x{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ y{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ gcd\ {\isaliteral{28}{\isacharparenleft}}Suc\ x{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}y\ {\isaliteral{2D}{\isacharminus}}\ x{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
974 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{5C3C6E6F743E}{\isasymnot}}\ x\ {\isaliteral{3C}{\isacharless}}\ y\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ gcd\ {\isaliteral{28}{\isacharparenleft}}Suc\ x{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ y{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ gcd\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{2D}{\isacharminus}}\ y{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}Suc\ y{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23188 | 975 |
% |
976 |
\isadelimproof |
|
977 |
% |
|
978 |
\endisadelimproof |
|
979 |
% |
|
980 |
\isatagproof |
|
981 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 982 |
\ {\isaliteral{28}{\isacharparenleft}}atomize{\isaliteral{5F}{\isacharunderscore}}elim{\isaliteral{2C}{\isacharcomma}}\ auto{\isaliteral{2C}{\isacharcomma}}\ arith{\isaliteral{29}{\isacharparenright}}% |
23188 | 983 |
\endisatagproof |
984 |
{\isafoldproof}% |
|
985 |
% |
|
986 |
\isadelimproof |
|
987 |
\isanewline |
|
988 |
% |
|
989 |
\endisadelimproof |
|
990 |
\isacommand{termination}\isamarkupfalse% |
|
991 |
% |
|
992 |
\isadelimproof |
|
993 |
\ % |
|
994 |
\endisadelimproof |
|
995 |
% |
|
996 |
\isatagproof |
|
997 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 998 |
\ lexicographic{\isaliteral{5F}{\isacharunderscore}}order% |
23188 | 999 |
\endisatagproof |
1000 |
{\isafoldproof}% |
|
1001 |
% |
|
1002 |
\isadelimproof |
|
1003 |
% |
|
1004 |
\endisadelimproof |
|
1005 |
% |
|
1006 |
\begin{isamarkuptext}% |
|
1007 |
By now, you can probably guess what the proof obligations for the |
|
1008 |
pattern completeness and compatibility look like. |
|
1009 |
||
1010 |
Again, functions with conditional patterns are not supported by the |
|
1011 |
code generator.% |
|
1012 |
\end{isamarkuptext}% |
|
1013 |
\isamarkuptrue% |
|
1014 |
% |
|
1015 |
\isamarkupsubsection{Pattern matching on strings% |
|
1016 |
} |
|
1017 |
\isamarkuptrue% |
|
1018 |
% |
|
1019 |
\begin{isamarkuptext}% |
|
23805 | 1020 |
As strings (as lists of characters) are normal datatypes, pattern |
23188 | 1021 |
matching on them is possible, but somewhat problematic. Consider the |
1022 |
following definition: |
|
1023 |
||
1024 |
\end{isamarkuptext} |
|
40406 | 1025 |
\noindent\cmd{fun} \isa{check\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequote}}string\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequote}}}\\% |
23188 | 1026 |
\cmd{where}\\% |
40406 | 1027 |
\hspace*{2ex}\isa{{\isaliteral{22}{\isachardoublequote}}check\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}{\isaliteral{27}{\isacharprime}}good{\isaliteral{27}{\isacharprime}}{\isaliteral{27}{\isacharprime}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ True{\isaliteral{22}{\isachardoublequote}}}\\% |
1028 |
\isa{{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequote}}check\ s\ {\isaliteral{3D}{\isacharequal}}\ False{\isaliteral{22}{\isachardoublequote}}} |
|
23188 | 1029 |
\begin{isamarkuptext} |
1030 |
||
23805 | 1031 |
\noindent An invocation of the above \cmd{fun} command does not |
23188 | 1032 |
terminate. What is the problem? Strings are lists of characters, and |
23805 | 1033 |
characters are a datatype with a lot of constructors. Splitting the |
23188 | 1034 |
catch-all pattern thus leads to an explosion of cases, which cannot |
1035 |
be handled by Isabelle. |
|
1036 |
||
1037 |
There are two things we can do here. Either we write an explicit |
|
1038 |
\isa{if} on the right hand side, or we can use conditional patterns:% |
|
1039 |
\end{isamarkuptext}% |
|
1040 |
\isamarkuptrue% |
|
1041 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 1042 |
\ check\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}string\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23188 | 1043 |
\isakeyword{where}\isanewline |
40406 | 1044 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}check\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}{\isaliteral{27}{\isacharprime}}good{\isaliteral{27}{\isacharprime}}{\isaliteral{27}{\isacharprime}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ True{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
1045 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}s\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isaliteral{27}{\isacharprime}}{\isaliteral{27}{\isacharprime}}good{\isaliteral{27}{\isacharprime}}{\isaliteral{27}{\isacharprime}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ check\ s\ {\isaliteral{3D}{\isacharequal}}\ False{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23188 | 1046 |
% |
1047 |
\isadelimproof |
|
1048 |
% |
|
1049 |
\endisadelimproof |
|
1050 |
% |
|
1051 |
\isatagproof |
|
1052 |
\isacommand{by}\isamarkupfalse% |
|
1053 |
\ auto% |
|
1054 |
\endisatagproof |
|
1055 |
{\isafoldproof}% |
|
1056 |
% |
|
1057 |
\isadelimproof |
|
43042
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1058 |
\isanewline |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1059 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1060 |
\endisadelimproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1061 |
\isacommand{termination}\isamarkupfalse% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1062 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1063 |
\isadelimproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1064 |
\ % |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1065 |
\endisadelimproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1066 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1067 |
\isatagproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1068 |
\isacommand{by}\isamarkupfalse% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1069 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{7B}{\isacharbraceleft}}{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}\ simp% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1070 |
\endisatagproof |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1071 |
{\isafoldproof}% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1072 |
% |
0f9534b7ea75
function tutorial: do not omit termination proof, even when discussing other things
krauss
parents:
41848
diff
changeset
|
1073 |
\isadelimproof |
23188 | 1074 |
% |
1075 |
\endisadelimproof |
|
21212 | 1076 |
% |
22065 | 1077 |
\isamarkupsection{Partiality% |
1078 |
} |
|
1079 |
\isamarkuptrue% |
|
1080 |
% |
|
1081 |
\begin{isamarkuptext}% |
|
1082 |
In HOL, all functions are total. A function \isa{f} applied to |
|
23188 | 1083 |
\isa{x} always has the value \isa{f\ x}, and there is no notion |
22065 | 1084 |
of undefinedness. |
23188 | 1085 |
This is why we have to do termination |
1086 |
proofs when defining functions: The proof justifies that the |
|
1087 |
function can be defined by wellfounded recursion. |
|
22065 | 1088 |
|
23188 | 1089 |
However, the \cmd{function} package does support partiality to a |
1090 |
certain extent. Let's look at the following function which looks |
|
1091 |
for a zero of a given function f.% |
|
23003 | 1092 |
\end{isamarkuptext}% |
1093 |
\isamarkuptrue% |
|
1094 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 1095 |
\ findzero\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{28}{\isacharparenleft}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1096 |
\isakeyword{where}\isanewline |
40406 | 1097 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}findzero\ f\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ f\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ then\ n\ else\ findzero\ f\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1098 |
% |
1099 |
\isadelimproof |
|
1100 |
% |
|
1101 |
\endisadelimproof |
|
1102 |
% |
|
1103 |
\isatagproof |
|
1104 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 1105 |
\ pat{\isaliteral{5F}{\isacharunderscore}}completeness\ auto% |
23003 | 1106 |
\endisatagproof |
1107 |
{\isafoldproof}% |
|
1108 |
% |
|
1109 |
\isadelimproof |
|
1110 |
% |
|
1111 |
\endisadelimproof |
|
1112 |
% |
|
1113 |
\begin{isamarkuptext}% |
|
23805 | 1114 |
\noindent Clearly, any attempt of a termination proof must fail. And without |
40406 | 1115 |
that, we do not get the usual rules \isa{findzero{\isaliteral{2E}{\isachardot}}simps} and |
1116 |
\isa{findzero{\isaliteral{2E}{\isachardot}}induct}. So what was the definition good for at all?% |
|
23003 | 1117 |
\end{isamarkuptext}% |
1118 |
\isamarkuptrue% |
|
1119 |
% |
|
1120 |
\isamarkupsubsection{Domain predicates% |
|
1121 |
} |
|
1122 |
\isamarkuptrue% |
|
1123 |
% |
|
1124 |
\begin{isamarkuptext}% |
|
1125 |
The trick is that Isabelle has not only defined the function \isa{findzero}, but also |
|
40406 | 1126 |
a predicate \isa{findzero{\isaliteral{5F}{\isacharunderscore}}dom} that characterizes the values where the function |
23188 | 1127 |
terminates: the \emph{domain} of the function. If we treat a |
1128 |
partial function just as a total function with an additional domain |
|
1129 |
predicate, we can derive simplification and |
|
1130 |
induction rules as we do for total functions. They are guarded |
|
1131 |
by domain conditions and are called \isa{psimps} and \isa{pinduct}:% |
|
23003 | 1132 |
\end{isamarkuptext}% |
1133 |
\isamarkuptrue% |
|
23805 | 1134 |
% |
23003 | 1135 |
\begin{isamarkuptext}% |
23805 | 1136 |
\noindent\begin{minipage}{0.79\textwidth}\begin{isabelle}% |
40406 | 1137 |
findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}f{\isaliteral{2C}{\isacharcomma}}\ {\isaliteral{3F}{\isacharquery}}n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline |
1138 |
findzero\ {\isaliteral{3F}{\isacharquery}}f\ {\isaliteral{3F}{\isacharquery}}n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ {\isaliteral{3F}{\isacharquery}}f\ {\isaliteral{3F}{\isacharquery}}n\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ then\ {\isaliteral{3F}{\isacharquery}}n\ else\ findzero\ {\isaliteral{3F}{\isacharquery}}f\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isaliteral{3F}{\isacharquery}}n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}% |
|
23805 | 1139 |
\end{isabelle}\end{minipage} |
40406 | 1140 |
\hfill(\isa{findzero{\isaliteral{2E}{\isachardot}}psimps}) |
23805 | 1141 |
\vspace{1em} |
1142 |
||
1143 |
\noindent\begin{minipage}{0.79\textwidth}\begin{isabelle}% |
|
40406 | 1144 |
{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}a{\isadigit{0}}{\isaliteral{2E}{\isachardot}}{\isadigit{0}}{\isaliteral{2C}{\isacharcomma}}\ {\isaliteral{3F}{\isacharquery}}a{\isadigit{1}}{\isaliteral{2E}{\isachardot}}{\isadigit{0}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{3B}{\isacharsemicolon}}\isanewline |
1145 |
\isaindent{\ }{\isaliteral{5C3C416E643E}{\isasymAnd}}f\ n{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{3B}{\isacharsemicolon}}\ f\ n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}P\ f\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}P\ f\ n{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\isanewline |
|
1146 |
{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}P\ {\isaliteral{3F}{\isacharquery}}a{\isadigit{0}}{\isaliteral{2E}{\isachardot}}{\isadigit{0}}\ {\isaliteral{3F}{\isacharquery}}a{\isadigit{1}}{\isaliteral{2E}{\isachardot}}{\isadigit{0}}% |
|
23805 | 1147 |
\end{isabelle}\end{minipage} |
40406 | 1148 |
\hfill(\isa{findzero{\isaliteral{2E}{\isachardot}}pinduct})% |
23003 | 1149 |
\end{isamarkuptext}% |
1150 |
\isamarkuptrue% |
|
1151 |
% |
|
1152 |
\begin{isamarkuptext}% |
|
23188 | 1153 |
Remember that all we |
1154 |
are doing here is use some tricks to make a total function appear |
|
40406 | 1155 |
as if it was partial. We can still write the term \isa{findzero\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}x{\isaliteral{2E}{\isachardot}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isadigit{0}}} and like any other term of type \isa{nat} it is equal |
23003 | 1156 |
to some natural number, although we might not be able to find out |
23188 | 1157 |
which one. The function is \emph{underdefined}. |
23003 | 1158 |
|
23805 | 1159 |
But it is defined enough to prove something interesting about it. We |
23188 | 1160 |
can prove that if \isa{findzero\ f\ n} |
23805 | 1161 |
terminates, it indeed returns a zero of \isa{f}:% |
23003 | 1162 |
\end{isamarkuptext}% |
1163 |
\isamarkuptrue% |
|
1164 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1165 |
\ findzero{\isaliteral{5F}{\isacharunderscore}}zero{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ f\ {\isaliteral{28}{\isacharparenleft}}findzero\ f\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}% |
23003 | 1166 |
\isadelimproof |
1167 |
% |
|
1168 |
\endisadelimproof |
|
1169 |
% |
|
1170 |
\isatagproof |
|
1171 |
% |
|
1172 |
\begin{isamarkuptxt}% |
|
23805 | 1173 |
\noindent We apply induction as usual, but using the partial induction |
23003 | 1174 |
rule:% |
1175 |
\end{isamarkuptxt}% |
|
1176 |
\isamarkuptrue% |
|
1177 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 1178 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ f\ n\ rule{\isaliteral{3A}{\isacharcolon}}\ findzero{\isaliteral{2E}{\isachardot}}pinduct{\isaliteral{29}{\isacharparenright}}% |
23003 | 1179 |
\begin{isamarkuptxt}% |
23805 | 1180 |
\noindent This gives the following subgoals: |
23003 | 1181 |
|
1182 |
\begin{isabelle}% |
|
40406 | 1183 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}f\ n{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{3B}{\isacharsemicolon}}\ f\ n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ f\ {\isaliteral{28}{\isacharparenleft}}findzero\ f\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\isanewline |
1184 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}f\ n{\isaliteral{2E}{\isachardot}}\ }{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ f\ {\isaliteral{28}{\isacharparenleft}}findzero\ f\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}% |
|
23003 | 1185 |
\end{isabelle} |
1186 |
||
23805 | 1187 |
\noindent The hypothesis in our lemma was used to satisfy the first premise in |
40406 | 1188 |
the induction rule. However, we also get \isa{findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}} as a local assumption in the induction step. This |
40403 | 1189 |
allows unfolding \isa{findzero\ f\ n} using the \isa{psimps} |
1190 |
rule, and the rest is trivial.% |
|
23003 | 1191 |
\end{isamarkuptxt}% |
1192 |
\isamarkuptrue% |
|
1193 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 1194 |
\ {\isaliteral{28}{\isacharparenleft}}simp\ add{\isaliteral{3A}{\isacharcolon}}\ findzero{\isaliteral{2E}{\isachardot}}psimps{\isaliteral{29}{\isacharparenright}}\isanewline |
23003 | 1195 |
\isacommand{done}\isamarkupfalse% |
1196 |
% |
|
1197 |
\endisatagproof |
|
1198 |
{\isafoldproof}% |
|
1199 |
% |
|
1200 |
\isadelimproof |
|
1201 |
% |
|
1202 |
\endisadelimproof |
|
1203 |
% |
|
1204 |
\begin{isamarkuptext}% |
|
1205 |
Proofs about partial functions are often not harder than for total |
|
1206 |
functions. Fig.~\ref{findzero_isar} shows a slightly more |
|
1207 |
complicated proof written in Isar. It is verbose enough to show how |
|
1208 |
partiality comes into play: From the partial induction, we get an |
|
1209 |
additional domain condition hypothesis. Observe how this condition |
|
1210 |
is applied when calls to \isa{findzero} are unfolded.% |
|
1211 |
\end{isamarkuptext}% |
|
1212 |
\isamarkuptrue% |
|
1213 |
% |
|
1214 |
\begin{figure} |
|
23188 | 1215 |
\hrule\vspace{6pt} |
23003 | 1216 |
\begin{minipage}{0.8\textwidth} |
1217 |
\isabellestyle{it} |
|
1218 |
\isastyle\isamarkuptrue |
|
1219 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1220 |
\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{3B}{\isacharsemicolon}}\ x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{7B}{\isacharbraceleft}}n\ {\isaliteral{2E}{\isachardot}}{\isaliteral{2E}{\isachardot}}{\isaliteral{3C}{\isacharless}}\ findzero\ f\ n{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ f\ x\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1221 |
% |
1222 |
\isadelimproof |
|
1223 |
% |
|
1224 |
\endisadelimproof |
|
1225 |
% |
|
1226 |
\isatagproof |
|
1227 |
\isacommand{proof}\isamarkupfalse% |
|
40406 | 1228 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ rule{\isaliteral{3A}{\isacharcolon}}\ findzero{\isaliteral{2E}{\isachardot}}pinduct{\isaliteral{29}{\isacharparenright}}\isanewline |
23003 | 1229 |
\ \ \isacommand{fix}\isamarkupfalse% |
1230 |
\ f\ n\ \isacommand{assume}\isamarkupfalse% |
|
40406 | 1231 |
\ dom{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
1232 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isakeyword{and}\ IH{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}f\ n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{3B}{\isacharsemicolon}}\ x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{7B}{\isacharbraceleft}}Suc\ n\ {\isaliteral{2E}{\isachardot}}{\isaliteral{2E}{\isachardot}}{\isaliteral{3C}{\isacharless}}\ findzero\ f\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ f\ x\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
1233 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isakeyword{and}\ x{\isaliteral{5F}{\isacharunderscore}}range{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{7B}{\isacharbraceleft}}n\ {\isaliteral{2E}{\isachardot}}{\isaliteral{2E}{\isachardot}}{\isaliteral{3C}{\isacharless}}\ findzero\ f\ n{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23003 | 1234 |
\ \ \isacommand{have}\isamarkupfalse% |
40406 | 1235 |
\ {\isaliteral{22}{\isachardoublequoteopen}}f\ n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1236 |
\ \ \isacommand{proof}\isamarkupfalse% |
1237 |
\ \isanewline |
|
1238 |
\ \ \ \ \isacommand{assume}\isamarkupfalse% |
|
40406 | 1239 |
\ {\isaliteral{22}{\isachardoublequoteopen}}f\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1240 |
\ \ \ \ \isacommand{with}\isamarkupfalse% |
1241 |
\ dom\ \isacommand{have}\isamarkupfalse% |
|
40406 | 1242 |
\ {\isaliteral{22}{\isachardoublequoteopen}}findzero\ f\ n\ {\isaliteral{3D}{\isacharequal}}\ n{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{by}\isamarkupfalse% |
1243 |
\ {\isaliteral{28}{\isacharparenleft}}simp\ add{\isaliteral{3A}{\isacharcolon}}\ findzero{\isaliteral{2E}{\isachardot}}psimps{\isaliteral{29}{\isacharparenright}}\isanewline |
|
23003 | 1244 |
\ \ \ \ \isacommand{with}\isamarkupfalse% |
40406 | 1245 |
\ x{\isaliteral{5F}{\isacharunderscore}}range\ \isacommand{show}\isamarkupfalse% |
23003 | 1246 |
\ False\ \isacommand{by}\isamarkupfalse% |
1247 |
\ auto\isanewline |
|
1248 |
\ \ \isacommand{qed}\isamarkupfalse% |
|
1249 |
\isanewline |
|
1250 |
\ \ \isanewline |
|
1251 |
\ \ \isacommand{from}\isamarkupfalse% |
|
40406 | 1252 |
\ x{\isaliteral{5F}{\isacharunderscore}}range\ \isacommand{have}\isamarkupfalse% |
1253 |
\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{3D}{\isacharequal}}\ n\ {\isaliteral{5C3C6F723E}{\isasymor}}\ x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{7B}{\isacharbraceleft}}Suc\ n\ {\isaliteral{2E}{\isachardot}}{\isaliteral{2E}{\isachardot}}{\isaliteral{3C}{\isacharless}}\ findzero\ f\ n{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{by}\isamarkupfalse% |
|
23003 | 1254 |
\ auto\isanewline |
1255 |
\ \ \isacommand{thus}\isamarkupfalse% |
|
40406 | 1256 |
\ {\isaliteral{22}{\isachardoublequoteopen}}f\ x\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1257 |
\ \ \isacommand{proof}\isamarkupfalse% |
1258 |
\isanewline |
|
1259 |
\ \ \ \ \isacommand{assume}\isamarkupfalse% |
|
40406 | 1260 |
\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{3D}{\isacharequal}}\ n{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1261 |
\ \ \ \ \isacommand{with}\isamarkupfalse% |
40406 | 1262 |
\ {\isaliteral{60}{\isacharbackquoteopen}}f\ n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{60}{\isacharbackquoteclose}}\ \isacommand{show}\isamarkupfalse% |
1263 |
\ {\isaliteral{3F}{\isacharquery}}thesis\ \isacommand{by}\isamarkupfalse% |
|
23003 | 1264 |
\ simp\isanewline |
1265 |
\ \ \isacommand{next}\isamarkupfalse% |
|
1266 |
\isanewline |
|
1267 |
\ \ \ \ \isacommand{assume}\isamarkupfalse% |
|
40406 | 1268 |
\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{7B}{\isacharbraceleft}}Suc\ n\ {\isaliteral{2E}{\isachardot}}{\isaliteral{2E}{\isachardot}}{\isaliteral{3C}{\isacharless}}\ findzero\ f\ n{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1269 |
\ \ \ \ \isacommand{with}\isamarkupfalse% |
40406 | 1270 |
\ dom\ \isakeyword{and}\ {\isaliteral{60}{\isacharbackquoteopen}}f\ n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{60}{\isacharbackquoteclose}}\ \isacommand{have}\isamarkupfalse% |
1271 |
\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{7B}{\isacharbraceleft}}Suc\ n\ {\isaliteral{2E}{\isachardot}}{\isaliteral{2E}{\isachardot}}{\isaliteral{3C}{\isacharless}}\ findzero\ f\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{7D}{\isacharbraceright}}{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{by}\isamarkupfalse% |
|
1272 |
\ {\isaliteral{28}{\isacharparenleft}}simp\ add{\isaliteral{3A}{\isacharcolon}}\ findzero{\isaliteral{2E}{\isachardot}}psimps{\isaliteral{29}{\isacharparenright}}\isanewline |
|
23003 | 1273 |
\ \ \ \ \isacommand{with}\isamarkupfalse% |
40406 | 1274 |
\ IH\ \isakeyword{and}\ {\isaliteral{60}{\isacharbackquoteopen}}f\ n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}{\isaliteral{60}{\isacharbackquoteclose}}\isanewline |
23003 | 1275 |
\ \ \ \ \isacommand{show}\isamarkupfalse% |
40406 | 1276 |
\ {\isaliteral{3F}{\isacharquery}}thesis\ \isacommand{by}\isamarkupfalse% |
23003 | 1277 |
\ simp\isanewline |
1278 |
\ \ \isacommand{qed}\isamarkupfalse% |
|
1279 |
\isanewline |
|
1280 |
\isacommand{qed}\isamarkupfalse% |
|
1281 |
% |
|
1282 |
\endisatagproof |
|
1283 |
{\isafoldproof}% |
|
1284 |
% |
|
1285 |
\isadelimproof |
|
1286 |
% |
|
1287 |
\endisadelimproof |
|
1288 |
% |
|
1289 |
\isamarkupfalse\isabellestyle{tt} |
|
23188 | 1290 |
\end{minipage}\vspace{6pt}\hrule |
23003 | 1291 |
\caption{A proof about a partial function}\label{findzero_isar} |
1292 |
\end{figure} |
|
1293 |
% |
|
1294 |
\isamarkupsubsection{Partial termination proofs% |
|
1295 |
} |
|
1296 |
\isamarkuptrue% |
|
1297 |
% |
|
1298 |
\begin{isamarkuptext}% |
|
1299 |
Now that we have proved some interesting properties about our |
|
1300 |
function, we should turn to the domain predicate and see if it is |
|
1301 |
actually true for some values. Otherwise we would have just proved |
|
1302 |
lemmas with \isa{False} as a premise. |
|
1303 |
||
40406 | 1304 |
Essentially, we need some introduction rules for \isa{findzero{\isaliteral{5F}{\isacharunderscore}}dom}. The function package can prove such domain |
23003 | 1305 |
introduction rules automatically. But since they are not used very |
23188 | 1306 |
often (they are almost never needed if the function is total), this |
1307 |
functionality is disabled by default for efficiency reasons. So we have to go |
|
40406 | 1308 |
back and ask for them explicitly by passing the \isa{{\isaliteral{28}{\isacharparenleft}}domintros{\isaliteral{29}{\isacharparenright}}} option to the function package: |
23003 | 1309 |
|
23188 | 1310 |
\vspace{1ex} |
40406 | 1311 |
\noindent\cmd{function} \isa{{\isaliteral{28}{\isacharparenleft}}domintros{\isaliteral{29}{\isacharparenright}}\ findzero\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequote}}{\isaliteral{28}{\isacharparenleft}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequote}}}\\% |
23003 | 1312 |
\cmd{where}\isanewline% |
1313 |
\ \ \ldots\\ |
|
1314 |
||
40406 | 1315 |
\noindent Now the package has proved an introduction rule for \isa{findzero{\isaliteral{5F}{\isacharunderscore}}dom}:% |
23003 | 1316 |
\end{isamarkuptext}% |
1317 |
\isamarkuptrue% |
|
1318 |
\isacommand{thm}\isamarkupfalse% |
|
40406 | 1319 |
\ findzero{\isaliteral{2E}{\isachardot}}domintros% |
23003 | 1320 |
\begin{isamarkuptext}% |
1321 |
\begin{isabelle}% |
|
40406 | 1322 |
{\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}\ {\isaliteral{3C}{\isacharless}}\ {\isaliteral{3F}{\isacharquery}}f\ {\isaliteral{3F}{\isacharquery}}n\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}f{\isaliteral{2C}{\isacharcomma}}\ Suc\ {\isaliteral{3F}{\isacharquery}}n{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}f{\isaliteral{2C}{\isacharcomma}}\ {\isaliteral{3F}{\isacharquery}}n{\isaliteral{29}{\isacharparenright}}% |
23003 | 1323 |
\end{isabelle} |
1324 |
||
1325 |
Domain introduction rules allow to show that a given value lies in the |
|
1326 |
domain of a function, if the arguments of all recursive calls |
|
1327 |
are in the domain as well. They allow to do a \qt{single step} in a |
|
1328 |
termination proof. Usually, you want to combine them with a suitable |
|
1329 |
induction principle. |
|
1330 |
||
1331 |
Since our function increases its argument at recursive calls, we |
|
1332 |
need an induction principle which works \qt{backwards}. We will use |
|
40406 | 1333 |
\isa{inc{\isaliteral{5F}{\isacharunderscore}}induct}, which allows to do induction from a fixed number |
23003 | 1334 |
\qt{downwards}: |
1335 |
||
40406 | 1336 |
\begin{center}\isa{{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}{\isaliteral{3F}{\isacharquery}}i\ {\isaliteral{5C3C6C653E}{\isasymle}}\ {\isaliteral{3F}{\isacharquery}}j{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{3F}{\isacharquery}}P\ {\isaliteral{3F}{\isacharquery}}j{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}i{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}i\ {\isaliteral{3C}{\isacharless}}\ {\isaliteral{3F}{\isacharquery}}j{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{3F}{\isacharquery}}P\ {\isaliteral{28}{\isacharparenleft}}Suc\ i{\isaliteral{29}{\isacharparenright}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}P\ i{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}P\ {\isaliteral{3F}{\isacharquery}}i}\hfill(\isa{inc{\isaliteral{5F}{\isacharunderscore}}induct})\end{center} |
23003 | 1337 |
|
23188 | 1338 |
Figure \ref{findzero_term} gives a detailed Isar proof of the fact |
23003 | 1339 |
that \isa{findzero} terminates if there is a zero which is greater |
1340 |
or equal to \isa{n}. First we derive two useful rules which will |
|
1341 |
solve the base case and the step case of the induction. The |
|
23805 | 1342 |
induction is then straightforward, except for the unusual induction |
23003 | 1343 |
principle.% |
1344 |
\end{isamarkuptext}% |
|
1345 |
\isamarkuptrue% |
|
1346 |
% |
|
1347 |
\begin{figure} |
|
23188 | 1348 |
\hrule\vspace{6pt} |
23003 | 1349 |
\begin{minipage}{0.8\textwidth} |
1350 |
\isabellestyle{it} |
|
1351 |
\isastyle\isamarkuptrue |
|
1352 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1353 |
\ findzero{\isaliteral{5F}{\isacharunderscore}}termination{\isaliteral{3A}{\isacharcolon}}\isanewline |
1354 |
\ \ \isakeyword{assumes}\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{5C3C67653E}{\isasymge}}\ n{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{and}\ {\isaliteral{22}{\isachardoublequoteopen}}f\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
1355 |
\ \ \isakeyword{shows}\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23003 | 1356 |
% |
1357 |
\isadelimproof |
|
1358 |
% |
|
1359 |
\endisadelimproof |
|
1360 |
% |
|
1361 |
\isatagproof |
|
1362 |
\isacommand{proof}\isamarkupfalse% |
|
40406 | 1363 |
\ {\isaliteral{2D}{\isacharminus}}\ \isanewline |
23003 | 1364 |
\ \ \isacommand{have}\isamarkupfalse% |
40406 | 1365 |
\ base{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ x{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1366 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
40406 | 1367 |
\ {\isaliteral{28}{\isacharparenleft}}rule\ findzero{\isaliteral{2E}{\isachardot}}domintros{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}simp\ add{\isaliteral{3A}{\isacharcolon}}{\isaliteral{60}{\isacharbackquoteopen}}f\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{60}{\isacharbackquoteclose}}{\isaliteral{29}{\isacharparenright}}\isanewline |
23003 | 1368 |
\isanewline |
1369 |
\ \ \isacommand{have}\isamarkupfalse% |
|
40406 | 1370 |
\ step{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{5C3C416E643E}{\isasymAnd}}i{\isaliteral{2E}{\isachardot}}\ findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ Suc\ i{\isaliteral{29}{\isacharparenright}}\ \isanewline |
1371 |
\ \ \ \ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ i{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23003 | 1372 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
40406 | 1373 |
\ {\isaliteral{28}{\isacharparenleft}}rule\ findzero{\isaliteral{2E}{\isachardot}}domintros{\isaliteral{29}{\isacharparenright}}\ simp\isanewline |
23003 | 1374 |
\isanewline |
1375 |
\ \ \isacommand{from}\isamarkupfalse% |
|
40406 | 1376 |
\ {\isaliteral{60}{\isacharbackquoteopen}}x\ {\isaliteral{5C3C67653E}{\isasymge}}\ n{\isaliteral{60}{\isacharbackquoteclose}}\ \isacommand{show}\isamarkupfalse% |
1377 |
\ {\isaliteral{3F}{\isacharquery}}thesis\isanewline |
|
23003 | 1378 |
\ \ \isacommand{proof}\isamarkupfalse% |
40406 | 1379 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ rule{\isaliteral{3A}{\isacharcolon}}inc{\isaliteral{5F}{\isacharunderscore}}induct{\isaliteral{29}{\isacharparenright}}\isanewline |
23003 | 1380 |
\ \ \ \ \isacommand{show}\isamarkupfalse% |
40406 | 1381 |
\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ x{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{by}\isamarkupfalse% |
1382 |
\ {\isaliteral{28}{\isacharparenleft}}rule\ base{\isaliteral{29}{\isacharparenright}}\isanewline |
|
23003 | 1383 |
\ \ \isacommand{next}\isamarkupfalse% |
1384 |
\isanewline |
|
1385 |
\ \ \ \ \isacommand{fix}\isamarkupfalse% |
|
1386 |
\ i\ \isacommand{assume}\isamarkupfalse% |
|
40406 | 1387 |
\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ Suc\ i{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1388 |
\ \ \ \ \isacommand{thus}\isamarkupfalse% |
40406 | 1389 |
\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ i{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{by}\isamarkupfalse% |
1390 |
\ {\isaliteral{28}{\isacharparenleft}}rule\ step{\isaliteral{29}{\isacharparenright}}\isanewline |
|
23003 | 1391 |
\ \ \isacommand{qed}\isamarkupfalse% |
1392 |
\isanewline |
|
1393 |
\isacommand{qed}\isamarkupfalse% |
|
1394 |
% |
|
1395 |
\endisatagproof |
|
1396 |
{\isafoldproof}% |
|
1397 |
% |
|
1398 |
\isadelimproof |
|
1399 |
% |
|
1400 |
\endisadelimproof |
|
1401 |
% |
|
1402 |
\isamarkupfalse\isabellestyle{tt} |
|
23188 | 1403 |
\end{minipage}\vspace{6pt}\hrule |
23003 | 1404 |
\caption{Termination proof for \isa{findzero}}\label{findzero_term} |
1405 |
\end{figure} |
|
1406 |
% |
|
1407 |
\begin{isamarkuptext}% |
|
1408 |
Again, the proof given in Fig.~\ref{findzero_term} has a lot of |
|
1409 |
detail in order to explain the principles. Using more automation, we |
|
1410 |
can also have a short proof:% |
|
1411 |
\end{isamarkuptext}% |
|
1412 |
\isamarkuptrue% |
|
1413 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1414 |
\ findzero{\isaliteral{5F}{\isacharunderscore}}termination{\isaliteral{5F}{\isacharunderscore}}short{\isaliteral{3A}{\isacharcolon}}\isanewline |
1415 |
\ \ \isakeyword{assumes}\ zero{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}x\ {\isaliteral{3E}{\isachargreater}}{\isaliteral{3D}{\isacharequal}}\ n{\isaliteral{22}{\isachardoublequoteclose}}\ \isanewline |
|
1416 |
\ \ \isakeyword{assumes}\ {\isaliteral{5B}{\isacharbrackleft}}simp{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}f\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
1417 |
\ \ \isakeyword{shows}\ {\isaliteral{22}{\isachardoublequoteopen}}findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23003 | 1418 |
% |
1419 |
\isadelimproof |
|
23805 | 1420 |
% |
23003 | 1421 |
\endisadelimproof |
1422 |
% |
|
1423 |
\isatagproof |
|
1424 |
\isacommand{using}\isamarkupfalse% |
|
1425 |
\ zero\isanewline |
|
23805 | 1426 |
\isacommand{by}\isamarkupfalse% |
40406 | 1427 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ rule{\isaliteral{3A}{\isacharcolon}}inc{\isaliteral{5F}{\isacharunderscore}}induct{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}auto\ intro{\isaliteral{3A}{\isacharcolon}}\ findzero{\isaliteral{2E}{\isachardot}}domintros{\isaliteral{29}{\isacharparenright}}% |
23003 | 1428 |
\endisatagproof |
1429 |
{\isafoldproof}% |
|
1430 |
% |
|
1431 |
\isadelimproof |
|
1432 |
% |
|
1433 |
\endisadelimproof |
|
1434 |
% |
|
1435 |
\begin{isamarkuptext}% |
|
23188 | 1436 |
\noindent It is simple to combine the partial correctness result with the |
23003 | 1437 |
termination lemma:% |
1438 |
\end{isamarkuptext}% |
|
1439 |
\isamarkuptrue% |
|
1440 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1441 |
\ findzero{\isaliteral{5F}{\isacharunderscore}}total{\isaliteral{5F}{\isacharunderscore}}correctness{\isaliteral{3A}{\isacharcolon}}\isanewline |
1442 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}f\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ f\ {\isaliteral{28}{\isacharparenleft}}findzero\ f\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23003 | 1443 |
% |
1444 |
\isadelimproof |
|
1445 |
% |
|
1446 |
\endisadelimproof |
|
1447 |
% |
|
1448 |
\isatagproof |
|
1449 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 1450 |
\ {\isaliteral{28}{\isacharparenleft}}blast\ intro{\isaliteral{3A}{\isacharcolon}}\ findzero{\isaliteral{5F}{\isacharunderscore}}zero\ findzero{\isaliteral{5F}{\isacharunderscore}}termination{\isaliteral{29}{\isacharparenright}}% |
23003 | 1451 |
\endisatagproof |
1452 |
{\isafoldproof}% |
|
1453 |
% |
|
1454 |
\isadelimproof |
|
1455 |
% |
|
1456 |
\endisadelimproof |
|
1457 |
% |
|
1458 |
\isamarkupsubsection{Definition of the domain predicate% |
|
1459 |
} |
|
1460 |
\isamarkuptrue% |
|
1461 |
% |
|
1462 |
\begin{isamarkuptext}% |
|
1463 |
Sometimes it is useful to know what the definition of the domain |
|
40406 | 1464 |
predicate looks like. Actually, \isa{findzero{\isaliteral{5F}{\isacharunderscore}}dom} is just an |
23003 | 1465 |
abbreviation: |
1466 |
||
1467 |
\begin{isabelle}% |
|
40406 | 1468 |
findzero{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{5C3C65717569763E}{\isasymequiv}}\ accp\ findzero{\isaliteral{5F}{\isacharunderscore}}rel% |
23003 | 1469 |
\end{isabelle} |
1470 |
||
40406 | 1471 |
The domain predicate is the \emph{accessible part} of a relation \isa{findzero{\isaliteral{5F}{\isacharunderscore}}rel}, which was also created internally by the function |
1472 |
package. \isa{findzero{\isaliteral{5F}{\isacharunderscore}}rel} is just a normal |
|
23188 | 1473 |
inductive predicate, so we can inspect its definition by |
40406 | 1474 |
looking at the introduction rules \isa{findzero{\isaliteral{5F}{\isacharunderscore}}rel{\isaliteral{2E}{\isachardot}}intros}. |
23003 | 1475 |
In our case there is just a single rule: |
1476 |
||
1477 |
\begin{isabelle}% |
|
40406 | 1478 |
{\isaliteral{3F}{\isacharquery}}f\ {\isaliteral{3F}{\isacharquery}}n\ {\isaliteral{5C3C6E6F7465713E}{\isasymnoteq}}\ {\isadigit{0}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ findzero{\isaliteral{5F}{\isacharunderscore}}rel\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}f{\isaliteral{2C}{\isacharcomma}}\ Suc\ {\isaliteral{3F}{\isacharquery}}n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}f{\isaliteral{2C}{\isacharcomma}}\ {\isaliteral{3F}{\isacharquery}}n{\isaliteral{29}{\isacharparenright}}% |
23003 | 1479 |
\end{isabelle} |
1480 |
||
40406 | 1481 |
The predicate \isa{findzero{\isaliteral{5F}{\isacharunderscore}}rel} |
23003 | 1482 |
describes the \emph{recursion relation} of the function |
1483 |
definition. The recursion relation is a binary relation on |
|
1484 |
the arguments of the function that relates each argument to its |
|
1485 |
recursive calls. In general, there is one introduction rule for each |
|
1486 |
recursive call. |
|
1487 |
||
40406 | 1488 |
The predicate \isa{findzero{\isaliteral{5F}{\isacharunderscore}}dom} is the accessible part of |
23003 | 1489 |
that relation. An argument belongs to the accessible part, if it can |
40406 | 1490 |
be reached in a finite number of steps (cf.~its definition in \isa{Wellfounded{\isaliteral{2E}{\isachardot}}thy}). |
23003 | 1491 |
|
1492 |
Since the domain predicate is just an abbreviation, you can use |
|
40406 | 1493 |
lemmas for \isa{accp} and \isa{findzero{\isaliteral{5F}{\isacharunderscore}}rel} directly. Some |
1494 |
lemmas which are occasionally useful are \isa{accpI}, \isa{accp{\isaliteral{5F}{\isacharunderscore}}downward}, and of course the introduction and elimination rules |
|
1495 |
for the recursion relation \isa{findzero{\isaliteral{2E}{\isachardot}}intros} and \isa{findzero{\isaliteral{2E}{\isachardot}}cases}.% |
|
23003 | 1496 |
\end{isamarkuptext}% |
1497 |
\isamarkuptrue% |
|
1498 |
% |
|
22065 | 1499 |
\isamarkupsection{Nested recursion% |
21212 | 1500 |
} |
1501 |
\isamarkuptrue% |
|
1502 |
% |
|
1503 |
\begin{isamarkuptext}% |
|
23003 | 1504 |
Recursive calls which are nested in one another frequently cause |
1505 |
complications, since their termination proof can depend on a partial |
|
1506 |
correctness property of the function itself. |
|
1507 |
||
1508 |
As a small example, we define the \qt{nested zero} function:% |
|
1509 |
\end{isamarkuptext}% |
|
1510 |
\isamarkuptrue% |
|
1511 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 1512 |
\ nz\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1513 |
\isakeyword{where}\isanewline |
40406 | 1514 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}nz\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
1515 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}nz\ {\isaliteral{28}{\isacharparenleft}}Suc\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ nz\ {\isaliteral{28}{\isacharparenleft}}nz\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23003 | 1516 |
% |
1517 |
\isadelimproof |
|
1518 |
% |
|
1519 |
\endisadelimproof |
|
1520 |
% |
|
1521 |
\isatagproof |
|
1522 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 1523 |
\ pat{\isaliteral{5F}{\isacharunderscore}}completeness\ auto% |
23003 | 1524 |
\endisatagproof |
1525 |
{\isafoldproof}% |
|
1526 |
% |
|
1527 |
\isadelimproof |
|
1528 |
% |
|
1529 |
\endisadelimproof |
|
1530 |
% |
|
1531 |
\begin{isamarkuptext}% |
|
1532 |
If we attempt to prove termination using the identity measure on |
|
1533 |
naturals, this fails:% |
|
1534 |
\end{isamarkuptext}% |
|
1535 |
\isamarkuptrue% |
|
1536 |
\isacommand{termination}\isamarkupfalse% |
|
1537 |
\isanewline |
|
1538 |
% |
|
1539 |
\isadelimproof |
|
1540 |
\ \ % |
|
1541 |
\endisadelimproof |
|
1542 |
% |
|
1543 |
\isatagproof |
|
1544 |
\isacommand{apply}\isamarkupfalse% |
|
40406 | 1545 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}measure\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}n{\isaliteral{2E}{\isachardot}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}\isanewline |
23003 | 1546 |
\ \ \isacommand{apply}\isamarkupfalse% |
1547 |
\ auto% |
|
1548 |
\begin{isamarkuptxt}% |
|
1549 |
We get stuck with the subgoal |
|
1550 |
||
1551 |
\begin{isabelle}% |
|
40406 | 1552 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}n{\isaliteral{2E}{\isachardot}}\ nz{\isaliteral{5F}{\isacharunderscore}}dom\ n\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ nz\ n\ {\isaliteral{3C}{\isacharless}}\ Suc\ n% |
23003 | 1553 |
\end{isabelle} |
1554 |
||
1555 |
Of course this statement is true, since we know that \isa{nz} is |
|
1556 |
the zero function. And in fact we have no problem proving this |
|
1557 |
property by induction.% |
|
1558 |
\end{isamarkuptxt}% |
|
1559 |
\isamarkuptrue% |
|
1560 |
% |
|
1561 |
\endisatagproof |
|
1562 |
{\isafoldproof}% |
|
1563 |
% |
|
1564 |
\isadelimproof |
|
1565 |
% |
|
1566 |
\endisadelimproof |
|
1567 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1568 |
\ nz{\isaliteral{5F}{\isacharunderscore}}is{\isaliteral{5F}{\isacharunderscore}}zero{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nz{\isaliteral{5F}{\isacharunderscore}}dom\ n\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ nz\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1569 |
% |
1570 |
\isadelimproof |
|
1571 |
\ \ % |
|
1572 |
\endisadelimproof |
|
1573 |
% |
|
1574 |
\isatagproof |
|
1575 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 1576 |
\ {\isaliteral{28}{\isacharparenleft}}induct\ rule{\isaliteral{3A}{\isacharcolon}}nz{\isaliteral{2E}{\isachardot}}pinduct{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}auto\ simp{\isaliteral{3A}{\isacharcolon}}\ nz{\isaliteral{2E}{\isachardot}}psimps{\isaliteral{29}{\isacharparenright}}% |
23003 | 1577 |
\endisatagproof |
1578 |
{\isafoldproof}% |
|
1579 |
% |
|
1580 |
\isadelimproof |
|
1581 |
% |
|
1582 |
\endisadelimproof |
|
1583 |
% |
|
1584 |
\begin{isamarkuptext}% |
|
1585 |
We formulate this as a partial correctness lemma with the condition |
|
40406 | 1586 |
\isa{nz{\isaliteral{5F}{\isacharunderscore}}dom\ n}. This allows us to prove it with the \isa{pinduct} rule before we have proved termination. With this lemma, |
23003 | 1587 |
the termination proof works as expected:% |
1588 |
\end{isamarkuptext}% |
|
1589 |
\isamarkuptrue% |
|
1590 |
\isacommand{termination}\isamarkupfalse% |
|
1591 |
\isanewline |
|
1592 |
% |
|
1593 |
\isadelimproof |
|
1594 |
\ \ % |
|
1595 |
\endisadelimproof |
|
1596 |
% |
|
1597 |
\isatagproof |
|
1598 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 1599 |
\ {\isaliteral{28}{\isacharparenleft}}relation\ {\isaliteral{22}{\isachardoublequoteopen}}measure\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}n{\isaliteral{2E}{\isachardot}}\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}auto\ simp{\isaliteral{3A}{\isacharcolon}}\ nz{\isaliteral{5F}{\isacharunderscore}}is{\isaliteral{5F}{\isacharunderscore}}zero{\isaliteral{29}{\isacharparenright}}% |
23003 | 1600 |
\endisatagproof |
1601 |
{\isafoldproof}% |
|
1602 |
% |
|
1603 |
\isadelimproof |
|
1604 |
% |
|
1605 |
\endisadelimproof |
|
1606 |
% |
|
1607 |
\begin{isamarkuptext}% |
|
1608 |
As a general strategy, one should prove the statements needed for |
|
1609 |
termination as a partial property first. Then they can be used to do |
|
1610 |
the termination proof. This also works for less trivial |
|
23188 | 1611 |
examples. Figure \ref{f91} defines the 91-function, a well-known |
1612 |
challenge problem due to John McCarthy, and proves its termination.% |
|
23003 | 1613 |
\end{isamarkuptext}% |
1614 |
\isamarkuptrue% |
|
1615 |
% |
|
1616 |
\begin{figure} |
|
23188 | 1617 |
\hrule\vspace{6pt} |
23003 | 1618 |
\begin{minipage}{0.8\textwidth} |
1619 |
\isabellestyle{it} |
|
1620 |
\isastyle\isamarkuptrue |
|
1621 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 1622 |
\ f{\isadigit{9}}{\isadigit{1}}\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1623 |
\isakeyword{where}\isanewline |
40406 | 1624 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}f{\isadigit{9}}{\isadigit{1}}\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isaliteral{3C}{\isacharless}}\ n\ then\ n\ {\isaliteral{2D}{\isacharminus}}\ {\isadigit{1}}{\isadigit{0}}\ else\ f{\isadigit{9}}{\isadigit{1}}\ {\isaliteral{28}{\isacharparenleft}}f{\isadigit{9}}{\isadigit{1}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1625 |
% |
1626 |
\isadelimproof |
|
1627 |
% |
|
1628 |
\endisadelimproof |
|
1629 |
% |
|
1630 |
\isatagproof |
|
1631 |
\isacommand{by}\isamarkupfalse% |
|
40406 | 1632 |
\ pat{\isaliteral{5F}{\isacharunderscore}}completeness\ auto% |
23003 | 1633 |
\endisatagproof |
1634 |
{\isafoldproof}% |
|
1635 |
% |
|
1636 |
\isadelimproof |
|
1637 |
\isanewline |
|
1638 |
% |
|
1639 |
\endisadelimproof |
|
1640 |
\isanewline |
|
1641 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1642 |
\ f{\isadigit{9}}{\isadigit{1}}{\isaliteral{5F}{\isacharunderscore}}estimate{\isaliteral{3A}{\isacharcolon}}\ \isanewline |
1643 |
\ \ \isakeyword{assumes}\ trm{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}f{\isadigit{9}}{\isadigit{1}}{\isaliteral{5F}{\isacharunderscore}}dom\ n{\isaliteral{22}{\isachardoublequoteclose}}\ \isanewline |
|
1644 |
\ \ \isakeyword{shows}\ {\isaliteral{22}{\isachardoublequoteopen}}n\ {\isaliteral{3C}{\isacharless}}\ f{\isadigit{9}}{\isadigit{1}}\ n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23003 | 1645 |
% |
1646 |
\isadelimproof |
|
1647 |
% |
|
1648 |
\endisadelimproof |
|
1649 |
% |
|
1650 |
\isatagproof |
|
1651 |
\isacommand{using}\isamarkupfalse% |
|
1652 |
\ trm\ \isacommand{by}\isamarkupfalse% |
|
40406 | 1653 |
\ induct\ {\isaliteral{28}{\isacharparenleft}}auto\ simp{\isaliteral{3A}{\isacharcolon}}\ f{\isadigit{9}}{\isadigit{1}}{\isaliteral{2E}{\isachardot}}psimps{\isaliteral{29}{\isacharparenright}}% |
23003 | 1654 |
\endisatagproof |
1655 |
{\isafoldproof}% |
|
1656 |
% |
|
1657 |
\isadelimproof |
|
1658 |
\isanewline |
|
1659 |
% |
|
1660 |
\endisadelimproof |
|
1661 |
\isanewline |
|
1662 |
\isacommand{termination}\isamarkupfalse% |
|
1663 |
\isanewline |
|
1664 |
% |
|
1665 |
\isadelimproof |
|
1666 |
% |
|
1667 |
\endisadelimproof |
|
1668 |
% |
|
1669 |
\isatagproof |
|
1670 |
\isacommand{proof}\isamarkupfalse% |
|
1671 |
\isanewline |
|
1672 |
\ \ \isacommand{let}\isamarkupfalse% |
|
40406 | 1673 |
\ {\isaliteral{3F}{\isacharquery}}R\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{22}{\isachardoublequoteopen}}measure\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6C616D6264613E}{\isasymlambda}}x{\isaliteral{2E}{\isachardot}}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{1}}\ {\isaliteral{2D}{\isacharminus}}\ x{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23003 | 1674 |
\ \ \isacommand{show}\isamarkupfalse% |
40406 | 1675 |
\ {\isaliteral{22}{\isachardoublequoteopen}}wf\ {\isaliteral{3F}{\isacharquery}}R{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{{\isaliteral{2E}{\isachardot}}{\isaliteral{2E}{\isachardot}}}\isamarkupfalse% |
23003 | 1676 |
\isanewline |
1677 |
\isanewline |
|
1678 |
\ \ \isacommand{fix}\isamarkupfalse% |
|
40406 | 1679 |
\ n\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ nat\ \isacommand{assume}\isamarkupfalse% |
1680 |
\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{5C3C6E6F743E}{\isasymnot}}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isaliteral{3C}{\isacharless}}\ n{\isaliteral{22}{\isachardoublequoteclose}}\ % |
|
23003 | 1681 |
\isamarkupcmt{Assumptions for both calls% |
1682 |
} |
|
1683 |
\isanewline |
|
1684 |
\isanewline |
|
1685 |
\ \ \isacommand{thus}\isamarkupfalse% |
|
40406 | 1686 |
\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{3F}{\isacharquery}}R{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{by}\isamarkupfalse% |
23003 | 1687 |
\ simp\ % |
1688 |
\isamarkupcmt{Inner call% |
|
1689 |
} |
|
1690 |
\isanewline |
|
1691 |
\isanewline |
|
1692 |
\ \ \isacommand{assume}\isamarkupfalse% |
|
40406 | 1693 |
\ inner{\isaliteral{5F}{\isacharunderscore}}trm{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}f{\isadigit{9}}{\isadigit{1}}{\isaliteral{5F}{\isacharunderscore}}dom\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\ % |
23003 | 1694 |
\isamarkupcmt{Outer call% |
1695 |
} |
|
1696 |
\isanewline |
|
1697 |
\ \ \isacommand{with}\isamarkupfalse% |
|
40406 | 1698 |
\ f{\isadigit{9}}{\isadigit{1}}{\isaliteral{5F}{\isacharunderscore}}estimate\ \isacommand{have}\isamarkupfalse% |
1699 |
\ {\isaliteral{22}{\isachardoublequoteopen}}n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}\ {\isaliteral{3C}{\isacharless}}\ f{\isadigit{9}}{\isadigit{1}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{{\isaliteral{2E}{\isachardot}}}\isamarkupfalse% |
|
23003 | 1700 |
\isanewline |
1701 |
\ \ \isacommand{with}\isamarkupfalse% |
|
40406 | 1702 |
\ {\isaliteral{60}{\isacharbackquoteopen}}{\isaliteral{5C3C6E6F743E}{\isasymnot}}\ {\isadigit{1}}{\isadigit{0}}{\isadigit{0}}\ {\isaliteral{3C}{\isacharless}}\ n{\isaliteral{60}{\isacharbackquoteclose}}\ \isacommand{show}\isamarkupfalse% |
1703 |
\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{28}{\isacharparenleft}}f{\isadigit{9}}{\isadigit{1}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{1}}{\isadigit{1}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{2C}{\isacharcomma}}\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{3F}{\isacharquery}}R{\isaliteral{22}{\isachardoublequoteclose}}\ \isacommand{by}\isamarkupfalse% |
|
23805 | 1704 |
\ simp\isanewline |
23003 | 1705 |
\isacommand{qed}\isamarkupfalse% |
1706 |
% |
|
1707 |
\endisatagproof |
|
1708 |
{\isafoldproof}% |
|
1709 |
% |
|
1710 |
\isadelimproof |
|
1711 |
% |
|
1712 |
\endisadelimproof |
|
1713 |
% |
|
1714 |
\isamarkupfalse\isabellestyle{tt} |
|
23188 | 1715 |
\end{minipage} |
1716 |
\vspace{6pt}\hrule |
|
23003 | 1717 |
\caption{McCarthy's 91-function}\label{f91} |
1718 |
\end{figure} |
|
1719 |
% |
|
1720 |
\isamarkupsection{Higher-Order Recursion% |
|
1721 |
} |
|
1722 |
\isamarkuptrue% |
|
1723 |
% |
|
1724 |
\begin{isamarkuptext}% |
|
1725 |
Higher-order recursion occurs when recursive calls |
|
1726 |
are passed as arguments to higher-order combinators such as \isa{map}, \isa{filter} etc. |
|
23805 | 1727 |
As an example, imagine a datatype of n-ary trees:% |
23003 | 1728 |
\end{isamarkuptext}% |
1729 |
\isamarkuptrue% |
|
1730 |
\isacommand{datatype}\isamarkupfalse% |
|
40406 | 1731 |
\ {\isaliteral{27}{\isacharprime}}a\ tree\ {\isaliteral{3D}{\isacharequal}}\ \isanewline |
1732 |
\ \ Leaf\ {\isaliteral{27}{\isacharprime}}a\ \isanewline |
|
1733 |
{\isaliteral{7C}{\isacharbar}}\ Branch\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ tree\ list{\isaliteral{22}{\isachardoublequoteclose}}% |
|
23003 | 1734 |
\begin{isamarkuptext}% |
25278 | 1735 |
\noindent We can define a function which swaps the left and right subtrees recursively, using the |
1736 |
list functions \isa{rev} and \isa{map}:% |
|
23003 | 1737 |
\end{isamarkuptext}% |
1738 |
\isamarkuptrue% |
|
27026 | 1739 |
\isacommand{fun}\isamarkupfalse% |
40406 | 1740 |
\ 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}}\isanewline |
26876 | 1741 |
\isakeyword{where}\isanewline |
40406 | 1742 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}mirror\ {\isaliteral{28}{\isacharparenleft}}Leaf\ n{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Leaf\ n{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
1743 |
{\isaliteral{7C}{\isacharbar}}\ {\isaliteral{22}{\isachardoublequoteopen}}mirror\ {\isaliteral{28}{\isacharparenleft}}Branch\ l{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Branch\ {\isaliteral{28}{\isacharparenleft}}rev\ {\isaliteral{28}{\isacharparenleft}}map\ mirror\ l{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
23003 | 1744 |
\begin{isamarkuptext}% |
27026 | 1745 |
Although the definition is accepted without problems, let us look at the termination proof:% |
23003 | 1746 |
\end{isamarkuptext}% |
1747 |
\isamarkuptrue% |
|
1748 |
\isacommand{termination}\isamarkupfalse% |
|
1749 |
% |
|
1750 |
\isadelimproof |
|
1751 |
\ % |
|
1752 |
\endisadelimproof |
|
1753 |
% |
|
1754 |
\isatagproof |
|
1755 |
\isacommand{proof}\isamarkupfalse% |
|
1756 |
% |
|
1757 |
\begin{isamarkuptxt}% |
|
1758 |
As usual, we have to give a wellfounded relation, such that the |
|
1759 |
arguments of the recursive calls get smaller. But what exactly are |
|
27026 | 1760 |
the arguments of the recursive calls when mirror is given as an |
30121 | 1761 |
argument to \isa{map}? Isabelle gives us the |
23003 | 1762 |
subgoals |
1763 |
||
1764 |
\begin{isabelle}% |
|
40406 | 1765 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ wf\ {\isaliteral{3F}{\isacharquery}}R\isanewline |
1766 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}l\ x{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ set\ l\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}x{\isaliteral{2C}{\isacharcomma}}\ Branch\ l{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C696E3E}{\isasymin}}\ {\isaliteral{3F}{\isacharquery}}R% |
|
23003 | 1767 |
\end{isabelle} |
1768 |
||
27026 | 1769 |
So the system seems to know that \isa{map} only |
25278 | 1770 |
applies the recursive call \isa{mirror} to elements |
27026 | 1771 |
of \isa{l}, which is essential for the termination proof. |
23003 | 1772 |
|
30121 | 1773 |
This knowledge about \isa{map} is encoded in so-called congruence rules, |
23003 | 1774 |
which are special theorems known to the \cmd{function} command. The |
30121 | 1775 |
rule for \isa{map} is |
23003 | 1776 |
|
1777 |
\begin{isabelle}% |
|
40406 | 1778 |
{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}{\isaliteral{3F}{\isacharquery}}xs\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}ys{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}x{\isaliteral{2E}{\isachardot}}\ x\ {\isaliteral{5C3C696E3E}{\isasymin}}\ set\ {\isaliteral{3F}{\isacharquery}}ys\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}f\ x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}g\ x{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ map\ {\isaliteral{3F}{\isacharquery}}f\ {\isaliteral{3F}{\isacharquery}}xs\ {\isaliteral{3D}{\isacharequal}}\ map\ {\isaliteral{3F}{\isacharquery}}g\ {\isaliteral{3F}{\isacharquery}}ys% |
23003 | 1779 |
\end{isabelle} |
1780 |
||
1781 |
You can read this in the following way: Two applications of \isa{map} are equal, if the list arguments are equal and the functions |
|
1782 |
coincide on the elements of the list. This means that for the value |
|
1783 |
\isa{map\ f\ l} we only have to know how \isa{f} behaves on |
|
27026 | 1784 |
the elements of \isa{l}. |
23003 | 1785 |
|
1786 |
Usually, one such congruence rule is |
|
1787 |
needed for each higher-order construct that is used when defining |
|
23805 | 1788 |
new functions. In fact, even basic functions like \isa{If} and \isa{Let} are handled by this mechanism. The congruence |
23003 | 1789 |
rule for \isa{If} states that the \isa{then} branch is only |
1790 |
relevant if the condition is true, and the \isa{else} branch only if it |
|
1791 |
is false: |
|
1792 |
||
1793 |
\begin{isabelle}% |
|
40406 | 1794 |
{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}{\isaliteral{3F}{\isacharquery}}b\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}c{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{3F}{\isacharquery}}c\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}x\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}u{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C6E6F743E}{\isasymnot}}\ {\isaliteral{3F}{\isacharquery}}c\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}y\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}v{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\isanewline |
1795 |
{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}if\ {\isaliteral{3F}{\isacharquery}}b\ then\ {\isaliteral{3F}{\isacharquery}}x\ else\ {\isaliteral{3F}{\isacharquery}}y{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}if\ {\isaliteral{3F}{\isacharquery}}c\ then\ {\isaliteral{3F}{\isacharquery}}u\ else\ {\isaliteral{3F}{\isacharquery}}v{\isaliteral{29}{\isacharparenright}}% |
|
23003 | 1796 |
\end{isabelle} |
1797 |
||
1798 |
Congruence rules can be added to the |
|
40406 | 1799 |
function package by giving them the \isa{fundef{\isaliteral{5F}{\isacharunderscore}}cong} attribute. |
23003 | 1800 |
|
23805 | 1801 |
The constructs that are predefined in Isabelle, usually |
1802 |
come with the respective congruence rules. |
|
27026 | 1803 |
But if you define your own higher-order functions, you may have to |
1804 |
state and prove the required congruence rules yourself, if you want to use your |
|
23805 | 1805 |
functions in recursive definitions.% |
27026 | 1806 |
\end{isamarkuptxt}% |
23003 | 1807 |
\isamarkuptrue% |
1808 |
% |
|
27026 | 1809 |
\endisatagproof |
1810 |
{\isafoldproof}% |
|
1811 |
% |
|
1812 |
\isadelimproof |
|
1813 |
% |
|
1814 |
\endisadelimproof |
|
1815 |
% |
|
23805 | 1816 |
\isamarkupsubsection{Congruence Rules and Evaluation Order% |
1817 |
} |
|
1818 |
\isamarkuptrue% |
|
1819 |
% |
|
23003 | 1820 |
\begin{isamarkuptext}% |
23805 | 1821 |
Higher order logic differs from functional programming languages in |
1822 |
that it has no built-in notion of evaluation order. A program is |
|
1823 |
just a set of equations, and it is not specified how they must be |
|
1824 |
evaluated. |
|
1825 |
||
1826 |
However for the purpose of function definition, we must talk about |
|
1827 |
evaluation order implicitly, when we reason about termination. |
|
1828 |
Congruence rules express that a certain evaluation order is |
|
1829 |
consistent with the logical definition. |
|
1830 |
||
1831 |
Consider the following function.% |
|
1832 |
\end{isamarkuptext}% |
|
1833 |
\isamarkuptrue% |
|
1834 |
\isacommand{function}\isamarkupfalse% |
|
40406 | 1835 |
\ f\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23805 | 1836 |
\isakeyword{where}\isanewline |
40406 | 1837 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}f\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ f\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2D}{\isacharminus}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
23805 | 1838 |
\isadelimproof |
1839 |
% |
|
1840 |
\endisadelimproof |
|
1841 |
% |
|
1842 |
\isatagproof |
|
1843 |
% |
|
1844 |
\endisatagproof |
|
1845 |
{\isafoldproof}% |
|
1846 |
% |
|
1847 |
\isadelimproof |
|
1848 |
% |
|
1849 |
\endisadelimproof |
|
1850 |
% |
|
1851 |
\begin{isamarkuptext}% |
|
27026 | 1852 |
For this definition, the termination proof fails. The default configuration |
23805 | 1853 |
specifies no congruence rule for disjunction. We have to add a |
1854 |
congruence rule that specifies left-to-right evaluation order: |
|
1855 |
||
1856 |
\vspace{1ex} |
|
40406 | 1857 |
\noindent \isa{{\isaliteral{5C3C6C6272616B6B3E}{\isasymlbrakk}}{\isaliteral{3F}{\isacharquery}}P\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}P{\isaliteral{27}{\isacharprime}}{\isaliteral{3B}{\isacharsemicolon}}\ {\isaliteral{5C3C6E6F743E}{\isasymnot}}\ {\isaliteral{3F}{\isacharquery}}P{\isaliteral{27}{\isacharprime}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{3F}{\isacharquery}}Q\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}Q{\isaliteral{27}{\isacharprime}}{\isaliteral{5C3C726272616B6B3E}{\isasymrbrakk}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}P\ {\isaliteral{5C3C6F723E}{\isasymor}}\ {\isaliteral{3F}{\isacharquery}}Q{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}{\isaliteral{3F}{\isacharquery}}P{\isaliteral{27}{\isacharprime}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ {\isaliteral{3F}{\isacharquery}}Q{\isaliteral{27}{\isacharprime}}{\isaliteral{29}{\isacharparenright}}}\hfill(\isa{disj{\isaliteral{5F}{\isacharunderscore}}cong}) |
23805 | 1858 |
\vspace{1ex} |
23003 | 1859 |
|
23805 | 1860 |
Now the definition works without problems. Note how the termination |
1861 |
proof depends on the extra condition that we get from the congruence |
|
1862 |
rule. |
|
23003 | 1863 |
|
23805 | 1864 |
However, as evaluation is not a hard-wired concept, we |
1865 |
could just turn everything around by declaring a different |
|
1866 |
congruence rule. Then we can make the reverse definition:% |
|
1867 |
\end{isamarkuptext}% |
|
1868 |
\isamarkuptrue% |
|
1869 |
\isacommand{lemma}\isamarkupfalse% |
|
40406 | 1870 |
\ disj{\isaliteral{5F}{\isacharunderscore}}cong{\isadigit{2}}{\isaliteral{5B}{\isacharbrackleft}}fundef{\isaliteral{5F}{\isacharunderscore}}cong{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3A}{\isacharcolon}}\ \isanewline |
1871 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{28}{\isacharparenleft}}{\isaliteral{5C3C6E6F743E}{\isasymnot}}\ Q{\isaliteral{27}{\isacharprime}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ P\ {\isaliteral{3D}{\isacharequal}}\ P{\isaliteral{27}{\isacharprime}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}Q\ {\isaliteral{3D}{\isacharequal}}\ Q{\isaliteral{27}{\isacharprime}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ {\isaliteral{28}{\isacharparenleft}}P\ {\isaliteral{5C3C6F723E}{\isasymor}}\ Q{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}P{\isaliteral{27}{\isacharprime}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ Q{\isaliteral{27}{\isacharprime}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
23805 | 1872 |
% |
1873 |
\isadelimproof |
|
1874 |
\ \ % |
|
1875 |
\endisadelimproof |
|
1876 |
% |
|
1877 |
\isatagproof |
|
1878 |
\isacommand{by}\isamarkupfalse% |
|
1879 |
\ blast% |
|
1880 |
\endisatagproof |
|
1881 |
{\isafoldproof}% |
|
1882 |
% |
|
1883 |
\isadelimproof |
|
1884 |
\isanewline |
|
1885 |
% |
|
1886 |
\endisadelimproof |
|
1887 |
\isanewline |
|
1888 |
\isacommand{fun}\isamarkupfalse% |
|
40406 | 1889 |
\ f{\isaliteral{27}{\isacharprime}}\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
23805 | 1890 |
\isakeyword{where}\isanewline |
40406 | 1891 |
\ \ {\isaliteral{22}{\isachardoublequoteopen}}f{\isaliteral{27}{\isacharprime}}\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}f{\isaliteral{27}{\isacharprime}}\ {\isaliteral{28}{\isacharparenleft}}n\ {\isaliteral{2D}{\isacharminus}}\ {\isadigit{1}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C6F723E}{\isasymor}}\ n\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
23805 | 1892 |
\begin{isamarkuptext}% |
1893 |
\noindent These examples show that, in general, there is no \qt{best} set of |
|
1894 |
congruence rules. |
|
1895 |
||
1896 |
However, such tweaking should rarely be necessary in |
|
1897 |
practice, as most of the time, the default set of congruence rules |
|
1898 |
works well.% |
|
21212 | 1899 |
\end{isamarkuptext}% |
1900 |
\isamarkuptrue% |
|
1901 |
% |
|
1902 |
\isadelimtheory |
|
1903 |
% |
|
1904 |
\endisadelimtheory |
|
1905 |
% |
|
1906 |
\isatagtheory |
|
1907 |
\isacommand{end}\isamarkupfalse% |
|
1908 |
% |
|
1909 |
\endisatagtheory |
|
1910 |
{\isafoldtheory}% |
|
1911 |
% |
|
1912 |
\isadelimtheory |
|
1913 |
% |
|
1914 |
\endisadelimtheory |
|
1915 |
\isanewline |
|
1916 |
\end{isabellebody}% |
|
1917 |
%%% Local Variables: |
|
1918 |
%%% mode: latex |
|
1919 |
%%% TeX-master: "root" |
|
1920 |
%%% End: |