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