author | wenzelm |
Wed, 09 Oct 2024 13:06:55 +0200 | |
changeset 81136 | 2b949a3bfaac |
parent 81128 | 5b201b24d99b |
permissions | -rw-r--r-- |
15136 | 1 |
theory ToyList |
58926
baf5a3c28f0c
proper import of Main: BNF_Least_Fixpoint does not "contain pretty much everything", especially it lacks the 'value' command, which is defined *after* theory List;
wenzelm
parents:
58860
diff
changeset
|
2 |
imports Main |
15136 | 3 |
begin |
8745 | 4 |
|
67406 | 5 |
text\<open>\noindent |
69505 | 6 |
HOL already has a predefined theory of lists called \<open>List\<close> --- |
7 |
\<open>ToyList\<close> is merely a small fragment of it chosen as an example. |
|
58926
baf5a3c28f0c
proper import of Main: BNF_Least_Fixpoint does not "contain pretty much everything", especially it lacks the 'value' command, which is defined *after* theory List;
wenzelm
parents:
58860
diff
changeset
|
8 |
To avoid some ambiguities caused by defining lists twice, we manipulate |
69597 | 9 |
the concrete syntax and name space of theory \<^theory>\<open>Main\<close> as follows. |
67406 | 10 |
\<close> |
8745 | 11 |
|
81136 | 12 |
unbundle no list_syntax |
13 |
no_notation append (infixr "@" 65) |
|
58926
baf5a3c28f0c
proper import of Main: BNF_Least_Fixpoint does not "contain pretty much everything", especially it lacks the 'value' command, which is defined *after* theory List;
wenzelm
parents:
58860
diff
changeset
|
14 |
hide_type list |
baf5a3c28f0c
proper import of Main: BNF_Least_Fixpoint does not "contain pretty much everything", especially it lacks the 'value' command, which is defined *after* theory List;
wenzelm
parents:
58860
diff
changeset
|
15 |
hide_const rev |
baf5a3c28f0c
proper import of Main: BNF_Least_Fixpoint does not "contain pretty much everything", especially it lacks the 'value' command, which is defined *after* theory List;
wenzelm
parents:
58860
diff
changeset
|
16 |
|
80983
2cc651d3ce8e
partial revert of d97fdabd9e2b, to build old documentation more reliably;
wenzelm
parents:
80914
diff
changeset
|
17 |
datatype 'a list = Nil ("[]") |
2cc651d3ce8e
partial revert of d97fdabd9e2b, to build old documentation more reliably;
wenzelm
parents:
80914
diff
changeset
|
18 |
| Cons 'a "'a list" (infixr "#" 65) |
8745 | 19 |
|
67406 | 20 |
text\<open>\noindent |
12327 | 21 |
The datatype\index{datatype@\isacommand {datatype} (command)} |
22 |
\tydx{list} introduces two |
|
11428 | 23 |
constructors \cdx{Nil} and \cdx{Cons}, the |
9541 | 24 |
empty~list and the operator that adds an element to the front of a list. For |
9792 | 25 |
example, the term \isa{Cons True (Cons False Nil)} is a value of |
69597 | 26 |
type \<^typ>\<open>bool list\<close>, namely the list with the elements \<^term>\<open>True\<close> and |
27 |
\<^term>\<open>False\<close>. Because this notation quickly becomes unwieldy, the |
|
8745 | 28 |
datatype declaration is annotated with an alternative syntax: instead of |
9792 | 29 |
@{term[source]Nil} and \isa{Cons x xs} we can write |
69597 | 30 |
\<^term>\<open>[]\<close>\index{$HOL2list@\isa{[]}|bold} and |
31 |
\<^term>\<open>x # xs\<close>\index{$HOL2list@\isa{\#}|bold}. In fact, this |
|
11450 | 32 |
alternative syntax is the familiar one. Thus the list \isa{Cons True |
69597 | 33 |
(Cons False Nil)} becomes \<^term>\<open>True # False # []\<close>. The annotation |
11428 | 34 |
\isacommand{infixr}\index{infixr@\isacommand{infixr} (annotation)} |
69505 | 35 |
means that \<open>#\<close> associates to |
69597 | 36 |
the right: the term \<^term>\<open>x # y # z\<close> is read as \<open>x # (y # z)\<close> |
69505 | 37 |
and not as \<open>(x # y) # z\<close>. |
38 |
The \<open>65\<close> is the priority of the infix \<open>#\<close>. |
|
8745 | 39 |
|
40 |
\begin{warn} |
|
13191 | 41 |
Syntax annotations can be powerful, but they are difficult to master and |
11456 | 42 |
are never necessary. You |
69505 | 43 |
could drop them from theory \<open>ToyList\<close> and go back to the identifiers |
27015 | 44 |
@{term[source]Nil} and @{term[source]Cons}. Novices should avoid using |
10795 | 45 |
syntax annotations in their own theories. |
8745 | 46 |
\end{warn} |
69505 | 47 |
Next, two functions \<open>app\<close> and \cdx{rev} are defined recursively, |
27015 | 48 |
in this order, because Isabelle insists on definition before use: |
67406 | 49 |
\<close> |
8745 | 50 |
|
80983
2cc651d3ce8e
partial revert of d97fdabd9e2b, to build old documentation more reliably;
wenzelm
parents:
80914
diff
changeset
|
51 |
primrec app :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where |
27015 | 52 |
"[] @ ys = ys" | |
53 |
"(x # xs) @ ys = x # (xs @ ys)" |
|
54 |
||
55 |
primrec rev :: "'a list \<Rightarrow> 'a list" where |
|
56 |
"rev [] = []" | |
|
57 |
"rev (x # xs) = (rev xs) @ (x # [])" |
|
8745 | 58 |
|
67406 | 59 |
text\<open>\noindent |
27015 | 60 |
Each function definition is of the form |
61 |
\begin{center} |
|
69505 | 62 |
\isacommand{primrec} \textit{name} \<open>::\<close> \textit{type} \textit{(optional syntax)} \isakeyword{where} \textit{equations} |
27015 | 63 |
\end{center} |
69505 | 64 |
The equations must be separated by \<open>|\<close>. |
27015 | 65 |
% |
69505 | 66 |
Function \<open>app\<close> is annotated with concrete syntax. Instead of the |
67 |
prefix syntax \<open>app xs ys\<close> the infix |
|
69597 | 68 |
\<^term>\<open>xs @ ys\<close>\index{$HOL2list@\isa{\at}|bold} becomes the preferred |
27015 | 69 |
form. |
8745 | 70 |
|
27015 | 71 |
\index{*rev (constant)|(}\index{append function|(} |
69597 | 72 |
The equations for \<open>app\<close> and \<^term>\<open>rev\<close> hardly need comments: |
73 |
\<open>app\<close> appends two lists and \<^term>\<open>rev\<close> reverses a list. The |
|
11428 | 74 |
keyword \commdx{primrec} indicates that the recursion is |
10790 | 75 |
of a particularly primitive kind where each recursive call peels off a datatype |
8771 | 76 |
constructor from one of the arguments. Thus the |
10654 | 77 |
recursion always terminates, i.e.\ the function is \textbf{total}. |
11428 | 78 |
\index{functions!total} |
8745 | 79 |
|
80 |
The termination requirement is absolutely essential in HOL, a logic of total |
|
81 |
functions. If we were to drop it, inconsistencies would quickly arise: the |
|
82 |
``definition'' $f(n) = f(n)+1$ immediately leads to $0 = 1$ by subtracting |
|
83 |
$f(n)$ on both sides. |
|
84 |
% However, this is a subtle issue that we cannot discuss here further. |
|
85 |
||
86 |
\begin{warn} |
|
11456 | 87 |
As we have indicated, the requirement for total functions is an essential characteristic of HOL\@. It is only |
8745 | 88 |
because of totality that reasoning in HOL is comparatively easy. More |
11456 | 89 |
generally, the philosophy in HOL is to refrain from asserting arbitrary axioms (such as |
8745 | 90 |
function definitions whose totality has not been proved) because they |
91 |
quickly lead to inconsistencies. Instead, fixed constructs for introducing |
|
92 |
types and functions are offered (such as \isacommand{datatype} and |
|
93 |
\isacommand{primrec}) which are guaranteed to preserve consistency. |
|
94 |
\end{warn} |
|
95 |
||
11456 | 96 |
\index{syntax}% |
8745 | 97 |
A remark about syntax. The textual definition of a theory follows a fixed |
10971 | 98 |
syntax with keywords like \isacommand{datatype} and \isacommand{end}. |
99 |
% (see Fig.~\ref{fig:keywords} in Appendix~\ref{sec:Appendix} for a full list). |
|
8745 | 100 |
Embedded in this syntax are the types and formulae of HOL, whose syntax is |
12631 | 101 |
extensible (see \S\ref{sec:concrete-syntax}), e.g.\ by new user-defined infix operators. |
10971 | 102 |
To distinguish the two levels, everything |
8745 | 103 |
HOL-specific (terms and types) should be enclosed in |
104 |
\texttt{"}\dots\texttt{"}. |
|
105 |
To lessen this burden, quotation marks around a single identifier can be |
|
27015 | 106 |
dropped, unless the identifier happens to be a keyword, for example |
107 |
\isa{"end"}. |
|
8745 | 108 |
When Isabelle prints a syntax error message, it refers to the HOL syntax as |
11456 | 109 |
the \textbf{inner syntax} and the enclosing theory language as the \textbf{outer syntax}. |
8745 | 110 |
|
74887 | 111 |
Comments\index{comment} must be in enclosed in \texttt{(*}and\texttt{*)}. |
38430
254a021ed66e
tuned text about "value" and added note on comments.
nipkow
parents:
27015
diff
changeset
|
112 |
|
25342 | 113 |
\section{Evaluation} |
114 |
\index{evaluation} |
|
115 |
||
116 |
Assuming you have processed the declarations and definitions of |
|
117 |
\texttt{ToyList} presented so far, you may want to test your |
|
118 |
functions by running them. For example, what is the value of |
|
69597 | 119 |
\<^term>\<open>rev(True#False#[])\<close>? Command |
67406 | 120 |
\<close> |
25342 | 121 |
|
122 |
value "rev (True # False # [])" |
|
123 |
||
69597 | 124 |
text\<open>\noindent yields the correct result \<^term>\<open>False # True # []\<close>. |
25342 | 125 |
But we can go beyond mere functional programming and evaluate terms with |
67406 | 126 |
variables in them, executing functions symbolically:\<close> |
25342 | 127 |
|
38430
254a021ed66e
tuned text about "value" and added note on comments.
nipkow
parents:
27015
diff
changeset
|
128 |
value "rev (a # b # c # [])" |
25342 | 129 |
|
69597 | 130 |
text\<open>\noindent yields \<^term>\<open>c # b # a # []\<close>. |
38432
439f50a241c1
Using type real does not require a separate logic now.
nipkow
parents:
38430
diff
changeset
|
131 |
|
10885 | 132 |
\section{An Introductory Proof} |
8745 | 133 |
\label{sec:intro-proof} |
134 |
||
25342 | 135 |
Having convinced ourselves (as well as one can by testing) that our |
136 |
definitions capture our intentions, we are ready to prove a few simple |
|
16360 | 137 |
theorems. This will illustrate not just the basic proof commands but |
138 |
also the typical proof process. |
|
8745 | 139 |
|
11457 | 140 |
\subsubsection*{Main Goal.} |
8745 | 141 |
|
142 |
Our goal is to show that reversing a list twice produces the original |
|
11456 | 143 |
list. |
67406 | 144 |
\<close> |
8745 | 145 |
|
58860 | 146 |
theorem rev_rev [simp]: "rev(rev xs) = xs" |
8745 | 147 |
|
67406 | 148 |
txt\<open>\index{theorem@\isacommand {theorem} (command)|bold}% |
10795 | 149 |
\noindent |
11456 | 150 |
This \isacommand{theorem} command does several things: |
8745 | 151 |
\begin{itemize} |
152 |
\item |
|
69597 | 153 |
It establishes a new theorem to be proved, namely \<^prop>\<open>rev(rev xs) = xs\<close>. |
8745 | 154 |
\item |
69505 | 155 |
It gives that theorem the name \<open>rev_rev\<close>, for later reference. |
8745 | 156 |
\item |
11456 | 157 |
It tells Isabelle (via the bracketed attribute \attrdx{simp}) to take the eventual theorem as a simplification rule: future proofs involving |
69597 | 158 |
simplification will replace occurrences of \<^term>\<open>rev(rev xs)\<close> by |
159 |
\<^term>\<open>xs\<close>. |
|
11457 | 160 |
\end{itemize} |
8745 | 161 |
The name and the simplification attribute are optional. |
12332 | 162 |
Isabelle's response is to print the initial proof state consisting |
163 |
of some header information (like how many subgoals there are) followed by |
|
13868 | 164 |
@{subgoals[display,indent=0]} |
12332 | 165 |
For compactness reasons we omit the header in this tutorial. |
166 |
Until we have finished a proof, the \rmindex{proof state} proper |
|
167 |
always looks like this: |
|
9723 | 168 |
\begin{isabelle} |
8745 | 169 |
~1.~$G\sb{1}$\isanewline |
170 |
~~\vdots~~\isanewline |
|
171 |
~$n$.~$G\sb{n}$ |
|
9723 | 172 |
\end{isabelle} |
13868 | 173 |
The numbered lines contain the subgoals $G\sb{1}$, \dots, $G\sb{n}$ |
174 |
that we need to prove to establish the main goal.\index{subgoals} |
|
175 |
Initially there is only one subgoal, which is identical with the |
|
176 |
main goal. (If you always want to see the main goal as well, |
|
177 |
set the flag \isa{Proof.show_main_goal}\index{*show_main_goal (flag)} |
|
178 |
--- this flag used to be set by default.) |
|
8745 | 179 |
|
69597 | 180 |
Let us now get back to \<^prop>\<open>rev(rev xs) = xs\<close>. Properties of recursively |
8745 | 181 |
defined functions are best established by induction. In this case there is |
69597 | 182 |
nothing obvious except induction on \<^term>\<open>xs\<close>: |
67406 | 183 |
\<close> |
8745 | 184 |
|
58860 | 185 |
apply(induct_tac xs) |
8745 | 186 |
|
67406 | 187 |
txt\<open>\noindent\index{*induct_tac (method)}% |
69597 | 188 |
This tells Isabelle to perform induction on variable \<^term>\<open>xs\<close>. The suffix |
189 |
\<^term>\<open>tac\<close> stands for \textbf{tactic},\index{tactics} |
|
11428 | 190 |
a synonym for ``theorem proving function''. |
8745 | 191 |
By default, induction acts on the first subgoal. The new proof state contains |
9792 | 192 |
two subgoals, namely the base case (@{term[source]Nil}) and the induction step |
193 |
(@{term[source]Cons}): |
|
10971 | 194 |
@{subgoals[display,indent=0,margin=65]} |
8745 | 195 |
|
11456 | 196 |
The induction step is an example of the general format of a subgoal:\index{subgoals} |
9723 | 197 |
\begin{isabelle} |
12327 | 198 |
~$i$.~{\isasymAnd}$x\sb{1}$~\dots$x\sb{n}$.~{\it assumptions}~{\isasymLongrightarrow}~{\it conclusion} |
10328 | 199 |
\end{isabelle}\index{$IsaAnd@\isasymAnd|bold} |
8745 | 200 |
The prefix of bound variables \isasymAnd$x\sb{1}$~\dots~$x\sb{n}$ can be |
201 |
ignored most of the time, or simply treated as a list of variables local to |
|
10302 | 202 |
this subgoal. Their deeper significance is explained in Chapter~\ref{chap:rules}. |
11456 | 203 |
The {\it assumptions}\index{assumptions!of subgoal} |
204 |
are the local assumptions for this subgoal and {\it |
|
205 |
conclusion}\index{conclusion!of subgoal} is the actual proposition to be proved. |
|
206 |
Typical proof steps |
|
207 |
that add new assumptions are induction and case distinction. In our example |
|
69597 | 208 |
the only assumption is the induction hypothesis \<^term>\<open>rev (rev list) = |
209 |
list\<close>, where \<^term>\<open>list\<close> is a variable name chosen by Isabelle. If there |
|
8745 | 210 |
are multiple assumptions, they are enclosed in the bracket pair |
211 |
\indexboldpos{\isasymlbrakk}{$Isabrl} and |
|
212 |
\indexboldpos{\isasymrbrakk}{$Isabrr} and separated by semicolons. |
|
213 |
||
214 |
Let us try to solve both goals automatically: |
|
67406 | 215 |
\<close> |
8745 | 216 |
|
58860 | 217 |
apply(auto) |
8745 | 218 |
|
67406 | 219 |
txt\<open>\noindent |
8745 | 220 |
This command tells Isabelle to apply a proof strategy called |
69505 | 221 |
\<open>auto\<close> to all subgoals. Essentially, \<open>auto\<close> tries to |
10978 | 222 |
simplify the subgoals. In our case, subgoal~1 is solved completely (thanks |
69597 | 223 |
to the equation \<^prop>\<open>rev [] = []\<close>) and disappears; the simplified version |
8745 | 224 |
of subgoal~2 becomes the new subgoal~1: |
10971 | 225 |
@{subgoals[display,indent=0,margin=70]} |
8745 | 226 |
In order to simplify this subgoal further, a lemma suggests itself. |
67406 | 227 |
\<close> |
8745 | 228 |
(*<*) |
229 |
oops |
|
230 |
(*>*) |
|
231 |
||
67406 | 232 |
subsubsection\<open>First Lemma\<close> |
9723 | 233 |
|
67406 | 234 |
text\<open> |
11428 | 235 |
\indexbold{abandoning a proof}\indexbold{proofs!abandoning} |
236 |
After abandoning the above proof attempt (at the shell level type |
|
237 |
\commdx{oops}) we start a new proof: |
|
67406 | 238 |
\<close> |
8745 | 239 |
|
58860 | 240 |
lemma rev_app [simp]: "rev(xs @ ys) = (rev ys) @ (rev xs)" |
8745 | 241 |
|
67406 | 242 |
txt\<open>\noindent The keywords \commdx{theorem} and |
11428 | 243 |
\commdx{lemma} are interchangeable and merely indicate |
10971 | 244 |
the importance we attach to a proposition. Therefore we use the words |
11428 | 245 |
\emph{theorem} and \emph{lemma} pretty much interchangeably, too. |
8745 | 246 |
|
69597 | 247 |
There are two variables that we could induct on: \<^term>\<open>xs\<close> and |
248 |
\<^term>\<open>ys\<close>. Because \<open>@\<close> is defined by recursion on |
|
249 |
the first argument, \<^term>\<open>xs\<close> is the correct one: |
|
67406 | 250 |
\<close> |
8745 | 251 |
|
58860 | 252 |
apply(induct_tac xs) |
8745 | 253 |
|
67406 | 254 |
txt\<open>\noindent |
8745 | 255 |
This time not even the base case is solved automatically: |
67406 | 256 |
\<close> |
8745 | 257 |
|
58860 | 258 |
apply(auto) |
8745 | 259 |
|
67406 | 260 |
txt\<open> |
10362 | 261 |
@{subgoals[display,indent=0,goals_limit=1]} |
262 |
Again, we need to abandon this proof attempt and prove another simple lemma |
|
263 |
first. In the future the step of abandoning an incomplete proof before |
|
264 |
embarking on the proof of a lemma usually remains implicit. |
|
67406 | 265 |
\<close> |
8745 | 266 |
(*<*) |
267 |
oops |
|
268 |
(*>*) |
|
269 |
||
67406 | 270 |
subsubsection\<open>Second Lemma\<close> |
9723 | 271 |
|
67406 | 272 |
text\<open> |
11456 | 273 |
We again try the canonical proof procedure: |
67406 | 274 |
\<close> |
8745 | 275 |
|
58860 | 276 |
lemma app_Nil2 [simp]: "xs @ [] = xs" |
277 |
apply(induct_tac xs) |
|
278 |
apply(auto) |
|
8745 | 279 |
|
67406 | 280 |
txt\<open> |
8745 | 281 |
\noindent |
69505 | 282 |
It works, yielding the desired message \<open>No subgoals!\<close>: |
10362 | 283 |
@{goals[display,indent=0]} |
8745 | 284 |
We still need to confirm that the proof is now finished: |
67406 | 285 |
\<close> |
8745 | 286 |
|
10171 | 287 |
done |
8745 | 288 |
|
67406 | 289 |
text\<open>\noindent |
11428 | 290 |
As a result of that final \commdx{done}, Isabelle associates the lemma just proved |
10171 | 291 |
with its name. In this tutorial, we sometimes omit to show that final \isacommand{done} |
292 |
if it is obvious from the context that the proof is finished. |
|
293 |
||
294 |
% Instead of \isacommand{apply} followed by a dot, you can simply write |
|
295 |
% \isacommand{by}\indexbold{by}, which we do most of the time. |
|
10971 | 296 |
Notice that in lemma @{thm[source]app_Nil2}, |
69597 | 297 |
as printed out after the final \isacommand{done}, the free variable \<^term>\<open>xs\<close> has been |
69505 | 298 |
replaced by the unknown \<open>?xs\<close>, just as explained in |
9792 | 299 |
\S\ref{sec:variables}. |
8745 | 300 |
|
301 |
Going back to the proof of the first lemma |
|
67406 | 302 |
\<close> |
8745 | 303 |
|
58860 | 304 |
lemma rev_app [simp]: "rev(xs @ ys) = (rev ys) @ (rev xs)" |
305 |
apply(induct_tac xs) |
|
306 |
apply(auto) |
|
8745 | 307 |
|
67406 | 308 |
txt\<open> |
8745 | 309 |
\noindent |
69505 | 310 |
we find that this time \<open>auto\<close> solves the base case, but the |
8745 | 311 |
induction step merely simplifies to |
10362 | 312 |
@{subgoals[display,indent=0,goals_limit=1]} |
69505 | 313 |
Now we need to remember that \<open>@\<close> associates to the right, and that |
314 |
\<open>#\<close> and \<open>@\<close> have the same priority (namely the \<open>65\<close> |
|
8745 | 315 |
in their \isacommand{infixr} annotation). Thus the conclusion really is |
9723 | 316 |
\begin{isabelle} |
9792 | 317 |
~~~~~(rev~ys~@~rev~list)~@~(a~\#~[])~=~rev~ys~@~(rev~list~@~(a~\#~[])) |
9723 | 318 |
\end{isabelle} |
69505 | 319 |
and the missing lemma is associativity of \<open>@\<close>. |
67406 | 320 |
\<close> |
9723 | 321 |
(*<*)oops(*>*) |
8745 | 322 |
|
67406 | 323 |
subsubsection\<open>Third Lemma\<close> |
8745 | 324 |
|
67406 | 325 |
text\<open> |
11456 | 326 |
Abandoning the previous attempt, the canonical proof procedure |
327 |
succeeds without further ado. |
|
67406 | 328 |
\<close> |
8745 | 329 |
|
58860 | 330 |
lemma app_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
331 |
apply(induct_tac xs) |
|
332 |
apply(auto) |
|
10171 | 333 |
done |
8745 | 334 |
|
67406 | 335 |
text\<open> |
8745 | 336 |
\noindent |
11456 | 337 |
Now we can prove the first lemma: |
67406 | 338 |
\<close> |
8745 | 339 |
|
58860 | 340 |
lemma rev_app [simp]: "rev(xs @ ys) = (rev ys) @ (rev xs)" |
341 |
apply(induct_tac xs) |
|
342 |
apply(auto) |
|
10171 | 343 |
done |
8745 | 344 |
|
67406 | 345 |
text\<open>\noindent |
11456 | 346 |
Finally, we prove our main theorem: |
67406 | 347 |
\<close> |
8745 | 348 |
|
58860 | 349 |
theorem rev_rev [simp]: "rev(rev xs) = xs" |
350 |
apply(induct_tac xs) |
|
351 |
apply(auto) |
|
10171 | 352 |
done |
8745 | 353 |
|
67406 | 354 |
text\<open>\noindent |
11456 | 355 |
The final \commdx{end} tells Isabelle to close the current theory because |
356 |
we are finished with its development:% |
|
357 |
\index{*rev (constant)|)}\index{append function|)} |
|
67406 | 358 |
\<close> |
8745 | 359 |
|
360 |
end |