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.
|
|
263 |
% Notice where the
|
|
264 |
%quotations marks are needed that we mostly sweep under the carpet. In
|
|
265 |
%particular, notice that \isacom{datatype} requires no quotation marks on the
|
|
266 |
%left-hand side, but that on the right-hand side each of the argument
|
|
267 |
%types of a constructor needs to be enclosed in quotation marks.
|
47269
|
268 |
|
|
269 |
\begin{figure}[htbp]
|
|
270 |
\begin{alltt}
|
|
271 |
\input{Thys/MyList.thy}\end{alltt}
|
|
272 |
\caption{A Theory of Lists}
|
|
273 |
\label{fig:MyList}
|
|
274 |
\end{figure}
|
|
275 |
|
|
276 |
\subsubsection{Structural Induction for Lists}
|
|
277 |
|
|
278 |
Just as for natural numbers, there is a proof principle of induction for
|
|
279 |
lists. Induction over a list is essentially induction over the length of
|
|
280 |
the list, although the length remains implicit. To prove that some property
|
|
281 |
\isa{P} holds for all lists \isa{xs}, i.e.\ \mbox{\isa{P\ xs}},
|
|
282 |
you need to prove
|
|
283 |
\begin{enumerate}
|
|
284 |
\item the base case \isa{P\ Nil} and
|
47711
|
285 |
\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
|
286 |
\end{enumerate}
|
|
287 |
This is often called \concept{structural induction}.
|
|
288 |
|
|
289 |
\subsection{The Proof Process}
|
|
290 |
|
|
291 |
We will now demonstrate the typical proof process, which involves
|
|
292 |
the formulation and proof of auxiliary lemmas.
|
|
293 |
Our goal is to show that reversing a list twice produces the original
|
|
294 |
list.%
|
|
295 |
\end{isamarkuptext}%
|
|
296 |
\isamarkuptrue%
|
|
297 |
\isacommand{theorem}\isamarkupfalse%
|
|
298 |
\ 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}}%
|
|
299 |
\isadelimproof
|
|
300 |
%
|
|
301 |
\endisadelimproof
|
|
302 |
%
|
|
303 |
\isatagproof
|
|
304 |
%
|
|
305 |
\begin{isamarkuptxt}%
|
|
306 |
Commands \isacom{theorem} and \isacom{lemma} are
|
|
307 |
interchangeable and merely indicate the importance we attach to a
|
|
308 |
proposition. Via the bracketed attribute \isa{simp} we also tell Isabelle
|
|
309 |
to make the eventual theorem a \concept{simplification rule}: future proofs
|
|
310 |
involving simplification will replace occurrences of \isa{rev\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}} by
|
|
311 |
\isa{xs}. The proof is by induction:%
|
|
312 |
\end{isamarkuptxt}%
|
|
313 |
\isamarkuptrue%
|
|
314 |
\isacommand{apply}\isamarkupfalse%
|
|
315 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}%
|
|
316 |
\begin{isamarkuptxt}%
|
|
317 |
As explained above, we obtain two subgoals, namely the base case (\isa{Nil}) and the induction step (\isa{Cons}):
|
|
318 |
\begin{isabelle}%
|
|
319 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ rev\ {\isaliteral{28}{\isacharparenleft}}rev\ Nil{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ Nil\isanewline
|
|
320 |
\ {\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%
|
|
321 |
\end{isabelle}
|
|
322 |
Let us try to solve both goals automatically:%
|
|
323 |
\end{isamarkuptxt}%
|
|
324 |
\isamarkuptrue%
|
|
325 |
\isacommand{apply}\isamarkupfalse%
|
|
326 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}%
|
|
327 |
\begin{isamarkuptxt}%
|
|
328 |
Subgoal~1 is proved, and disappears; the simplified version
|
|
329 |
of subgoal~2 becomes the new subgoal~1:
|
|
330 |
\begin{isabelle}%
|
|
331 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs{\isaliteral{2E}{\isachardot}}\isanewline
|
|
332 |
\isaindent{\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ \ \ \ }rev\ {\isaliteral{28}{\isacharparenleft}}rev\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs\ {\isaliteral{5C3C4C6F6E6772696768746172726F773E}{\isasymLongrightarrow}}\isanewline
|
|
333 |
\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%
|
|
334 |
\end{isabelle}
|
|
335 |
In order to simplify this subgoal further, a lemma suggests itself.
|
|
336 |
|
|
337 |
\subsubsection{A First Lemma}
|
|
338 |
|
|
339 |
We insert the following lemma in front of the main theorem:%
|
|
340 |
\end{isamarkuptxt}%
|
|
341 |
\isamarkuptrue%
|
|
342 |
%
|
|
343 |
\endisatagproof
|
|
344 |
{\isafoldproof}%
|
|
345 |
%
|
|
346 |
\isadelimproof
|
|
347 |
%
|
|
348 |
\endisadelimproof
|
|
349 |
\isacommand{lemma}\isamarkupfalse%
|
|
350 |
\ 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}}%
|
|
351 |
\isadelimproof
|
|
352 |
%
|
|
353 |
\endisadelimproof
|
|
354 |
%
|
|
355 |
\isatagproof
|
|
356 |
%
|
|
357 |
\begin{isamarkuptxt}%
|
|
358 |
There are two variables that we could induct on: \isa{xs} and
|
|
359 |
\isa{ys}. Because \isa{app} is defined by recursion on
|
|
360 |
the first argument, \isa{xs} is the correct one:%
|
|
361 |
\end{isamarkuptxt}%
|
|
362 |
\isamarkuptrue%
|
|
363 |
\isacommand{apply}\isamarkupfalse%
|
|
364 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}%
|
|
365 |
\begin{isamarkuptxt}%
|
|
366 |
This time not even the base case is solved automatically:%
|
|
367 |
\end{isamarkuptxt}%
|
|
368 |
\isamarkuptrue%
|
|
369 |
\isacommand{apply}\isamarkupfalse%
|
|
370 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}%
|
|
371 |
\begin{isamarkuptxt}%
|
|
372 |
\vspace{-5ex}
|
|
373 |
\begin{isabelle}%
|
|
374 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ rev\ ys\ {\isaliteral{3D}{\isacharequal}}\ app\ {\isaliteral{28}{\isacharparenleft}}rev\ ys{\isaliteral{29}{\isacharparenright}}\ Nil%
|
|
375 |
\end{isabelle}
|
|
376 |
Again, we need to abandon this proof attempt and prove another simple lemma
|
|
377 |
first.
|
|
378 |
|
|
379 |
\subsubsection{A Second Lemma}
|
|
380 |
|
|
381 |
We again try the canonical proof procedure:%
|
|
382 |
\end{isamarkuptxt}%
|
|
383 |
\isamarkuptrue%
|
|
384 |
%
|
|
385 |
\endisatagproof
|
|
386 |
{\isafoldproof}%
|
|
387 |
%
|
|
388 |
\isadelimproof
|
|
389 |
%
|
|
390 |
\endisadelimproof
|
|
391 |
\isacommand{lemma}\isamarkupfalse%
|
|
392 |
\ 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
|
|
393 |
%
|
|
394 |
\isadelimproof
|
|
395 |
%
|
|
396 |
\endisadelimproof
|
|
397 |
%
|
|
398 |
\isatagproof
|
|
399 |
\isacommand{apply}\isamarkupfalse%
|
|
400 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}\isanewline
|
|
401 |
\isacommand{apply}\isamarkupfalse%
|
|
402 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}\isanewline
|
|
403 |
\isacommand{done}\isamarkupfalse%
|
|
404 |
%
|
|
405 |
\endisatagproof
|
|
406 |
{\isafoldproof}%
|
|
407 |
%
|
|
408 |
\isadelimproof
|
|
409 |
%
|
|
410 |
\endisadelimproof
|
|
411 |
%
|
|
412 |
\begin{isamarkuptext}%
|
|
413 |
Thankfully, this worked.
|
|
414 |
Now we can continue with our stuck proof attempt of the first lemma:%
|
|
415 |
\end{isamarkuptext}%
|
|
416 |
\isamarkuptrue%
|
|
417 |
\isacommand{lemma}\isamarkupfalse%
|
|
418 |
\ 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
|
|
419 |
%
|
|
420 |
\isadelimproof
|
|
421 |
%
|
|
422 |
\endisadelimproof
|
|
423 |
%
|
|
424 |
\isatagproof
|
|
425 |
\isacommand{apply}\isamarkupfalse%
|
|
426 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}\isanewline
|
|
427 |
\isacommand{apply}\isamarkupfalse%
|
|
428 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}%
|
|
429 |
\begin{isamarkuptxt}%
|
|
430 |
We find that this time \isa{auto} solves the base case, but the
|
|
431 |
induction step merely simplifies to
|
|
432 |
\begin{isabelle}%
|
|
433 |
\ {\isadigit{1}}{\isaliteral{2E}{\isachardot}}\ {\isaliteral{5C3C416E643E}{\isasymAnd}}a\ xs{\isaliteral{2E}{\isachardot}}\isanewline
|
|
434 |
\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
|
|
435 |
\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
|
|
436 |
\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}}%
|
|
437 |
\end{isabelle}
|
47711
|
438 |
The missing lemma is associativity of \isa{app},
|
47269
|
439 |
which we insert in front of the failed lemma \isa{rev{\isaliteral{5F}{\isacharunderscore}}app}.
|
|
440 |
|
|
441 |
\subsubsection{Associativity of \isa{app}}
|
|
442 |
|
|
443 |
The canonical proof procedure succeeds without further ado:%
|
|
444 |
\end{isamarkuptxt}%
|
|
445 |
\isamarkuptrue%
|
|
446 |
%
|
|
447 |
\endisatagproof
|
|
448 |
{\isafoldproof}%
|
|
449 |
%
|
|
450 |
\isadelimproof
|
|
451 |
%
|
|
452 |
\endisadelimproof
|
|
453 |
\isacommand{lemma}\isamarkupfalse%
|
|
454 |
\ 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
|
|
455 |
%
|
|
456 |
\isadelimproof
|
|
457 |
%
|
|
458 |
\endisadelimproof
|
|
459 |
%
|
|
460 |
\isatagproof
|
|
461 |
\isacommand{apply}\isamarkupfalse%
|
|
462 |
{\isaliteral{28}{\isacharparenleft}}induction\ xs{\isaliteral{29}{\isacharparenright}}\isanewline
|
|
463 |
\isacommand{apply}\isamarkupfalse%
|
|
464 |
{\isaliteral{28}{\isacharparenleft}}auto{\isaliteral{29}{\isacharparenright}}\isanewline
|
|
465 |
\isacommand{done}\isamarkupfalse%
|
|
466 |
%
|
|
467 |
\endisatagproof
|
|
468 |
{\isafoldproof}%
|
|
469 |
%
|
|
470 |
\isadelimproof
|
|
471 |
%
|
|
472 |
\endisadelimproof
|
|
473 |
%
|
|
474 |
\isadelimproof
|
|
475 |
%
|
|
476 |
\endisadelimproof
|
|
477 |
%
|
|
478 |
\isatagproof
|
|
479 |
%
|
|
480 |
\endisatagproof
|
|
481 |
{\isafoldproof}%
|
|
482 |
%
|
|
483 |
\isadelimproof
|
|
484 |
%
|
|
485 |
\endisadelimproof
|
|
486 |
%
|
|
487 |
\isadelimproof
|
|
488 |
%
|
|
489 |
\endisadelimproof
|
|
490 |
%
|
|
491 |
\isatagproof
|
|
492 |
%
|
|
493 |
\endisatagproof
|
|
494 |
{\isafoldproof}%
|
|
495 |
%
|
|
496 |
\isadelimproof
|
|
497 |
%
|
|
498 |
\endisadelimproof
|
|
499 |
%
|
|
500 |
\begin{isamarkuptext}%
|
|
501 |
Finally the proofs of \isa{rev{\isaliteral{5F}{\isacharunderscore}}app} and \isa{rev{\isaliteral{5F}{\isacharunderscore}}rev}
|
|
502 |
succeed, too.
|
|
503 |
|
|
504 |
\subsubsection{Another informal proof}
|
|
505 |
|
|
506 |
Here is the informal proof of associativity of \isa{app}
|
|
507 |
corresponding to the Isabelle proof above.
|
|
508 |
\bigskip
|
|
509 |
|
|
510 |
\noindent
|
|
511 |
\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}}}
|
|
512 |
|
|
513 |
\noindent
|
|
514 |
\textbf{Proof} by induction on \isa{xs}.
|
|
515 |
\begin{itemize}
|
|
516 |
\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}}}
|
|
517 |
\mbox{\isa{app\ Nil\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}}} \ holds by definition of \isa{app}.
|
|
518 |
\item Case \isa{Cons\ x\ xs}: We assume
|
|
519 |
\begin{center} \hfill \isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ xs\ ys{\isaliteral{29}{\isacharparenright}}\ zs} \isa{{\isaliteral{3D}{\isacharequal}}}
|
|
520 |
\isa{app\ xs\ {\isaliteral{28}{\isacharparenleft}}app\ ys\ zs{\isaliteral{29}{\isacharparenright}}} \hfill (IH) \end{center}
|
|
521 |
and we need to show
|
|
522 |
\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}
|
|
523 |
The proof is as follows:\smallskip
|
|
524 |
|
|
525 |
\begin{tabular}{@ {}l@ {\quad}l@ {}}
|
|
526 |
\isa{app\ {\isaliteral{28}{\isacharparenleft}}app\ {\isaliteral{28}{\isacharparenleft}}Cons\ x\ xs{\isaliteral{29}{\isacharparenright}}\ ys{\isaliteral{29}{\isacharparenright}}\ zs}\\
|
|
527 |
\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}\\
|
|
528 |
\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}\\
|
|
529 |
\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\\
|
|
530 |
\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}
|
|
531 |
\end{tabular}
|
|
532 |
\end{itemize}
|
|
533 |
\medskip
|
|
534 |
|
|
535 |
\noindent Didn't we say earlier that all proofs are by simplification? But
|
|
536 |
in both cases, going from left to right, the last equality step is not a
|
|
537 |
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
|
|
538 |
term by appending \isa{Nil} on the left. What is really going on is this:
|
|
539 |
when proving some equality \mbox{\isa{s\ {\isaliteral{3D}{\isacharequal}}\ t}}, both \isa{s} and \isa{t} are
|
|
540 |
simplified to some common term \isa{u}. This heuristic for equality proofs
|
|
541 |
works well for a functional programming context like ours. In the base case
|
|
542 |
\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}.
|
|
543 |
|
|
544 |
\subsection{Predefined lists}
|
|
545 |
\label{sec:predeflists}
|
|
546 |
|
|
547 |
Isabelle's predefined lists are the same as the ones above, but with
|
|
548 |
more syntactic sugar:
|
|
549 |
\begin{itemize}
|
|
550 |
\item \isa{{\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} is \isa{Nil},
|
|
551 |
\item \isa{x\ {\isaliteral{23}{\isacharhash}}\ xs} is \isa{Cons\ x\ xs},
|
|
552 |
\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
|
|
553 |
\item \isa{xs\ {\isaliteral{40}{\isacharat}}\ ys} is \isa{app\ xs\ ys}.
|
|
554 |
\end{itemize}
|
|
555 |
There is also a large library of predefined functions.
|
|
556 |
The most important ones are the length function
|
|
557 |
\isa{length\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ nat} (with the obvious definition),
|
|
558 |
and the map function that applies a function to all elements of a list:
|
|
559 |
\begin{isabelle}
|
47306
|
560 |
\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}}}\\
|
|
561 |
\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}}}\\
|
|
562 |
\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
|
563 |
\end{isabelle}
|
47306
|
564 |
\sem
|
47269
|
565 |
Also useful are the \concept{head} of a list, its first element,
|
|
566 |
and the \concept{tail}, the rest of the list:
|
|
567 |
\begin{isabelle}
|
|
568 |
\isacom{fun} \isa{hd\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a}\\
|
|
569 |
\isa{hd\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ x}
|
|
570 |
\end{isabelle}
|
|
571 |
\begin{isabelle}
|
|
572 |
\isacom{fun} \isa{tl\ {\isaliteral{3A}{\isacharcolon}}{\isaliteral{3A}{\isacharcolon}}\ {\isaliteral{27}{\isacharprime}}a\ list\ {\isaliteral{5C3C52696768746172726F773E}{\isasymRightarrow}}\ {\isaliteral{27}{\isacharprime}}a\ list}\\
|
|
573 |
\isa{tl\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}\ {\isaliteral{3D}{\isacharequal}}\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} \isa{{\isaliteral{7C}{\isacharbar}}}\\
|
|
574 |
\isa{tl\ {\isaliteral{28}{\isacharparenleft}}x\ {\isaliteral{23}{\isacharhash}}\ xs{\isaliteral{29}{\isacharparenright}}\ {\isaliteral{3D}{\isacharequal}}\ xs}
|
|
575 |
\end{isabelle}
|
|
576 |
Note that since HOL is a logic of total functions, \isa{hd\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} is defined,
|
|
577 |
but we do now know what the result is. That is, \isa{hd\ {\isaliteral{5B}{\isacharbrackleft}}{\isaliteral{5D}{\isacharbrackright}}} is not undefined
|
47306
|
578 |
but underdefined.
|
|
579 |
\endsem
|
|
580 |
%%
|
47269
|
581 |
\end{isamarkuptext}%
|
|
582 |
\isamarkuptrue%
|
|
583 |
%
|
|
584 |
\isadelimtheory
|
|
585 |
%
|
|
586 |
\endisadelimtheory
|
|
587 |
%
|
|
588 |
\isatagtheory
|
|
589 |
%
|
|
590 |
\endisatagtheory
|
|
591 |
{\isafoldtheory}%
|
|
592 |
%
|
|
593 |
\isadelimtheory
|
|
594 |
%
|
|
595 |
\endisadelimtheory
|
|
596 |
\end{isabellebody}%
|
|
597 |
%%% Local Variables:
|
|
598 |
%%% mode: latex
|
|
599 |
%%% TeX-master: "root"
|
|
600 |
%%% End:
|