doc-src/TutorialI/IsarOverview/Isar/Logic.thy
changeset 13322 3323ecc0b97c
parent 13317 bb74918cc0dd
child 13326 900f220c800d
equal deleted inserted replaced
13321:70a5d5fc081a 13322:3323ecc0b97c
     1 (*<*)theory Logic = Main:(*>*)
     1 (*<*)theory Logic = Main:(*>*)
     2 text{* We begin by looking at a simplified grammar for Isar proofs
     2 text{* We begin by looking at a simplified grammar for Isar proofs
     3 where paranthesis are used for grouping and $^?$ indicates an optional item:
     3 where parentheses are used for grouping and $^?$ indicates an optional item:
     4 \begin{center}
     4 \begin{center}
     5 \begin{tabular}{lrl}
     5 \begin{tabular}{lrl}
     6 \emph{proof} & ::= & \isakeyword{proof} \emph{method}$^?$
     6 \emph{proof} & ::= & \isakeyword{proof} \emph{method}$^?$
     7                      \emph{statement}*
     7                      \emph{statement}*
     8                      \isakeyword{qed} \\
     8                      \isakeyword{qed} \\
     9                  &$\mid$& \isakeyword{by} \emph{method}\\[1ex]
     9                  &$\mid$& \isakeyword{by} \emph{method}\\[1ex]
    10 \emph{statement} &::= & \isakeyword{fix} \emph{variables} \\
    10 \emph{statement} &::= & \isakeyword{fix} \emph{variables} \\
    11              &$\mid$& \isakeyword{assume} \emph{proposition}
    11              &$\mid$& \isakeyword{assume} \emph{propositions} \\
    12                       (\isakeyword{and} \emph{proposition})*\\
    12              &$\mid$& (\isakeyword{from} \emph{facts})$^?$ 
    13              &$\mid$& (\isakeyword{from} \emph{label}$^*$ $\mid$
       
    14                        \isakeyword{then})$^?$ 
       
    15                     (\isakeyword{show} $\mid$ \isakeyword{have})
    13                     (\isakeyword{show} $\mid$ \isakeyword{have})
    16                       \emph{string} \emph{proof} \\[1ex]
    14                       \emph{propositions} \emph{proof} \\[1ex]
    17 \emph{proposition} &::=& (\emph{label}{\bf:})$^?$ \emph{string}
    15 \emph{proposition} &::=& (\emph{label}{\bf:})$^?$ \emph{string} \\[1ex]
       
    16 \emph{fact} &::=& \emph{label}
    18 \end{tabular}
    17 \end{tabular}
    19 \end{center}
    18 \end{center}
    20 
    19 
    21 This is a typical proof skeleton:
    20 This is a typical proof skeleton:
    22 \begin{center}
    21 \begin{center}
    30 \isakeyword{qed}
    29 \isakeyword{qed}
    31 \end{tabular}
    30 \end{tabular}
    32 \end{center}
    31 \end{center}
    33 It proves \emph{the-assm}~@{text"\<Longrightarrow>"}~\emph{the-concl}.
    32 It proves \emph{the-assm}~@{text"\<Longrightarrow>"}~\emph{the-concl}.
    34 Text starting with ``--'' is a comment.
    33 Text starting with ``--'' is a comment.
       
    34 
       
    35 Note that propositions in \isakeyword{assume} may but need not be
       
    36 separated by \isakeyword{and} whose purpose is to structure the
       
    37 assumptions into possibly named blocks, for example
       
    38 \begin{center}
       
    39 \isakeyword{assume} @{text"A:"} $A_1$ $A_2$ \isakeyword{and} @{text"B:"} $A_3$
       
    40 \isakeyword{and} $A_4$
       
    41 \end{center}
       
    42 Here label @{text A} refers to the list of propositions $A_1$ $A_2$ and
       
    43 label @{text B} to $A_3$.
    35 *}
    44 *}
    36 
    45 
    37 section{*Logic*}
    46 section{*Logic*}
    38 
    47 
    39 subsection{*Propositional logic*}
    48 subsection{*Propositional logic*}
    65   assume a: "A"
    74   assume a: "A"
    66   show "A" by(rule a)
    75   show "A" by(rule a)
    67 qed
    76 qed
    68 
    77 
    69 text{* Trivial proofs, in particular those by assumption, should be trivial
    78 text{* Trivial proofs, in particular those by assumption, should be trivial
    70 to perform. Method ``.'' does just that (and a bit more --- see later). Thus
    79 to perform. Proof ``.'' does just that (and a bit more). Thus
    71 naming of assumptions is often superfluous: *}
    80 naming of assumptions is often superfluous: *}
    72 lemma "A \<longrightarrow> A"
    81 lemma "A \<longrightarrow> A"
    73 proof
    82 proof
    74   assume "A"
    83   assume "A"
    75   show "A" .
    84   show "A" .
    86 text{*\noindent A drawback of these implicit proofs by assumption is that it
    95 text{*\noindent A drawback of these implicit proofs by assumption is that it
    87 is no longer obvious where an assumption is used.
    96 is no longer obvious where an assumption is used.
    88 
    97 
    89 Proofs of the form \isakeyword{by}@{text"(rule"}~\emph{name}@{text")"} can be
    98 Proofs of the form \isakeyword{by}@{text"(rule"}~\emph{name}@{text")"} can be
    90 abbreviated to ``..''
    99 abbreviated to ``..''
    91 if \emph{name} is one of the predefined introduction rules:
   100 if \emph{name} refers to one of the predefined introduction rules:
    92 *}
   101 *}
    93 
   102 
    94 lemma "A \<longrightarrow> A \<and> A"
   103 lemma "A \<longrightarrow> A \<and> A"
    95 proof
   104 proof
    96   assume A
   105   assume A
   116     show ?thesis ..
   125     show ?thesis ..
   117   qed
   126   qed
   118 qed
   127 qed
   119 text{*\noindent Note that the term @{text"?thesis"} always stands for the
   128 text{*\noindent Note that the term @{text"?thesis"} always stands for the
   120 ``current goal'', i.e.\ the enclosing \isakeyword{show} (or
   129 ``current goal'', i.e.\ the enclosing \isakeyword{show} (or
   121 \isakeyword{have}).
   130 \isakeyword{have}) statement.
   122 
   131 
   123 This is too much proof text. Elimination rules should be selected
   132 This is too much proof text. Elimination rules should be selected
   124 automatically based on their major premise, the formula or rather connective
   133 automatically based on their major premise, the formula or rather connective
   125 to be eliminated. In Isar they are triggered by propositions being fed
   134 to be eliminated. In Isar they are triggered by propositions being fed
   126 \emph{into} a proof block. Syntax:
   135 \emph{into} a proof block. Syntax:
   159     assume A and B
   168     assume A and B
   160     show ?thesis ..
   169     show ?thesis ..
   161   qed
   170   qed
   162 qed
   171 qed
   163 
   172 
   164 text{*\noindent
   173 text{*\noindent Because of the frequency of \isakeyword{from}~@{text
   165 A final simplification: \isakeyword{from}~@{text this} can be
   174 this} Isar provides two abbreviations:
   166 abbreviated to \isakeyword{thus}.
   175 \begin{center}
       
   176 \begin{tabular}{rcl}
       
   177 \isakeyword{then} &=& \isakeyword{from} @{text this} \\
       
   178 \isakeyword{thus} &=& \isakeyword{then} \isakeyword{show}
       
   179 \end{tabular}
       
   180 \end{center}
   167 \medskip
   181 \medskip
   168 
   182 
   169 Here is an alternative proof that operates purely by forward reasoning: *}
   183 Here is an alternative proof that operates purely by forward reasoning: *}
   170 lemma "A \<and> B \<longrightarrow> B \<and> A"
   184 lemma "A \<and> B \<longrightarrow> B \<and> A"
   171 proof
   185 proof
   173   from ab have a: A ..
   187   from ab have a: A ..
   174   from ab have b: B ..
   188   from ab have b: B ..
   175   from b a show "B \<and> A" ..
   189   from b a show "B \<and> A" ..
   176 qed
   190 qed
   177 text{*\noindent
   191 text{*\noindent
   178 It is worth examining this text in detail because it exhibits a number of new features.
   192 It is worth examining this text in detail because it exhibits a number of new concepts.
   179 
   193 
   180 For a start, this is the first time we have proved intermediate propositions
   194 For a start, this is the first time we have proved intermediate propositions
   181 (\isakeyword{have}) on the way to the final \isakeyword{show}. This is the
   195 (\isakeyword{have}) on the way to the final \isakeyword{show}. This is the
   182 norm in any nontrival proof where one cannot bridge the gap between the
   196 norm in any nontrival proof where one cannot bridge the gap between the
   183 assumptions and the conclusion in one step. Both \isakeyword{have}s above are
   197 assumptions and the conclusion in one step. Both \isakeyword{have}s above are
   185 \isakeyword{from}~@{text ab}.
   199 \isakeyword{from}~@{text ab}.
   186 
   200 
   187 The \isakeyword{show} command illustrates two things:
   201 The \isakeyword{show} command illustrates two things:
   188 \begin{itemize}
   202 \begin{itemize}
   189 \item \isakeyword{from} can be followed by any number of facts.
   203 \item \isakeyword{from} can be followed by any number of facts.
   190 Given \isakeyword{from}~@{text f}$_1$~\dots~@{text f}$_n$, \isakeyword{show}
   204 Given \isakeyword{from}~@{text f}$_1$~\dots~@{text f}$_n$, the
       
   205 \isakeyword{proof} step after \isakeyword{show}
   191 tries to find an elimination rule whose first $n$ premises can be proved
   206 tries to find an elimination rule whose first $n$ premises can be proved
   192 by the given facts in the given order.
   207 by the given facts in the given order.
   193 \item If there is no matching elimination rule, introduction rules are tried,
   208 \item If there is no matching elimination rule, introduction rules are tried,
   194 again using the facts to prove the premises.
   209 again using the facts to prove the premises.
   195 \end{itemize}
   210 \end{itemize}
   201 application of introduction and elimination rules but applies to explicit
   216 application of introduction and elimination rules but applies to explicit
   202 application of rules as well. Thus you could replace the final ``..'' above
   217 application of rules as well. Thus you could replace the final ``..'' above
   203 with \isakeyword{by}@{text"(rule conjI)"}. 
   218 with \isakeyword{by}@{text"(rule conjI)"}. 
   204 
   219 
   205 Note that Isar's predefined introduction and elimination rules include all the
   220 Note that Isar's predefined introduction and elimination rules include all the
   206 usual natural deduction rules for propositional logic. We conclude this
   221 usual natural deduction rules. We conclude this
   207 section with an extended example --- which rules are used implicitly where?
   222 section with an extended example --- which rules are used implicitly where?
   208 Rule @{thm[source]ccontr} is @{thm ccontr[no_vars]}.
   223 Rule @{thm[source]ccontr} is @{thm ccontr[no_vars]}.
   209 *}
   224 *}
   210 
   225 
   211 lemma "\<not>(A \<and> B) \<longrightarrow> \<not>A \<or> \<not>B"
   226 lemma "\<not>(A \<and> B) \<longrightarrow> \<not>A \<or> \<not>B"
   245 subsection{*Avoiding duplication*}
   260 subsection{*Avoiding duplication*}
   246 
   261 
   247 text{* So far our examples have been a bit unnatural: normally we want to
   262 text{* So far our examples have been a bit unnatural: normally we want to
   248 prove rules expressed with @{text"\<Longrightarrow>"}, not @{text"\<longrightarrow>"}. Here is an example:
   263 prove rules expressed with @{text"\<Longrightarrow>"}, not @{text"\<longrightarrow>"}. Here is an example:
   249 *}
   264 *}
   250 lemma "(A \<Longrightarrow> False) \<Longrightarrow> \<not> A"
   265 lemma "A \<and> B \<Longrightarrow> B \<and> A"
   251 proof
   266 proof
   252   assume "A \<Longrightarrow> False" "A"
   267   assume "A \<and> B" thus "B" ..
   253   thus False .
   268 next
       
   269   assume "A \<and> B" thus "A" ..
   254 qed
   270 qed
   255 text{*\noindent The \isakeyword{proof} always works on the conclusion,
   271 text{*\noindent The \isakeyword{proof} always works on the conclusion,
   256 @{prop"\<not>A"} in our case, thus selecting $\neg$-introduction. Hence we can
   272 @{prop"B \<and> A"} in our case, thus selecting $\land$-introduction. Hence
   257 additionally assume @{prop"A"}. Why does ``.'' prove @{prop False}? Because
   273 we must show @{prop B} and @{prop A}; both are proved by
   258 ``.'' tries any of the assumptions, including proper rules (here: @{prop"A \<Longrightarrow>
   274 $\land$-elimination.
   259 False"}), such that all of its premises can be solved directly by some other
       
   260 assumption (here: @{prop A}).
       
   261 
   275 
   262 This is all very well as long as formulae are small. Let us now look at some
   276 This is all very well as long as formulae are small. Let us now look at some
   263 devices to avoid repeating (possibly large) formulae. A very general method
   277 devices to avoid repeating (possibly large) formulae. A very general method
   264 is pattern matching: *}
   278 is pattern matching: *}
   265 
   279 
   266 lemma "(large_formula \<Longrightarrow> False) \<Longrightarrow> \<not> large_formula"
   280 lemma "large_A \<and> large_B \<Longrightarrow> large_B \<and> large_A"
   267       (is "(?P \<Longrightarrow> _) \<Longrightarrow> _")
   281       (is "?AB \<Longrightarrow> ?B \<and> ?A")
   268 proof
   282 proof
   269   assume "?P \<Longrightarrow> False" ?P
   283   assume ?AB thus ?B ..
   270   thus False .
   284 next
       
   285   assume ?AB thus ?A ..
   271 qed
   286 qed
   272 text{*\noindent Any formula may be followed by
   287 text{*\noindent Any formula may be followed by
   273 @{text"("}\isakeyword{is}~\emph{pattern}@{text")"} which causes the pattern
   288 @{text"("}\isakeyword{is}~\emph{pattern}@{text")"} which causes the pattern
   274 to be matched against the formula, instantiating the @{text"?"}-variables in
   289 to be matched against the formula, instantiating the @{text"?"}-variables in
   275 the pattern. Subsequent uses of these variables in other terms simply causes
   290 the pattern. Subsequent uses of these variables in other terms simply causes
   276 them to be replaced by the terms they stand for.
   291 them to be replaced by the terms they stand for.
   277 
   292 
   278 We can simplify things even more by stating the theorem by means of the
   293 We can simplify things even more by stating the theorem by means of the
   279 \isakeyword{assumes} and \isakeyword{shows} primitives which allow direct
   294 \isakeyword{assumes} and \isakeyword{shows} elements which allow direct
   280 naming of assumptions: *}
   295 naming of assumptions: *}
   281 
   296 
   282 lemma assumes A: "large_formula \<Longrightarrow> False"
   297 lemma assumes AB: "large_A \<and> large_B"
   283   shows "\<not> large_formula" (is "\<not> ?P")
   298   shows "large_B \<and> large_A" (is "?B \<and> ?A")
   284 proof
   299 proof
   285   assume ?P
   300   from AB show ?B ..
   286   with A show False .
   301 next
   287 qed
   302   from AB show ?A ..
   288 text{*\noindent Here we have used the abbreviation
   303 qed
       
   304 text{*\noindent Note the difference between @{text ?AB}, a term, and
       
   305 @{text AB}, a fact.
       
   306 
       
   307 Finally we want to start the proof with $\land$-elimination so we
       
   308 don't have to perform it twice, as above. Here is a slick way to
       
   309 achieve this: *}
       
   310 
       
   311 lemma assumes AB: "large_A \<and> large_B"
       
   312   shows "large_B \<and> large_A" (is "?B \<and> ?A")
       
   313 using AB
       
   314 proof
       
   315   assume ?A and ?B show ?thesis ..
       
   316 qed
       
   317 text{*\noindent Command \isakeyword{using} can appear before a proof
       
   318 and adds further facts to those piped into the proof. Here @{text AB}
       
   319 is the only such fact and it triggers $\land$-elimination. Another
       
   320 frequent usage is as follows:
   289 \begin{center}
   321 \begin{center}
   290 \isakeyword{with}~\emph{facts} \quad = \quad
   322 \isakeyword{from} \emph{important facts}
   291 \isakeyword{from}~\emph{facts} \isakeyword{and} @{text this}
   323 \isakeyword{show} \emph{something}
       
   324 \isakeyword{using} \emph{minor facts}
   292 \end{center}
   325 \end{center}
       
   326 \medskip
   293 
   327 
   294 Sometimes it is necessary to supress the implicit application of rules in a
   328 Sometimes it is necessary to supress the implicit application of rules in a
   295 \isakeyword{proof}. For example \isakeyword{show}~@{prop"A \<or> B"} would
   329 \isakeyword{proof}. For example \isakeyword{show}~@{prop"A \<or> B"} would
   296 trigger $\lor$-introduction, requiring us to prove @{prop A}. A simple
   330 trigger $\lor$-introduction, requiring us to prove @{prop A}. A simple
   297 ``@{text"-"}'' prevents this \emph{faut pas}: *}
   331 ``@{text"-"}'' prevents this \emph{faut pas}: *}
   303     assume A show ?thesis ..
   337     assume A show ?thesis ..
   304   next
   338   next
   305     assume B show ?thesis ..
   339     assume B show ?thesis ..
   306   qed
   340   qed
   307 qed
   341 qed
   308 
   342 text{*\noindent Could \isakeyword{using} help to eliminate this ``@{text"-"}''? *}
   309 
   343 
   310 subsection{*Predicate calculus*}
   344 subsection{*Predicate calculus*}
   311 
   345 
   312 text{* Command \isakeyword{fix} introduces new local variables into a
   346 text{* Command \isakeyword{fix} introduces new local variables into a
   313 proof. It corresponds to @{text"\<And>"} (the universal quantifier at the
   347 proof. The pair \isakeyword{fix}-\isakeyword{show} corresponds to @{text"\<And>"}
       
   348 (the universal quantifier at the
   314 meta-level) just like \isakeyword{assume}-\isakeyword{show} corresponds to
   349 meta-level) just like \isakeyword{assume}-\isakeyword{show} corresponds to
   315 @{text"\<Longrightarrow>"}. Here is a sample proof, annotated with the rules that are
   350 @{text"\<Longrightarrow>"}. Here is a sample proof, annotated with the rules that are
   316 applied implictly: *}
   351 applied implictly: *}
   317 lemma assumes P: "\<forall>x. P x" shows "\<forall>x. P(f x)"
   352 lemma assumes P: "\<forall>x. P x" shows "\<forall>x. P(f x)"
   318 proof  -- "@{thm[source]allI}: @{thm allI[no_vars]}"
   353 proof  -- "@{thm[source]allI}: @{thm allI[no_vars]}"
   319   fix a
   354   fix a
   320   from P show "P(f a)" ..  --"@{thm[source]allE}: @{thm allE[no_vars]}"
   355   from P show "P(f a)" ..  --"@{thm[source]allE}: @{thm allE[no_vars]}"
   321 qed
   356 qed
   322 text{*\noindent Note that in the proof we have chosen to call the bound
   357 text{*\noindent Note that in the proof we have chosen to call the bound
   323 variable @{term a} instead of @{term x} merely to show that the choice of
   358 variable @{term a} instead of @{term x} merely to show that the choice of
   324 names is irrelevant.
   359 local names is irrelevant.
   325 
   360 
   326 Next we look at @{text"\<exists>"} which is a bit more tricky.
   361 Next we look at @{text"\<exists>"} which is a bit more tricky.
   327 *}
   362 *}
   328 
   363 
   329 lemma assumes Pf: "\<exists>x. P(f x)" shows "\<exists>y. P y"
   364 lemma assumes Pf: "\<exists>x. P(f x)" shows "\<exists>y. P y"
   335     show ?thesis ..  --"@{thm[source]exI}: @{thm exI[no_vars]}"
   370     show ?thesis ..  --"@{thm[source]exI}: @{thm exI[no_vars]}"
   336   qed
   371   qed
   337 qed
   372 qed
   338 text{*\noindent Explicit $\exists$-elimination as seen above can become
   373 text{*\noindent Explicit $\exists$-elimination as seen above can become
   339 cumbersome in practice.  The derived Isar language element
   374 cumbersome in practice.  The derived Isar language element
   340 \isakeyword{obtain} provides a more handsome way to perform generalized
   375 \isakeyword{obtain} provides a more appealing form of generalized
   341 existence reasoning: *}
   376 existence reasoning: *}
   342 
   377 
   343 lemma assumes Pf: "\<exists>x. P(f x)" shows "\<exists>y. P y"
   378 lemma assumes Pf: "\<exists>x. P(f x)" shows "\<exists>y. P y"
   344 proof -
   379 proof -
   345   from Pf obtain a where "P(f a)" ..
   380   from Pf obtain x where "P(f x)" ..
   346   thus "\<exists>y. P y" ..
   381   thus "\<exists>y. P y" ..
   347 qed
   382 qed
   348 text{*\noindent Note how the proof text follows the usual mathematical style
   383 text{*\noindent Note how the proof text follows the usual mathematical style
   349 of concluding $P(x)$ from $\exists x. P(x)$, while carefully introducing $x$
   384 of concluding $P(x)$ from $\exists x. P(x)$, while carefully introducing $x$
   350 as a new local variable.  Technically, \isakeyword{obtain} is similar to
   385 as a new local variable.  Technically, \isakeyword{obtain} is similar to
   351 \isakeyword{fix} and \isakeyword{assume} together with a soundness proof of
   386 \isakeyword{fix} and \isakeyword{assume} together with a soundness proof of
   352 the elimination involved.  Thus it behaves similar to any other forward proof
   387 the elimination involved.
   353 element.
       
   354 
   388 
   355 Here is a proof of a well known tautology which employs another useful
   389 Here is a proof of a well known tautology which employs another useful
   356 abbreviation: \isakeyword{hence} abbreviates \isakeyword{from}~@{text
   390 abbreviation: \isakeyword{hence} abbreviates \isakeyword{from}~@{text
   357 this}~\isakeyword{have}.  Figure out which rule is used where!  *}
   391 this}~\isakeyword{have}.  Figure out which rule is used where!  *}
   358 
   392 
   384       with fy show False by blast
   418       with fy show False by blast
   385     qed
   419     qed
   386   qed
   420   qed
   387 qed
   421 qed
   388 text{*\noindent
   422 text{*\noindent
   389 For a start, the example demonstrates two new language elements:
   423 For a start, the example demonstrates three new language elements:
   390 \begin{itemize}
   424 \begin{itemize}
   391 \item \isakeyword{let} introduces an abbreviation for a term, in our case
   425 \item \isakeyword{let} introduces an abbreviation for a term, in our case
   392 the witness for the claim, because the term occurs multiple times in the proof.
   426 the witness for the claim, because the term occurs multiple times in the proof.
   393 \item Proof by @{text"cases"} starts a proof by cases. Note that it remains
   427 \item Proof by @{text"cases"} starts a proof by cases. Note that it remains
   394 implicit what the two cases are: it is merely expected that the two subproofs
   428 implicit what the two cases are: it is merely expected that the two subproofs
   395 prove @{prop"P \<Longrightarrow> Q"} and @{prop"\<not>P \<Longrightarrow> Q"} for suitable @{term P} and
   429 prove @{prop"P \<Longrightarrow> Q"} and @{prop"\<not>P \<Longrightarrow> Q"} for suitable @{term P} and
   396 @{term Q}.
   430 @{term Q}.
       
   431 \item \isakeyword{with} is an abbreviation:
       
   432 \begin{center}
       
   433 \isakeyword{with}~\emph{facts} \quad = \quad
       
   434 \isakeyword{from}~\emph{facts} \isakeyword{and} @{text this}
       
   435 \end{center}
   397 \end{itemize}
   436 \end{itemize}
   398 If you wonder how to \isakeyword{obtain} @{term y}:
   437 If you wonder how to \isakeyword{obtain} @{term y}:
   399 via the predefined elimination rule @{thm rangeE[no_vars]}.
   438 via the predefined elimination rule @{thm rangeE[no_vars]}.
   400 
   439 
   401 Method @{text blast} is used because the contradiction does not follow easily
   440 Method @{text blast} is used because the contradiction does not follow easily
   423       with notin show False    by contradiction
   462       with notin show False    by contradiction
   424     qed
   463     qed
   425   qed
   464   qed
   426 qed
   465 qed
   427 text{*\noindent Method @{text contradiction} succeeds if both $P$ and
   466 text{*\noindent Method @{text contradiction} succeeds if both $P$ and
   428 $\neg P$ are among the assumptions and the facts fed into that step.
   467 $\neg P$ are among the assumptions and the facts fed into that step, in any order.
   429 
   468 
   430 As it happens, Cantor's theorem can be proved automatically by best-first
   469 As it happens, Cantor's theorem can be proved automatically by best-first
   431 search. Depth-first search would diverge, but best-first search successfully
   470 search. Depth-first search would diverge, but best-first search successfully
   432 navigates through the large search space:
   471 navigates through the large search space:
   433 *}
   472 *}
   438 constructed introduction and elimination rules for set theory.
   477 constructed introduction and elimination rules for set theory.
   439 
   478 
   440 Finally, whole scripts may appear in the leaves of the proof tree, although
   479 Finally, whole scripts may appear in the leaves of the proof tree, although
   441 this is best avoided.  Here is a contrived example: *}
   480 this is best avoided.  Here is a contrived example: *}
   442 
   481 
   443 lemma "A \<longrightarrow> (A \<longrightarrow>B) \<longrightarrow> B"
   482 lemma "A \<longrightarrow> (A \<longrightarrow> B) \<longrightarrow> B"
   444 proof
   483 proof
   445   assume a: A
   484   assume a: A
   446   show "(A \<longrightarrow>B) \<longrightarrow> B"
   485   show "(A \<longrightarrow>B) \<longrightarrow> B"
   447     apply(rule impI)
   486     apply(rule impI)
   448     apply(erule impE)
   487     apply(erule impE)
   464 proof cases
   503 proof cases
   465   assume "P" thus ?thesis ..
   504   assume "P" thus ?thesis ..
   466 next
   505 next
   467   assume "\<not> P" thus ?thesis ..
   506   assume "\<not> P" thus ?thesis ..
   468 qed
   507 qed
   469 text{*\noindent As always, the cases can be tackled in any order.
   508 text{*\noindent Note that the two cases must come in this order.
   470 
   509 
   471 This form is appropriate if @{term P} is textually small.  However, if
   510 This form is appropriate if @{term P} is textually small.  However, if
   472 @{term P} is large, we don't want to repeat it. This can be avoided by
   511 @{term P} is large, we don't want to repeat it. This can be avoided by
   473 the following idiom *}
   512 the following idiom *}
   474 
   513 
   479   case False thus ?thesis ..
   518   case False thus ?thesis ..
   480 qed
   519 qed
   481 text{*\noindent which is simply a shorthand for the previous
   520 text{*\noindent which is simply a shorthand for the previous
   482 proof. More precisely, the phrases \isakeyword{case}~@{prop
   521 proof. More precisely, the phrases \isakeyword{case}~@{prop
   483 True}/@{prop False} abbreviate the corresponding assumptions @{prop P}
   522 True}/@{prop False} abbreviate the corresponding assumptions @{prop P}
   484 and @{prop"\<not>P"}.
   523 and @{prop"\<not>P"}. In contrast to the previous proof we can prove the cases
       
   524 in arbitrary order.
   485 
   525 
   486 The same game can be played with other datatypes, for example lists:
   526 The same game can be played with other datatypes, for example lists:
   487 *}
   527 *}
   488 
       
   489 (*<*)declare length_tl[simp del](*>*)
   528 (*<*)declare length_tl[simp del](*>*)
   490 
       
   491 lemma "length(tl xs) = length xs - 1"
   529 lemma "length(tl xs) = length xs - 1"
   492 proof (cases xs)
   530 proof (cases xs)
   493   case Nil thus ?thesis by simp
   531   case Nil thus ?thesis by simp
   494 next
   532 next
   495   case Cons thus ?thesis by simp
   533   case Cons thus ?thesis by simp
   496 qed
   534 qed
   497 text{*\noindent Here \isakeyword{case}~@{text Nil} abbreviates
   535 text{*\noindent Here \isakeyword{case}~@{text Nil} abbreviates
   498 \isakeyword{assume}~@{prop"x = []"} and \isakeyword{case}~@{text Cons}
   536 \isakeyword{assume}~@{prop"x = []"} and \isakeyword{case}~@{text Cons}
   499 abbreviates \isakeyword{assume}~@{text"xs = _ # _"}. The names of the head
   537 abbreviates \isakeyword{assume}~@{text"xs = _ # _"}. The names of the head
   500 and tail of @{text xs} have been chosen by the system. Therefore we cannot
   538 and tail of @{text xs} have been chosen by the system. Therefore we cannot
   501 refer to them (this would lead to brittle proofs) and have not even shown
   539 refer to them (this would lead to obscure proofs) and have not even shown
   502 them. Luckily, the proof is simple enough we do not need to refer to them.
   540 them. Luckily, this proof is simple enough we do not need to refer to them.
   503 However, in general one may have to. Hence Isar offers a simple scheme for
   541 However, in general one may have to. Hence Isar offers a simple scheme for
   504 naming those variables: Replace the anonymous @{text Cons} by, for example,
   542 naming those variables: replace the anonymous @{text Cons} by, for example,
   505 @{text"(Cons y ys)"}, which abbreviates \isakeyword{fix}~@{text"y ys"}
   543 @{text"(Cons y ys)"}, which abbreviates \isakeyword{fix}~@{text"y ys"}
   506 \isakeyword{assume}~@{text"xs = Cons y ys"}, i.e.\ @{prop"xs = y # ys"}. Here
   544 \isakeyword{assume}~@{text"xs = Cons y ys"}, i.e.\ @{prop"xs = y # ys"}. Here
   507 is a (somewhat contrived) example: *}
   545 is a (somewhat contrived) example: *}
   508 
   546 
   509 lemma "length(tl xs) = length xs - 1"
   547 lemma "length(tl xs) = length xs - 1"
   513   case (Cons y ys)
   551   case (Cons y ys)
   514   hence "length(tl xs) = length ys  \<and>  length xs = length ys + 1"
   552   hence "length(tl xs) = length ys  \<and>  length xs = length ys + 1"
   515     by simp
   553     by simp
   516   thus ?thesis by simp
   554   thus ?thesis by simp
   517 qed
   555 qed
   518 text{* New case distincion rules can be declared any time, even with
   556 
   519 named cases. *}
       
   520 
   557 
   521 subsection{*Induction*}
   558 subsection{*Induction*}
   522 
   559 
   523 
   560 
   524 subsubsection{*Structural induction*}
   561 subsubsection{*Structural induction*}
   550 
   587 
   551 text{* \noindent The implicitly defined @{text ?case} refers to the
   588 text{* \noindent The implicitly defined @{text ?case} refers to the
   552 corresponding case to be proved, i.e.\ @{text"?P 0"} in the first case and
   589 corresponding case to be proved, i.e.\ @{text"?P 0"} in the first case and
   553 @{text"?P(Suc n)"} in the second case. Context \isakeyword{case}~@{text 0} is
   590 @{text"?P(Suc n)"} in the second case. Context \isakeyword{case}~@{text 0} is
   554 empty whereas \isakeyword{case}~@{text Suc} assumes @{text"?P n"}. Again we
   591 empty whereas \isakeyword{case}~@{text Suc} assumes @{text"?P n"}. Again we
   555 have the same problem as with case distinctions: we cannot refer to @{term n}
   592 have the same problem as with case distinctions: we cannot refer to an anonymous @{term n}
   556 in the induction step because it has not been introduced via \isakeyword{fix}
   593 in the induction step because it has not been introduced via \isakeyword{fix}
   557 (in contrast to the previous proof). The solution is the same as above:
   594 (in contrast to the previous proof). The solution is the same as above:
   558 replace @{term Suc} by @{text"(Suc i)"}: *}
   595 replace @{term Suc} by @{text"(Suc i)"}: *}
   559 lemma fixes n::nat shows "n < n*n + 1"
   596 lemma fixes n::nat shows "n < n*n + 1"
   560 proof (induct n)
   597 proof (induct n)
   607 looks confusing at first and reveals that the very suggestive @{text"(Suc
   644 looks confusing at first and reveals that the very suggestive @{text"(Suc
   608 n)"} used above is not the whole truth. The variable names after the case
   645 n)"} used above is not the whole truth. The variable names after the case
   609 name (here: @{term Suc}) are the names of the parameters of that subgoal. So
   646 name (here: @{term Suc}) are the names of the parameters of that subgoal. So
   610 far the only parameters where the arguments to the constructor, but now we
   647 far the only parameters where the arguments to the constructor, but now we
   611 have an additonal parameter coming from the @{text"\<And>m"} in the main
   648 have an additonal parameter coming from the @{text"\<And>m"} in the main
   612 \isakeyword{shows} clause. Thus  Thus @{text"(Suc n m)"} does not mean that
   649 \isakeyword{shows} clause. Thus @{text"(Suc n m)"} does not mean that
   613 constructor @{term Suc} is applied to two arguments but that the two
   650 constructor @{term Suc} is applied to two arguments but that the two
   614 parameters in the @{term Suc} case are to be named @{text n} and @{text
   651 parameters in the @{term Suc} case are to be named @{text n} and @{text
   615 m}. *}
   652 m}. *}
   616 
   653 
   617 subsubsection{*Rule induction*}
   654 subsubsection{*Rule induction*}
   623 intros
   660 intros
   624 refl:  "(x,x) \<in> r*"
   661 refl:  "(x,x) \<in> r*"
   625 step:  "\<lbrakk> (x,y) \<in> r; (y,z) \<in> r* \<rbrakk> \<Longrightarrow> (x,z) \<in> r*"
   662 step:  "\<lbrakk> (x,y) \<in> r; (y,z) \<in> r* \<rbrakk> \<Longrightarrow> (x,z) \<in> r*"
   626 
   663 
   627 text{* \noindent and prove that @{term"r*"} is indeed transitive: *}
   664 text{* \noindent and prove that @{term"r*"} is indeed transitive: *}
   628 lemma assumes A: "(x,y) \<in> r*"
   665 lemma assumes A: "(x,y) \<in> r*" shows "(y,z) \<in> r* \<Longrightarrow> (x,z) \<in> r*"
   629   shows "(y,z) \<in> r* \<Longrightarrow> (x,z) \<in> r*" (is "PROP ?P x y")
       
   630 using A
   666 using A
   631 proof induct
   667 proof induct
   632   case refl thus ?case .
   668   case refl thus ?case .
   633 next
   669 next
   634   case step thus ?case by(blast intro: rtc.step)
   670   case step thus ?case by(blast intro: rtc.step)
   635 qed
   671 qed
   636 (*
   672 text{*\noindent Rule induction is triggered by a fact $(x_1,\dots,x_n)
   637 lemma assumes A: "(x,y) \<in> r*"
   673 \in R$ piped into the proof, here \isakeyword{using}~@{text A}. The
   638   shows "(y,z) \<in> r* \<Longrightarrow> (x,z) \<in> r*" (is "PROP ?P x y")
   674 proof itself follows the inductive definition very
   639 proof -
   675 closely: there is one case for each rule, and it has the same name as
   640   from A show "PROP ?P x y"
   676 the rule, analogous to structural induction.
   641   proof induct
   677 
   642     fix x show "PROP ?P x x" .
   678 However, this proof is rather terse. Here is a more readable version:
   643   next
       
   644     fix x' x y
       
   645     assume "(x',x) \<in> r"
       
   646            "PROP ?P x y"   -- "induction hypothesis"
       
   647     thus "PROP ?P x' y" by(blast intro: rtc.step)
       
   648   qed
       
   649 qed
       
   650 *)
       
   651 text{*\noindent Rule induction is triggered by a fact $(x_1,\dots,x_n) \in R$
       
   652 piped into the proof, here \isakeyword{from}~@{text A}. The proof itself
       
   653 follows the two rules of the inductive definition very closely.  The only
       
   654 surprising thing should be the keyword @{text PROP}: because of certain
       
   655 syntactic subleties it is required in front of terms of type @{typ prop} (the
       
   656 type of meta-level propositions) which are not obviously of type @{typ prop}
       
   657 (e.g.\ @{text"?P x y"}) because they do not contain a tell-tale constant such
       
   658 as @{text"\<And>"} or @{text"\<Longrightarrow>"}.
       
   659 
       
   660 However, the proof is rather terse. Here is a more readable version:
       
   661 *}
   679 *}
   662 
   680 
   663 lemma assumes A: "(x,y) \<in> r*" and B: "(y,z) \<in> r*"
   681 lemma assumes A: "(x,y) \<in> r*" and B: "(y,z) \<in> r*"
   664   shows "(x,z) \<in> r*"
   682   shows "(x,z) \<in> r*"
   665 proof -
   683 proof -
   673            IH: "(y,z) \<in> r* \<Longrightarrow> (x,z) \<in> r*" and
   691            IH: "(y,z) \<in> r* \<Longrightarrow> (x,z) \<in> r*" and
   674            B:  "(y,z) \<in> r*"
   692            B:  "(y,z) \<in> r*"
   675     from 1 IH[OF B] show "(x',z) \<in> r*" by(rule rtc.step)
   693     from 1 IH[OF B] show "(x',z) \<in> r*" by(rule rtc.step)
   676   qed
   694   qed
   677 qed
   695 qed
   678 
       
   679 text{*\noindent We start the proof with \isakeyword{from}~@{text"A
   696 text{*\noindent We start the proof with \isakeyword{from}~@{text"A
   680 B"}. Only @{text A} is ``consumed'' by the first proof step, here
   697 B"}. Only @{text A} is ``consumed'' by the induction step.
   681 induction. Since @{text B} is left over we don't just prove @{text
   698 Since @{text B} is left over we don't just prove @{text
   682 ?thesis} but @{text"B \<Longrightarrow> ?thesis"}, just as in the previous
   699 ?thesis} but @{text"B \<Longrightarrow> ?thesis"}, just as in the previous proof. The
   683 proof, only more elegantly. The base case is trivial. In the
   700 base case is trivial. In the assumptions for the induction step we can
   684 assumptions for the induction step we can see very clearly how things
   701 see very clearly how things fit together and permit ourselves the
   685 fit together and permit ourselves the obvious forward step
   702 obvious forward step @{text"IH[OF B]"}. *}
   686 @{text"IH[OF B]"}. *}
       
   687 
   703 
   688 (*<*)end(*>*)
   704 (*<*)end(*>*)