author | wenzelm |
Sun, 20 May 2012 11:34:33 +0200 | |
changeset 47884 | 21c42b095c84 |
parent 47719 | 8aac84627b84 |
permissions | -rw-r--r-- |
47269 | 1 |
% |
2 |
\begin{isabellebody}% |
|
3 |
\def\isabellecontext{Bool{\isaliteral{5F}{\isacharunderscore}}nat{\isaliteral{5F}{\isacharunderscore}}list}% |
|
4 |
% |
|
5 |
\isadelimtheory |
|
6 |
% |
|
7 |
\endisadelimtheory |
|
8 |
% |
|
9 |
\isatagtheory |
|
10 |
% |
|
11 |
\endisatagtheory |
|
12 |
{\isafoldtheory}% |
|
13 |
% |
|
14 |
\isadelimtheory |
|
15 |
% |
|
16 |
\endisadelimtheory |
|
17 |
% |
|
18 |
\begin{isamarkuptext}% |
|
19 |
\vspace{-4ex} |
|
20 |
\section{\texorpdfstring{Types \isa{bool}, \isa{nat} and \isa{list}}{Types bool, nat and list}} |
|
21 |
||
22 |
These are the most important predefined types. We go through them one by one. |
|
23 |
Based on examples we learn how to define (possibly recursive) functions and |
|
24 |
prove theorems about them by induction and simplification. |
|
25 |
||
26 |
\subsection{Type \isa{bool}} |
|
27 |
||
28 |
The type of boolean values is a predefined datatype |
|
29 |
\begin{isabelle}% |
|
30 |
\isacommand{datatype}\ bool\ {\isaliteral{3D}{\isacharequal}}\ True\ {\isaliteral{7C}{\isacharbar}}\ False% |
|
31 |
\end{isabelle} |
|
32 |
with the two values \isa{True} and \isa{False} and |
|
33 |
with many predefined functions: \isa{{\isaliteral{5C3C6E6F743E}{\isasymnot}}}, \isa{{\isaliteral{5C3C616E643E}{\isasymand}}}, \isa{{\isaliteral{5C3C6F723E}{\isasymor}}}, \isa{{\isaliteral{5C3C6C6F6E6772696768746172726F773E}{\isasymlongrightarrow}}} etc. Here is how conjunction could be defined by pattern matching:% |
|
34 |
\end{isamarkuptext}% |
|
35 |
\isamarkuptrue% |
|
36 |
\isacommand{fun}\isamarkupfalse% |
|
37 |
\ conj\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}bool\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ bool{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline |
|
38 |
{\isaliteral{22}{\isachardoublequoteopen}}conj\ True\ True\ {\isaliteral{3D}{\isacharequal}}\ True{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline |
|
39 |
{\isaliteral{22}{\isachardoublequoteopen}}conj\ {\isaliteral{5F}{\isacharunderscore}}\ {\isaliteral{5F}{\isacharunderscore}}\ {\isaliteral{3D}{\isacharequal}}\ False{\isaliteral{22}{\isachardoublequoteclose}}% |
|
40 |
\begin{isamarkuptext}% |
|
41 |
Both the datatype and function definitions roughly follow the syntax |
|
42 |
of functional programming languages. |
|
43 |
||
44 |
\subsection{Type \isa{nat}} |
|
45 |
||
46 |
Natural numbers are another predefined datatype: |
|
47 |
\begin{isabelle}% |
|
48 |
\isacommand{datatype}\ nat\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\ {\isaliteral{7C}{\isacharbar}}\ Suc\ nat% |
|
49 |
\end{isabelle} |
|
50 |
All values of type \isa{nat} are generated by the constructors |
|
51 |
\isa{{\isadigit{0}}} and \isa{Suc}. Thus the values of type \isa{nat} are |
|
52 |
\isa{{\isadigit{0}}}, \isa{Suc\ {\isadigit{0}}}, \isa{Suc\ {\isaliteral{28}{\isacharparenleft}}Suc\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}} etc. |
|
53 |
There are many predefined functions: \isa{{\isaliteral{2B}{\isacharplus}}}, \isa{{\isaliteral{2A}{\isacharasterisk}}}, \isa{{\isaliteral{5C3C6C653E}{\isasymle}}}, etc. Here is how you could define your own addition:% |
|
54 |
\end{isamarkuptext}% |
|
55 |
\isamarkuptrue% |
|
56 |
\isacommand{fun}\isamarkupfalse% |
|
57 |
\ add\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline |
|
58 |
{\isaliteral{22}{\isachardoublequoteopen}}add\ {\isadigit{0}}\ n\ {\isaliteral{3D}{\isacharequal}}\ n{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline |
|
59 |
{\isaliteral{22}{\isachardoublequoteopen}}add\ {\isaliteral{28}{\isacharparenleft}}Suc\ m{\isaliteral{29}{\isacharparenright}}\ n\ {\isaliteral{3D}{\isacharequal}}\ Suc{\isaliteral{28}{\isacharparenleft}}add\ m\ n{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
60 |
\begin{isamarkuptext}% |
|
61 |
And here is a proof of the fact that \isa{add\ m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ m}:% |
|
62 |
\end{isamarkuptext}% |
|
63 |
\isamarkuptrue% |
|
64 |
\isacommand{lemma}\isamarkupfalse% |
|
65 |
\ add{\isaliteral{5F}{\isacharunderscore}}{\isadigit{0}}{\isadigit{2}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}add\ m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ m{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
66 |
% |
|
67 |
\isadelimproof |
|
68 |
% |
|
69 |
\endisadelimproof |
|
70 |
% |
|
71 |
\isatagproof |
|
72 |
\isacommand{apply}\isamarkupfalse% |
|
73 |
{\isaliteral{28}{\isacharparenleft}}induction\ m{\isaliteral{29}{\isacharparenright}}\isanewline |
|
74 |
\isacommand{apply}\isamarkupfalse% |
|
75 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}\isanewline |
|
76 |
\isacommand{done}\isamarkupfalse% |
|
77 |
% |
|
78 |
\endisatagproof |
|
79 |
{\isafoldproof}% |
|
80 |
% |
|
81 |
\isadelimproof |
|
82 |
% |
|
83 |
\endisadelimproof |
|
84 |
% |
|
85 |
\isadelimproof |
|
86 |
% |
|
87 |
\endisadelimproof |
|
88 |
% |
|
89 |
\isatagproof |
|
90 |
% |
|
91 |
\begin{isamarkuptxt}% |
|
92 |
The \isacom{lemma} command starts the proof and gives the lemma |
|
93 |
a name, \isa{add{\isaliteral{5F}{\isacharunderscore}}{\isadigit{0}}{\isadigit{2}}}. Properties of recursively defined functions |
|
94 |
need to be established by induction in most cases. |
|
95 |
Command \isacom{apply}\isa{{\isaliteral{28}{\isacharparenleft}}induction\ m{\isaliteral{29}{\isacharparenright}}} instructs Isabelle to |
|
96 |
start a proof by induction on \isa{m}. In response, it will show the |
|
97 |
following proof state: |
|
98 |
\begin{isabelle}% |
|
99 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ add\ {\isadigit{0}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}\isanewline |
|
100 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}m{\isaliteral{2E}{\isachardot}}\ add\ m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ m\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ add\ {\isaliteral{28}{\isacharparenleft}}Suc\ m{\isaliteral{29}{\isacharparenright}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ m% |
|
101 |
\end{isabelle} |
|
102 |
The numbered lines are known as \emph{subgoals}. |
|
103 |
The first subgoal is the base case, the second one the induction step. |
|
104 |
The prefix \isa{{\isaliteral{5C3C416E643E}{\isasymAnd}}m{\isaliteral{2E}{\isachardot}}} is Isabelle's way of saying ``for an arbitrary but fixed \isa{m}''. The \isa{{\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}} separates assumptions from the conclusion. |
|
105 |
The command \isacom{apply}\isa{{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}} instructs Isabelle to try |
|
106 |
and prove all subgoals automatically, essentially by simplifying them. |
|
107 |
Because both subgoals are easy, Isabelle can do it. |
|
108 |
The base case \isa{add\ {\isadigit{0}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}} holds by definition of \isa{add}, |
|
109 |
and the induction step is almost as simple: |
|
110 |
\isa{add~{\isaliteral{28}{\isacharparenleft}}Suc\ m{\isaliteral{29}{\isacharparenright}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ Suc{\isaliteral{28}{\isacharparenleft}}add\ m\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ m} |
|
111 |
using first the definition of \isa{add} and then the induction hypothesis. |
|
112 |
In summary, both subproofs rely on simplification with function definitions and |
|
113 |
the induction hypothesis. |
|
114 |
As a result of that final \isacom{done}, Isabelle associates the lemma |
|
115 |
just proved with its name. You can now inspect the lemma with the command% |
|
116 |
\end{isamarkuptxt}% |
|
117 |
\isamarkuptrue% |
|
118 |
% |
|
119 |
\endisatagproof |
|
120 |
{\isafoldproof}% |
|
121 |
% |
|
122 |
\isadelimproof |
|
123 |
% |
|
124 |
\endisadelimproof |
|
125 |
\isacommand{thm}\isamarkupfalse% |
|
126 |
\ add{\isaliteral{5F}{\isacharunderscore}}{\isadigit{0}}{\isadigit{2}}% |
|
127 |
\isadelimproof |
|
128 |
% |
|
129 |
\endisadelimproof |
|
130 |
% |
|
131 |
\isatagproof |
|
132 |
% |
|
133 |
\begin{isamarkuptxt}% |
|
134 |
which displays \begin{isabelle}% |
|
135 |
add\ {\isaliteral{3F}{\isacharquery}}m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{3F}{\isacharquery}}m% |
|
136 |
\end{isabelle} The free |
|
137 |
variable \isa{m} has been replaced by the \concept{unknown} |
|
138 |
\isa{{\isaliteral{3F}{\isacharquery}}m}. There is no logical difference between the two but an |
|
139 |
operational one: unknowns can be instantiated, which is what you want after |
|
140 |
some lemma has been proved. |
|
141 |
||
142 |
Note that there is also a proof method \isa{induct}, which behaves almost |
|
143 |
like \isa{induction}; the difference is explained in \autoref{ch:Isar}. |
|
144 |
||
145 |
\begin{warn} |
|
146 |
Terminology: We use \concept{lemma}, \concept{theorem} and \concept{rule} |
|
147 |
interchangeably for propositions that have been proved. |
|
148 |
\end{warn} |
|
149 |
\begin{warn} |
|
150 |
Numerals (\isa{{\isadigit{0}}}, \isa{{\isadigit{1}}}, \isa{{\isadigit{2}}}, \dots) and most of the standard |
|
151 |
arithmetic operations (\isa{{\isaliteral{2B}{\isacharplus}}}, \isa{{\isaliteral{2D}{\isacharminus}}}, \isa{{\isaliteral{2A}{\isacharasterisk}}}, \isa{{\isaliteral{5C3C6C653E}{\isasymle}}}, |
|
152 |
\isa{{\isaliteral{3C}{\isacharless}}} etc) are overloaded: they are available |
|
153 |
not just for natural numbers but for other types as well. |
|
154 |
For example, given the goal \isa{x\ {\isaliteral{2B}{\isacharplus}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ x}, there is nothing to indicate |
|
155 |
that you are talking about natural numbers. Hence Isabelle can only infer |
|
156 |
that \isa{x} is of some arbitrary type where \isa{{\isadigit{0}}} and \isa{{\isaliteral{2B}{\isacharplus}}} |
|
157 |
exist. As a consequence, you will be unable to prove the |
|
158 |
goal. To alert you to such pitfalls, Isabelle flags numerals without a |
|
159 |
fixed type in its output: \isa{x\ {\isaliteral{2B}{\isacharplus}}\ {\isaliteral{28}{\isacharparenleft}}{\isadigit{0}}{\isaliteral{5C3C436F6C6F6E3E}{\isasymColon}}{\isaliteral{27}{\isacharprime}}a{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ x}. In this particular example, |
|
160 |
you need to include |
|
161 |
an explicit type constraint, for example \isa{x{\isaliteral{2B}{\isacharplus}}{\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{28}{\isacharparenleft}}x{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}nat{\isaliteral{29}{\isacharparenright}}}. If there |
|
162 |
is enough contextual information this may not be necessary: \isa{Suc\ x\ {\isaliteral{3D}{\isacharequal}}\ x} automatically implies \isa{x{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}nat} because \isa{Suc} is not |
|
163 |
overloaded. |
|
164 |
\end{warn} |
|
165 |
||
166 |
\subsubsection{An informal proof} |
|
167 |
||
168 |
Above we gave some terse informal explanation of the proof of |
|
169 |
\isa{add\ m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ m}. A more detailed informal exposition of the lemma |
|
170 |
might look like this: |
|
171 |
\bigskip |
|
172 |
||
173 |
\noindent |
|
174 |
\textbf{Lemma} \isa{add\ m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ m} |
|
175 |
||
176 |
\noindent |
|
177 |
\textbf{Proof} by induction on \isa{m}. |
|
178 |
\begin{itemize} |
|
179 |
\item Case \isa{{\isadigit{0}}} (the base case): \isa{add\ {\isadigit{0}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}} |
|
180 |
holds by definition of \isa{add}. |
|
181 |
\item Case \isa{Suc\ m} (the induction step): |
|
182 |
We assume \isa{add\ m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ m}, the induction hypothesis (IH), |
|
183 |
and we need to show \isa{add\ {\isaliteral{28}{\isacharparenleft}}Suc\ m{\isaliteral{29}{\isacharparenright}}\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ Suc\ m}. |
|
184 |
The proof is as follows:\smallskip |
|
185 |
||
186 |
\begin{tabular}{@ {}rcl@ {\quad}l@ {}} |
|
187 |
\isa{add\ {\isaliteral{28}{\isacharparenleft}}Suc\ m{\isaliteral{29}{\isacharparenright}}\ {\isadigit{0}}} &\isa{{\isaliteral{3D}{\isacharequal}}}& \isa{Suc\ {\isaliteral{28}{\isacharparenleft}}add\ m\ {\isadigit{0}}{\isaliteral{29}{\isacharparenright}}} |
|
188 |
& by definition of \isa{add}\\ |
|
189 |
&\isa{{\isaliteral{3D}{\isacharequal}}}& \isa{Suc\ m} & by IH |
|
190 |
\end{tabular} |
|
191 |
\end{itemize} |
|
192 |
Throughout this book, \concept{IH} will stand for ``induction hypothesis''. |
|
193 |
||
194 |
We have now seen three proofs of \isa{add\ m\ {\isadigit{0}}\ {\isaliteral{3D}{\isacharequal}}\ {\isadigit{0}}}: the Isabelle one, the |
|
47711 | 195 |
terse four lines explaining the base case and the induction step, and just now a |
47269 | 196 |
model of a traditional inductive proof. The three proofs differ in the level |
197 |
of detail given and the intended reader: the Isabelle proof is for the |
|
198 |
machine, the informal proofs are for humans. Although this book concentrates |
|
47711 | 199 |
on Isabelle proofs, it is important to be able to rephrase those proofs |
47269 | 200 |
as informal text comprehensible to a reader familiar with traditional |
201 |
mathematical proofs. Later on we will introduce an Isabelle proof language |
|
202 |
that is closer to traditional informal mathematical language and is often |
|
203 |
directly readable. |
|
204 |
||
205 |
\subsection{Type \isa{list}} |
|
206 |
||
207 |
Although lists are already predefined, we define our own copy just for |
|
208 |
demonstration purposes:% |
|
209 |
\end{isamarkuptxt}% |
|
210 |
\isamarkuptrue% |
|
211 |
% |
|
212 |
\endisatagproof |
|
213 |
{\isafoldproof}% |
|
214 |
% |
|
215 |
\isadelimproof |
|
216 |
% |
|
217 |
\endisadelimproof |
|
218 |
\isacommand{datatype}\isamarkupfalse% |
|
47302 | 219 |
\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{3D}{\isacharequal}}\ Nil\ {\isaliteral{7C}{\isacharbar}}\ Cons\ {\isaliteral{27}{\isacharprime}}a\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ list{\isaliteral{22}{\isachardoublequoteclose}}% |
47269 | 220 |
\begin{isamarkuptext}% |
47302 | 221 |
\begin{itemize} |
47711 | 222 |
\item Type \isa{{\isaliteral{27}{\isacharprime}}a\ list} is the type of lists over elements of type \isa{{\isaliteral{27}{\isacharprime}}a}. Because \isa{{\isaliteral{27}{\isacharprime}}a} is a type variable, lists are in fact \concept{polymorphic}: the elements of a list can be of arbitrary type (but must all be of the same type). |
47302 | 223 |
\item Lists have two constructors: \isa{Nil}, the empty list, and \isa{Cons}, which puts an element (of type \isa{{\isaliteral{27}{\isacharprime}}a}) in front of a list (of type \isa{{\isaliteral{27}{\isacharprime}}a\ list}). |
224 |
Hence all lists are of the form \isa{Nil}, or \isa{Cons\ x\ Nil}, |
|
47269 | 225 |
or \isa{Cons\ x\ {\isaliteral{28}{\isacharparenleft}}Cons\ y\ Nil{\isaliteral{29}{\isacharparenright}}} etc. |
47302 | 226 |
\item \isacom{datatype} requires no quotation marks on the |
227 |
left-hand side, but on the right-hand side each of the argument |
|
228 |
types of a constructor needs to be enclosed in quotation marks, unless |
|
229 |
it is just an identifier (e.g.\ \isa{nat} or \isa{{\isaliteral{27}{\isacharprime}}a}). |
|
230 |
\end{itemize} |
|
47269 | 231 |
We also define two standard functions, append and reverse:% |
232 |
\end{isamarkuptext}% |
|
233 |
\isamarkuptrue% |
|
234 |
\isacommand{fun}\isamarkupfalse% |
|
235 |
\ app\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline |
|
236 |
{\isaliteral{22}{\isachardoublequoteopen}}app\ Nil\ ys\ {\isaliteral{3D}{\isacharequal}}\ ys{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline |
|
237 |
{\isaliteral{22}{\isachardoublequoteopen}}app\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}\ ys\ {\isaliteral{3D}{\isacharequal}}\ Cons\ x\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
238 |
\isanewline |
|
239 |
\isacommand{fun}\isamarkupfalse% |
|
240 |
\ rev\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}{\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list{\isaliteral{22}{\isachardoublequoteclose}}\ \isakeyword{where}\isanewline |
|
241 |
{\isaliteral{22}{\isachardoublequoteopen}}rev\ Nil\ {\isaliteral{3D}{\isacharequal}}\ Nil{\isaliteral{22}{\isachardoublequoteclose}}\ {\isaliteral{7C}{\isacharbar}}\isanewline |
|
242 |
{\isaliteral{22}{\isachardoublequoteopen}}rev\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ Nil{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
243 |
\begin{isamarkuptext}% |
|
244 |
By default, variables \isa{xs}, \isa{ys} and \isa{zs} are of |
|
245 |
\isa{list} type. |
|
246 |
||
247 |
Command \isacom{value} evaluates a term. For example,% |
|
248 |
\end{isamarkuptext}% |
|
249 |
\isamarkuptrue% |
|
250 |
\isacommand{value}\isamarkupfalse% |
|
251 |
\ {\isaliteral{22}{\isachardoublequoteopen}}rev{\isaliteral{28}{\isacharparenleft}}Cons\ True\ {\isaliteral{28}{\isacharparenleft}}Cons\ False\ Nil{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
252 |
\begin{isamarkuptext}% |
|
253 |
yields the result \isa{Cons\ False\ {\isaliteral{28}{\isacharparenleft}}Cons\ True\ Nil{\isaliteral{29}{\isacharparenright}}}. This works symbolically, too:% |
|
254 |
\end{isamarkuptext}% |
|
255 |
\isamarkuptrue% |
|
256 |
\isacommand{value}\isamarkupfalse% |
|
257 |
\ {\isaliteral{22}{\isachardoublequoteopen}}rev{\isaliteral{28}{\isacharparenleft}}Cons\ a\ {\isaliteral{28}{\isacharparenleft}}Cons\ b\ Nil{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
258 |
\begin{isamarkuptext}% |
|
259 |
yields \isa{Cons\ b\ {\isaliteral{28}{\isacharparenleft}}Cons\ a\ Nil{\isaliteral{29}{\isacharparenright}}}. |
|
260 |
\medskip |
|
261 |
||
47302 | 262 |
Figure~\ref{fig:MyList} shows the theory created so far. |
47719
8aac84627b84
the perennial doc problem of how to define lists a second time
nipkow
parents:
47711
diff
changeset
|
263 |
Because \isa{list}, \isa{Nil}, \isa{Cons} etc are already predefined, |
8aac84627b84
the perennial doc problem of how to define lists a second time
nipkow
parents:
47711
diff
changeset
|
264 |
Isabelle prints qualified (long) names when executing this theory, for example, \isa{MyList{\isaliteral{2E}{\isachardot}}Nil} |
8aac84627b84
the perennial doc problem of how to define lists a second time
nipkow
parents:
47711
diff
changeset
|
265 |
instead of \isa{Nil}. |
8aac84627b84
the perennial doc problem of how to define lists a second time
nipkow
parents:
47711
diff
changeset
|
266 |
To suppress the qualified names you can insert the command |
8aac84627b84
the perennial doc problem of how to define lists a second time
nipkow
parents:
47711
diff
changeset
|
267 |
\texttt{declare [[names\_short]]}. |
8aac84627b84
the perennial doc problem of how to define lists a second time
nipkow
parents:
47711
diff
changeset
|
268 |
This is not recommended in general but just for this unusual example. |
47302 | 269 |
% Notice where the |
270 |
%quotations marks are needed that we mostly sweep under the carpet. In |
|
271 |
%particular, notice that \isacom{datatype} requires no quotation marks on the |
|
272 |
%left-hand side, but that on the right-hand side each of the argument |
|
273 |
%types of a constructor needs to be enclosed in quotation marks. |
|
47269 | 274 |
|
275 |
\begin{figure}[htbp] |
|
276 |
\begin{alltt} |
|
277 |
\input{Thys/MyList.thy}\end{alltt} |
|
278 |
\caption{A Theory of Lists} |
|
279 |
\label{fig:MyList} |
|
280 |
\end{figure} |
|
281 |
||
282 |
\subsubsection{Structural Induction for Lists} |
|
283 |
||
284 |
Just as for natural numbers, there is a proof principle of induction for |
|
285 |
lists. Induction over a list is essentially induction over the length of |
|
286 |
the list, although the length remains implicit. To prove that some property |
|
287 |
\isa{P} holds for all lists \isa{xs}, i.e.\ \mbox{\isa{P\ xs}}, |
|
288 |
you need to prove |
|
289 |
\begin{enumerate} |
|
290 |
\item the base case \isa{P\ Nil} and |
|
47711 | 291 |
\item the inductive case \isa{P\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}} under the assumption \isa{P\ xs}, for some arbitrary but fixed \isa{x} and \isa{xs}. |
47269 | 292 |
\end{enumerate} |
293 |
This is often called \concept{structural induction}. |
|
294 |
||
295 |
\subsection{The Proof Process} |
|
296 |
||
297 |
We will now demonstrate the typical proof process, which involves |
|
298 |
the formulation and proof of auxiliary lemmas. |
|
299 |
Our goal is to show that reversing a list twice produces the original |
|
300 |
list.% |
|
301 |
\end{isamarkuptext}% |
|
302 |
\isamarkuptrue% |
|
303 |
\isacommand{theorem}\isamarkupfalse% |
|
304 |
\ rev{\isaliteral{5F}{\isacharunderscore}}rev\ {\isaliteral{5B}{\isacharbrackleft}}simp{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}rev{\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs{\isaliteral{22}{\isachardoublequoteclose}}% |
|
305 |
\isadelimproof |
|
306 |
% |
|
307 |
\endisadelimproof |
|
308 |
% |
|
309 |
\isatagproof |
|
310 |
% |
|
311 |
\begin{isamarkuptxt}% |
|
312 |
Commands \isacom{theorem} and \isacom{lemma} are |
|
313 |
interchangeable and merely indicate the importance we attach to a |
|
314 |
proposition. Via the bracketed attribute \isa{simp} we also tell Isabelle |
|
315 |
to make the eventual theorem a \concept{simplification rule}: future proofs |
|
316 |
involving simplification will replace occurrences of \isa{rev\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}} by |
|
317 |
\isa{xs}. The proof is by induction:% |
|
318 |
\end{isamarkuptxt}% |
|
319 |
\isamarkuptrue% |
|
320 |
\isacommand{apply}\isamarkupfalse% |
|
321 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}% |
|
322 |
\begin{isamarkuptxt}% |
|
323 |
As explained above, we obtain two subgoals, namely the base case (\isa{Nil}) and the induction step (\isa{Cons}): |
|
324 |
\begin{isabelle}% |
|
325 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ rev\ {\isaliteral{28}{\isacharparenleft}}rev\ Nil{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Nil\isanewline |
|
326 |
\ {\isadigit{2}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs{\isaliteral{2E}{\isachardot}}\ rev\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\ rev\ {\isaliteral{28}{\isacharparenleft}}rev\ {\isaliteral{28}{\isacharparenleft}}Cons\ a\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Cons\ a\ xs% |
|
327 |
\end{isabelle} |
|
328 |
Let us try to solve both goals automatically:% |
|
329 |
\end{isamarkuptxt}% |
|
330 |
\isamarkuptrue% |
|
331 |
\isacommand{apply}\isamarkupfalse% |
|
332 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}% |
|
333 |
\begin{isamarkuptxt}% |
|
334 |
Subgoal~1 is proved, and disappears; the simplified version |
|
335 |
of subgoal~2 becomes the new subgoal~1: |
|
336 |
\begin{isabelle}% |
|
337 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs{\isaliteral{2E}{\isachardot}}\isanewline |
|
338 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }rev\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline |
|
339 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }rev\ {\isaliteral{28}{\isacharparenleft}}app\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}Cons\ a\ Nil{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Cons\ a\ xs% |
|
340 |
\end{isabelle} |
|
341 |
In order to simplify this subgoal further, a lemma suggests itself. |
|
342 |
||
343 |
\subsubsection{A First Lemma} |
|
344 |
||
345 |
We insert the following lemma in front of the main theorem:% |
|
346 |
\end{isamarkuptxt}% |
|
347 |
\isamarkuptrue% |
|
348 |
% |
|
349 |
\endisatagproof |
|
350 |
{\isafoldproof}% |
|
351 |
% |
|
352 |
\isadelimproof |
|
353 |
% |
|
354 |
\endisadelimproof |
|
355 |
\isacommand{lemma}\isamarkupfalse% |
|
356 |
\ rev{\isaliteral{5F}{\isacharunderscore}}app\ {\isaliteral{5B}{\isacharbrackleft}}simp{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}rev{\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}rev\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}% |
|
357 |
\isadelimproof |
|
358 |
% |
|
359 |
\endisadelimproof |
|
360 |
% |
|
361 |
\isatagproof |
|
362 |
% |
|
363 |
\begin{isamarkuptxt}% |
|
364 |
There are two variables that we could induct on: \isa{xs} and |
|
365 |
\isa{ys}. Because \isa{app} is defined by recursion on |
|
366 |
the first argument, \isa{xs} is the correct one:% |
|
367 |
\end{isamarkuptxt}% |
|
368 |
\isamarkuptrue% |
|
369 |
\isacommand{apply}\isamarkupfalse% |
|
370 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}% |
|
371 |
\begin{isamarkuptxt}% |
|
372 |
This time not even the base case is solved automatically:% |
|
373 |
\end{isamarkuptxt}% |
|
374 |
\isamarkuptrue% |
|
375 |
\isacommand{apply}\isamarkupfalse% |
|
376 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}% |
|
377 |
\begin{isamarkuptxt}% |
|
378 |
\vspace{-5ex} |
|
379 |
\begin{isabelle}% |
|
380 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ rev\ ys\ {\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}rev\ ys{\isaliteral{29}{\isacharparenright}}\ Nil% |
|
381 |
\end{isabelle} |
|
382 |
Again, we need to abandon this proof attempt and prove another simple lemma |
|
383 |
first. |
|
384 |
||
385 |
\subsubsection{A Second Lemma} |
|
386 |
||
387 |
We again try the canonical proof procedure:% |
|
388 |
\end{isamarkuptxt}% |
|
389 |
\isamarkuptrue% |
|
390 |
% |
|
391 |
\endisatagproof |
|
392 |
{\isafoldproof}% |
|
393 |
% |
|
394 |
\isadelimproof |
|
395 |
% |
|
396 |
\endisadelimproof |
|
397 |
\isacommand{lemma}\isamarkupfalse% |
|
398 |
\ app{\isaliteral{5F}{\isacharunderscore}}Nil{\isadigit{2}}\ {\isaliteral{5B}{\isacharbrackleft}}simp{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}app\ xs\ Nil\ {\isaliteral{3D}{\isacharequal}}\ xs{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
399 |
% |
|
400 |
\isadelimproof |
|
401 |
% |
|
402 |
\endisadelimproof |
|
403 |
% |
|
404 |
\isatagproof |
|
405 |
\isacommand{apply}\isamarkupfalse% |
|
406 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}\isanewline |
|
407 |
\isacommand{apply}\isamarkupfalse% |
|
408 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}\isanewline |
|
409 |
\isacommand{done}\isamarkupfalse% |
|
410 |
% |
|
411 |
\endisatagproof |
|
412 |
{\isafoldproof}% |
|
413 |
% |
|
414 |
\isadelimproof |
|
415 |
% |
|
416 |
\endisadelimproof |
|
417 |
% |
|
418 |
\begin{isamarkuptext}% |
|
419 |
Thankfully, this worked. |
|
420 |
Now we can continue with our stuck proof attempt of the first lemma:% |
|
421 |
\end{isamarkuptext}% |
|
422 |
\isamarkuptrue% |
|
423 |
\isacommand{lemma}\isamarkupfalse% |
|
424 |
\ rev{\isaliteral{5F}{\isacharunderscore}}app\ {\isaliteral{5B}{\isacharbrackleft}}simp{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}rev{\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}rev\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
425 |
% |
|
426 |
\isadelimproof |
|
427 |
% |
|
428 |
\endisadelimproof |
|
429 |
% |
|
430 |
\isatagproof |
|
431 |
\isacommand{apply}\isamarkupfalse% |
|
432 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}\isanewline |
|
433 |
\isacommand{apply}\isamarkupfalse% |
|
434 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}% |
|
435 |
\begin{isamarkuptxt}% |
|
436 |
We find that this time \isa{auto} solves the base case, but the |
|
437 |
induction step merely simplifies to |
|
438 |
\begin{isabelle}% |
|
439 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs{\isaliteral{2E}{\isachardot}}\isanewline |
|
440 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }rev\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}rev\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline |
|
441 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }app\ {\isaliteral{28}{\isacharparenleft}}app\ {\isaliteral{28}{\isacharparenleft}}rev\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}Cons\ a\ Nil{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\isanewline |
|
442 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }app\ {\isaliteral{28}{\isacharparenleft}}rev\ ys{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}app\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}Cons\ a\ Nil{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}% |
|
443 |
\end{isabelle} |
|
47711 | 444 |
The missing lemma is associativity of \isa{app}, |
47269 | 445 |
which we insert in front of the failed lemma \isa{rev{\isaliteral{5F}{\isacharunderscore}}app}. |
446 |
||
447 |
\subsubsection{Associativity of \isa{app}} |
|
448 |
||
449 |
The canonical proof procedure succeeds without further ado:% |
|
450 |
\end{isamarkuptxt}% |
|
451 |
\isamarkuptrue% |
|
452 |
% |
|
453 |
\endisatagproof |
|
454 |
{\isafoldproof}% |
|
455 |
% |
|
456 |
\isadelimproof |
|
457 |
% |
|
458 |
\endisadelimproof |
|
459 |
\isacommand{lemma}\isamarkupfalse% |
|
460 |
\ app{\isaliteral{5F}{\isacharunderscore}}assoc\ {\isaliteral{5B}{\isacharbrackleft}}simp{\isaliteral{5D}{\isacharbrackright}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{22}{\isachardoublequoteopen}}app\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ zs\ {\isaliteral{3D}{\isacharequal}}\ app\ xs\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}{\isaliteral{22}{\isachardoublequoteclose}}\isanewline |
|
461 |
% |
|
462 |
\isadelimproof |
|
463 |
% |
|
464 |
\endisadelimproof |
|
465 |
% |
|
466 |
\isatagproof |
|
467 |
\isacommand{apply}\isamarkupfalse% |
|
468 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}\isanewline |
|
469 |
\isacommand{apply}\isamarkupfalse% |
|
470 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}\isanewline |
|
471 |
\isacommand{done}\isamarkupfalse% |
|
472 |
% |
|
473 |
\endisatagproof |
|
474 |
{\isafoldproof}% |
|
475 |
% |
|
476 |
\isadelimproof |
|
477 |
% |
|
478 |
\endisadelimproof |
|
479 |
% |
|
480 |
\isadelimproof |
|
481 |
% |
|
482 |
\endisadelimproof |
|
483 |
% |
|
484 |
\isatagproof |
|
485 |
% |
|
486 |
\endisatagproof |
|
487 |
{\isafoldproof}% |
|
488 |
% |
|
489 |
\isadelimproof |
|
490 |
% |
|
491 |
\endisadelimproof |
|
492 |
% |
|
493 |
\isadelimproof |
|
494 |
% |
|
495 |
\endisadelimproof |
|
496 |
% |
|
497 |
\isatagproof |
|
498 |
% |
|
499 |
\endisatagproof |
|
500 |
{\isafoldproof}% |
|
501 |
% |
|
502 |
\isadelimproof |
|
503 |
% |
|
504 |
\endisadelimproof |
|
505 |
% |
|
506 |
\begin{isamarkuptext}% |
|
507 |
Finally the proofs of \isa{rev{\isaliteral{5F}{\isacharunderscore}}app} and \isa{rev{\isaliteral{5F}{\isacharunderscore}}rev} |
|
508 |
succeed, too. |
|
509 |
||
510 |
\subsubsection{Another informal proof} |
|
511 |
||
512 |
Here is the informal proof of associativity of \isa{app} |
|
513 |
corresponding to the Isabelle proof above. |
|
514 |
\bigskip |
|
515 |
||
516 |
\noindent |
|
517 |
\textbf{Lemma} \isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ zs\ {\isaliteral{3D}{\isacharequal}}\ app\ xs\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}} |
|
518 |
||
519 |
\noindent |
|
520 |
\textbf{Proof} by induction on \isa{xs}. |
|
521 |
\begin{itemize} |
|
522 |
\item Case \isa{Nil}: \ \isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ Nil\ ys{\isaliteral{29}{\isacharparenright}}\ zs\ {\isaliteral{3D}{\isacharequal}}\ app\ ys\ zs} \isa{{\isaliteral{3D}{\isacharequal}}} |
|
523 |
\mbox{\isa{app\ Nil\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}}} \ holds by definition of \isa{app}. |
|
524 |
\item Case \isa{Cons\ x\ xs}: We assume |
|
525 |
\begin{center} \hfill \isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ zs} \isa{{\isaliteral{3D}{\isacharequal}}} |
|
526 |
\isa{app\ xs\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}} \hfill (IH) \end{center} |
|
527 |
and we need to show |
|
528 |
\begin{center} \isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}\ ys{\isaliteral{29}{\isacharparenright}}\ zs\ {\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}}.\end{center} |
|
529 |
The proof is as follows:\smallskip |
|
530 |
||
531 |
\begin{tabular}{@ {}l@ {\quad}l@ {}} |
|
532 |
\isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}\ ys{\isaliteral{29}{\isacharparenright}}\ zs}\\ |
|
533 |
\isa{{\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}\ zs} & by definition of \isa{app}\\ |
|
534 |
\isa{{\isaliteral{3D}{\isacharequal}}\ Cons\ x\ {\isaliteral{28}{\isacharparenleft}}app\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ zs{\isaliteral{29}{\isacharparenright}}} & by definition of \isa{app}\\ |
|
535 |
\isa{{\isaliteral{3D}{\isacharequal}}\ Cons\ x\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}{\isaliteral{29}{\isacharparenright}}} & by IH\\ |
|
536 |
\isa{{\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}} & by definition of \isa{app} |
|
537 |
\end{tabular} |
|
538 |
\end{itemize} |
|
539 |
\medskip |
|
540 |
||
541 |
\noindent Didn't we say earlier that all proofs are by simplification? But |
|
542 |
in both cases, going from left to right, the last equality step is not a |
|
543 |
simplification at all! In the base case it is \isa{app\ ys\ zs\ {\isaliteral{3D}{\isacharequal}}\ app\ Nil\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}}. It appears almost mysterious because we suddenly complicate the |
|
544 |
term by appending \isa{Nil} on the left. What is really going on is this: |
|
545 |
when proving some equality \mbox{\isa{s\ {\isaliteral{3D}{\isacharequal}}\ t}}, both \isa{s} and \isa{t} are |
|
546 |
simplified to some common term \isa{u}. This heuristic for equality proofs |
|
547 |
works well for a functional programming context like ours. In the base case |
|
548 |
\isa{s} is \isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ Nil\ ys{\isaliteral{29}{\isacharparenright}}\ zs}, \isa{t} is \isa{app\ Nil\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}}, and \isa{u} is \isa{app\ ys\ zs}. |
|
549 |
||
550 |
\subsection{Predefined lists} |
|
551 |
\label{sec:predeflists} |
|
552 |
||
553 |
Isabelle's predefined lists are the same as the ones above, but with |
|
554 |
more syntactic sugar: |
|
555 |
\begin{itemize} |
|
556 |
\item \isa{{\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} is \isa{Nil}, |
|
557 |
\item \isa{x\ {\isaliteral{23}{\isacharhash}}\ xs} is \isa{Cons\ x\ xs}, |
|
558 |
\item \isa{{\isaliteral{5B}{\isacharbrackleft}}x\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}{\isaliteral{2C}{\isacharcomma}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}{\isaliteral{2C}{\isacharcomma}}\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub n{\isaliteral{5D}{\isacharbrackright}}} is \isa{x\isaliteral{5C3C5E697375623E}{}\isactrlisub {\isadigit{1}}\ {\isaliteral{23}{\isacharhash}}\ {\isaliteral{5C3C646F74733E}{\isasymdots}}\ {\isaliteral{23}{\isacharhash}}\ x\isaliteral{5C3C5E697375623E}{}\isactrlisub n\ {\isaliteral{23}{\isacharhash}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}}, and |
|
559 |
\item \isa{xs\ {\isaliteral{40}{\isacharat}}\ ys} is \isa{app\ xs\ ys}. |
|
560 |
\end{itemize} |
|
561 |
There is also a large library of predefined functions. |
|
562 |
The most important ones are the length function |
|
563 |
\isa{length\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat} (with the obvious definition), |
|
564 |
and the map function that applies a function to all elements of a list: |
|
565 |
\begin{isabelle} |
|
47306 | 566 |
\isacom{fun} \isa{map} \isa{{\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}} \isa{{\isaliteral{22}{\isachardoublequote}}{\isaliteral{28}{\isacharparenleft}}{\isaliteral{27}{\isacharprime}}a\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}b{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}b\ list{\isaliteral{22}{\isachardoublequote}}}\\ |
567 |
\isa{{\isaliteral{22}{\isachardoublequote}}}\isa{map\ f\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}}\isa{{\isaliteral{22}{\isachardoublequote}}\ {\isaliteral{7C}{\isacharbar}}}\\ |
|
568 |
\isa{{\isaliteral{22}{\isachardoublequote}}}\isa{map\ f\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ f\ x\ {\isaliteral{23}{\isacharhash}}\ map\ f\ xs}\isa{{\isaliteral{22}{\isachardoublequote}}} |
|
47269 | 569 |
\end{isabelle} |
47306 | 570 |
\sem |
47269 | 571 |
Also useful are the \concept{head} of a list, its first element, |
572 |
and the \concept{tail}, the rest of the list: |
|
573 |
\begin{isabelle} |
|
574 |
\isacom{fun} \isa{hd\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a}\\ |
|
575 |
\isa{hd\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ x} |
|
576 |
\end{isabelle} |
|
577 |
\begin{isabelle} |
|
578 |
\isacom{fun} \isa{tl\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list}\\ |
|
579 |
\isa{tl\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} \isa{{\isaliteral{7C}{\isacharbar}}}\\ |
|
580 |
\isa{tl\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs} |
|
581 |
\end{isabelle} |
|
582 |
Note that since HOL is a logic of total functions, \isa{hd\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} is defined, |
|
583 |
but we do now know what the result is. That is, \isa{hd\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} is not undefined |
|
47306 | 584 |
but underdefined. |
585 |
\endsem |
|
586 |
%% |
|
47269 | 587 |
\end{isamarkuptext}% |
588 |
\isamarkuptrue% |
|
589 |
% |
|
590 |
\isadelimtheory |
|
591 |
% |
|
592 |
\endisadelimtheory |
|
593 |
% |
|
594 |
\isatagtheory |
|
595 |
% |
|
596 |
\endisatagtheory |
|
597 |
{\isafoldtheory}% |
|
598 |
% |
|
599 |
\isadelimtheory |
|
600 |
% |
|
601 |
\endisadelimtheory |
|
602 |
\end{isabellebody}% |
|
603 |
%%% Local Variables: |
|
604 |
%%% mode: latex |
|
605 |
%%% TeX-master: "root" |
|
606 |
%%% End: |