doc-src/IsarOverview/Isar/Logic.thy
author nipkow
Sun, 11 Jun 2006 19:36:10 +0200
changeset 19840 600c35fd1b5e
parent 17914 99ead7a7eb42
child 25412 6f56f0350f6c
permissions -rw-r--r--
added quoting via back quotes
     1 (*<*)theory Logic imports Main begin(*>*)
     2 
     3 section{*Logic \label{sec:Logic}*}
     4 
     5 subsection{*Propositional logic*}
     6 
     7 subsubsection{*Introduction rules*}
     8 
     9 text{* We start with a really trivial toy proof to introduce the basic
    10 features of structured proofs. *}
    11 lemma "A \<longrightarrow> A"
    12 proof (rule impI)
    13   assume a: "A"
    14   show "A" by(rule a)
    15 qed
    16 text{*\noindent
    17 The operational reading: the \isakeyword{assume}-\isakeyword{show}
    18 block proves @{prop"A \<Longrightarrow> A"} (@{term a} is a degenerate rule (no
    19 assumptions) that proves @{term A} outright), which rule
    20 @{thm[source]impI} (@{thm impI}) turns into the desired @{prop"A \<longrightarrow>
    21 A"}.  However, this text is much too detailed for comfort. Therefore
    22 Isar implements the following principle: \begin{quote}\em Command
    23 \isakeyword{proof} automatically tries to select an introduction rule
    24 based on the goal and a predefined list of rules.  \end{quote} Here
    25 @{thm[source]impI} is applied automatically: *}
    26 
    27 lemma "A \<longrightarrow> A"
    28 proof
    29   assume a: A
    30   show A by(rule a)
    31 qed
    32 
    33 text{*\noindent Single-identifier formulae such as @{term A} need not
    34 be enclosed in double quotes. However, we will continue to do so for
    35 uniformity.
    36 
    37 Trivial proofs, in particular those by assumption, should be trivial
    38 to perform. Proof ``.'' does just that (and a bit more). Thus
    39 naming of assumptions is often superfluous: *}
    40 lemma "A \<longrightarrow> A"
    41 proof
    42   assume "A"
    43   show "A" .
    44 qed
    45 
    46 text{* To hide proofs by assumption further, \isakeyword{by}@{text"(method)"}
    47 first applies @{text method} and then tries to solve all remaining subgoals
    48 by assumption: *}
    49 lemma "A \<longrightarrow> A \<and> A"
    50 proof
    51   assume "A"
    52   show "A \<and> A" by(rule conjI)
    53 qed
    54 text{*\noindent Rule @{thm[source]conjI} is of course @{thm conjI}.
    55 A drawback of implicit proofs by assumption is that it
    56 is no longer obvious where an assumption is used.
    57 
    58 Proofs of the form \isakeyword{by}@{text"(rule"}~\emph{name}@{text")"}
    59 can be abbreviated to ``..''  if \emph{name} refers to one of the
    60 predefined introduction rules (or elimination rules, see below): *}
    61 
    62 lemma "A \<longrightarrow> A \<and> A"
    63 proof
    64   assume "A"
    65   show "A \<and> A" ..
    66 qed
    67 text{*\noindent
    68 This is what happens: first the matching introduction rule @{thm[source]conjI}
    69 is applied (first ``.''), then the two subgoals are solved by assumption
    70 (second ``.''). *}
    71 
    72 subsubsection{*Elimination rules*}
    73 
    74 text{*A typical elimination rule is @{thm[source]conjE}, $\land$-elimination:
    75 @{thm[display,indent=5]conjE}  In the following proof it is applied
    76 by hand, after its first (\emph{major}) premise has been eliminated via
    77 @{text"[OF AB]"}: *}
    78 lemma "A \<and> B \<longrightarrow> B \<and> A"
    79 proof
    80   assume AB: "A \<and> B"
    81   show "B \<and> A"
    82   proof (rule conjE[OF AB])  -- {*@{text"conjE[OF AB]"}: @{thm conjE[OF AB]} *}
    83     assume "A" "B"
    84     show ?thesis ..
    85   qed
    86 qed
    87 text{*\noindent Note that the term @{text"?thesis"} always stands for the
    88 ``current goal'', i.e.\ the enclosing \isakeyword{show} (or
    89 \isakeyword{have}) statement.
    90 
    91 This is too much proof text. Elimination rules should be selected
    92 automatically based on their major premise, the formula or rather connective
    93 to be eliminated. In Isar they are triggered by facts being fed
    94 \emph{into} a proof. Syntax:
    95 \begin{center}
    96 \isakeyword{from} \emph{fact} \isakeyword{show} \emph{proposition} \emph{proof}
    97 \end{center}
    98 where \emph{fact} stands for the name of a previously proved
    99 proposition, e.g.\ an assumption, an intermediate result or some global
   100 theorem, which may also be modified with @{text OF} etc.
   101 The \emph{fact} is ``piped'' into the \emph{proof}, which can deal with it
   102 how it chooses. If the \emph{proof} starts with a plain \isakeyword{proof},
   103 an elimination rule (from a predefined list) is applied
   104 whose first premise is solved by the \emph{fact}. Thus the proof above
   105 is equivalent to the following one: *}
   106 
   107 lemma "A \<and> B \<longrightarrow> B \<and> A"
   108 proof
   109   assume AB: "A \<and> B"
   110   from AB show "B \<and> A"
   111   proof
   112     assume "A" "B"
   113     show ?thesis ..
   114   qed
   115 qed
   116 
   117 text{* Now we come to a second important principle:
   118 \begin{quote}\em
   119 Try to arrange the sequence of propositions in a UNIX-like pipe,
   120 such that the proof of each proposition builds on the previous proposition.
   121 \end{quote}
   122 The previous proposition can be referred to via the fact @{text this}.
   123 This greatly reduces the need for explicit naming of propositions:
   124 *}
   125 lemma "A \<and> B \<longrightarrow> B \<and> A"
   126 proof
   127   assume "A \<and> B"
   128   from this show "B \<and> A"
   129   proof
   130     assume "A" "B"
   131     show ?thesis ..
   132   qed
   133 qed
   134 
   135 text{*\noindent Because of the frequency of \isakeyword{from}~@{text
   136 this}, Isar provides two abbreviations:
   137 \begin{center}
   138 \begin{tabular}{r@ {\quad=\quad}l}
   139 \isakeyword{then} & \isakeyword{from} @{text this} \\
   140 \isakeyword{thus} & \isakeyword{then} \isakeyword{show}
   141 \end{tabular}
   142 \end{center}
   143 
   144 Here is an alternative proof that operates purely by forward reasoning: *}
   145 lemma "A \<and> B \<longrightarrow> B \<and> A"
   146 proof
   147   assume ab: "A \<and> B"
   148   from ab have a: "A" ..
   149   from ab have b: "B" ..
   150   from b a show "B \<and> A" ..
   151 qed
   152 
   153 text{*\noindent It is worth examining this text in detail because it
   154 exhibits a number of new concepts.  For a start, it is the first time
   155 we have proved intermediate propositions (\isakeyword{have}) on the
   156 way to the final \isakeyword{show}. This is the norm in nontrivial
   157 proofs where one cannot bridge the gap between the assumptions and the
   158 conclusion in one step. To understand how the proof works we need to
   159 explain more Isar details.
   160 
   161 Method @{text rule} can be given a list of rules, in which case
   162 @{text"(rule"}~\textit{rules}@{text")"} applies the first matching
   163 rule in the list \textit{rules}. Command \isakeyword{from} can be
   164 followed by any number of facts.  Given \isakeyword{from}~@{text
   165 f}$_1$~\dots~@{text f}$_n$, the proof step
   166 @{text"(rule"}~\textit{rules}@{text")"} following a \isakeyword{have}
   167 or \isakeyword{show} searches \textit{rules} for a rule whose first
   168 $n$ premises can be proved by @{text f}$_1$~\dots~@{text f}$_n$ in the
   169 given order. Finally one needs to know that ``..'' is short for
   170 @{text"by(rule"}~\textit{elim-rules intro-rules}@{text")"} (or
   171 @{text"by(rule"}~\textit{intro-rules}@{text")"} if there are no facts
   172 fed into the proof), i.e.\ elimination rules are tried before
   173 introduction rules.
   174 
   175 Thus in the above proof both \isakeyword{have}s are proved via
   176 @{thm[source]conjE} triggered by \isakeyword{from}~@{text ab} whereas
   177 in the \isakeyword{show} step no elimination rule is applicable and
   178 the proof succeeds with @{thm[source]conjI}. The latter would fail had
   179 we written \isakeyword{from}~@{text"a b"} instead of
   180 \isakeyword{from}~@{text"b a"}.
   181 
   182 Proofs starting with a plain @{text proof} behave the same because the
   183 latter is short for @{text"proof (rule"}~\textit{elim-rules
   184 intro-rules}@{text")"} (or @{text"proof
   185 (rule"}~\textit{intro-rules}@{text")"} if there are no facts fed into
   186 the proof). *}
   187 
   188 subsection{*More constructs*}
   189 
   190 text{* In the previous proof of @{prop"A \<and> B \<longrightarrow> B \<and> A"} we needed to feed
   191 more than one fact into a proof step, a frequent situation. Then the
   192 UNIX-pipe model appears to break down and we need to name the different
   193 facts to refer to them. But this can be avoided:
   194 *}
   195 lemma "A \<and> B \<longrightarrow> B \<and> A"
   196 proof
   197   assume ab: "A \<and> B"
   198   from ab have "B" ..
   199   moreover
   200   from ab have "A" ..
   201   ultimately show "B \<and> A" ..
   202 qed
   203 text{*\noindent You can combine any number of facts @{term A1} \dots\ @{term
   204 An} into a sequence by separating their proofs with
   205 \isakeyword{moreover}. After the final fact, \isakeyword{ultimately} stands
   206 for \isakeyword{from}~@{term A1}~\dots~@{term An}.  This avoids having to
   207 introduce names for all of the sequence elements.  *}
   208 
   209 text{* Although we have only seen a few introduction and elimination rules so
   210 far, Isar's predefined rules include all the usual natural deduction
   211 rules. We conclude our exposition of propositional logic with an extended
   212 example --- which rules are used implicitly where? *}
   213 lemma "\<not> (A \<and> B) \<longrightarrow> \<not> A \<or> \<not> B"
   214 proof
   215   assume n: "\<not> (A \<and> B)"
   216   show "\<not> A \<or> \<not> B"
   217   proof (rule ccontr)
   218     assume nn: "\<not> (\<not> A \<or> \<not> B)"
   219     have "\<not> A"
   220     proof
   221       assume "A"
   222       have "\<not> B"
   223       proof
   224         assume "B"
   225         have "A \<and> B" ..
   226         with n show False ..
   227       qed
   228       hence "\<not> A \<or> \<not> B" ..
   229       with nn show False ..
   230     qed
   231     hence "\<not> A \<or> \<not> B" ..
   232     with nn show False ..
   233   qed
   234 qed
   235 text{*\noindent
   236 Rule @{thm[source]ccontr} (``classical contradiction'') is
   237 @{thm ccontr[no_vars]}.
   238 Apart from demonstrating the strangeness of classical
   239 arguments by contradiction, this example also introduces two new
   240 abbreviations:
   241 \begin{center}
   242 \begin{tabular}{l@ {\quad=\quad}l}
   243 \isakeyword{hence} & \isakeyword{then} \isakeyword{have} \\
   244 \isakeyword{with}~\emph{facts} &
   245 \isakeyword{from}~\emph{facts} @{text this}
   246 \end{tabular}
   247 \end{center}
   248 *}
   249 
   250 
   251 subsection{*Avoiding duplication*}
   252 
   253 text{* So far our examples have been a bit unnatural: normally we want to
   254 prove rules expressed with @{text"\<Longrightarrow>"}, not @{text"\<longrightarrow>"}. Here is an example:
   255 *}
   256 lemma "A \<and> B \<Longrightarrow> B \<and> A"
   257 proof
   258   assume "A \<and> B" thus "B" ..
   259 next
   260   assume "A \<and> B" thus "A" ..
   261 qed
   262 text{*\noindent The \isakeyword{proof} always works on the conclusion,
   263 @{prop"B \<and> A"} in our case, thus selecting $\land$-introduction. Hence
   264 we must show @{prop B} and @{prop A}; both are proved by
   265 $\land$-elimination and the proofs are separated by \isakeyword{next}:
   266 \begin{description}
   267 \item[\isakeyword{next}] deals with multiple subgoals. For example,
   268 when showing @{term"A \<and> B"} we need to show both @{term A} and @{term
   269 B}.  Each subgoal is proved separately, in \emph{any} order. The
   270 individual proofs are separated by \isakeyword{next}.  \footnote{Each
   271 \isakeyword{show} must prove one of the pending subgoals.  If a
   272 \isakeyword{show} matches multiple subgoals, e.g.\ if the subgoals
   273 contain ?-variables, the first one is proved. Thus the order in which
   274 the subgoals are proved can matter --- see
   275 \S\ref{sec:CaseDistinction} for an example.}
   276 
   277 Strictly speaking \isakeyword{next} is only required if the subgoals
   278 are proved in different assumption contexts which need to be
   279 separated, which is not the case above. For clarity we
   280 have employed \isakeyword{next} anyway and will continue to do so.
   281 \end{description}
   282 
   283 This is all very well as long as formulae are small. Let us now look at some
   284 devices to avoid repeating (possibly large) formulae. A very general method
   285 is pattern matching: *}
   286 
   287 lemma "large_A \<and> large_B \<Longrightarrow> large_B \<and> large_A"
   288       (is "?AB \<Longrightarrow> ?B \<and> ?A")
   289 proof
   290   assume "?AB" thus "?B" ..
   291 next
   292   assume "?AB" thus "?A" ..
   293 qed
   294 text{*\noindent Any formula may be followed by
   295 @{text"("}\isakeyword{is}~\emph{pattern}@{text")"} which causes the pattern
   296 to be matched against the formula, instantiating the @{text"?"}-variables in
   297 the pattern. Subsequent uses of these variables in other terms causes
   298 them to be replaced by the terms they stand for.
   299 
   300 We can simplify things even more by stating the theorem by means of the
   301 \isakeyword{assumes} and \isakeyword{shows} elements which allow direct
   302 naming of assumptions: *}
   303 
   304 lemma assumes AB: "large_A \<and> large_B"
   305   shows "large_B \<and> large_A" (is "?B \<and> ?A")
   306 proof
   307   from AB show "?B" ..
   308 next
   309   from AB show "?A" ..
   310 qed
   311 text{*\noindent Note the difference between @{text ?AB}, a term, and
   312 @{text AB}, a fact.
   313 
   314 Finally we want to start the proof with $\land$-elimination so we
   315 don't have to perform it twice, as above. Here is a slick way to
   316 achieve this: *}
   317 
   318 lemma assumes AB: "large_A \<and> large_B"
   319   shows "large_B \<and> large_A" (is "?B \<and> ?A")
   320 using AB
   321 proof
   322   assume "?A" "?B" show ?thesis ..
   323 qed
   324 text{*\noindent Command \isakeyword{using} can appear before a proof
   325 and adds further facts to those piped into the proof. Here @{text AB}
   326 is the only such fact and it triggers $\land$-elimination. Another
   327 frequent idiom is as follows:
   328 \begin{center}
   329 \isakeyword{from} \emph{major-facts}~
   330 \isakeyword{show} \emph{proposition}~
   331 \isakeyword{using} \emph{minor-facts}~
   332 \emph{proof}
   333 \end{center}
   334 
   335 Sometimes it is necessary to suppress the implicit application of rules in a
   336 \isakeyword{proof}. For example \isakeyword{show}~@{prop"A \<or> B"} would
   337 trigger $\lor$-introduction, requiring us to prove @{prop A}. A simple
   338 ``@{text"-"}'' prevents this \emph{faux pas}: *}
   339 
   340 lemma assumes AB: "A \<or> B" shows "B \<or> A"
   341 proof -
   342   from AB show ?thesis
   343   proof
   344     assume A show ?thesis ..
   345   next
   346     assume B show ?thesis ..
   347   qed
   348 qed
   349 
   350 text{* Too many names can easily clutter a proof.  We already learned
   351 about @{text this} as a means of avoiding explicit names. Another
   352 handy device is to refer to a fact not by name but by contents: for
   353 example, writing @{text "`A \<or> B`"} (enclosing the formula in back quotes)
   354 refers to the fact @{text"A \<or> B"}
   355 without the need to name it. Here is a simple example, a revised version
   356 of the previous proof *}
   357 
   358 lemma assumes "A \<or> B" shows "B \<or> A"
   359 proof -
   360   from `A \<or> B` show ?thesis
   361 (*<*)oops(*>*)
   362 text{*\noindent which continues as before.
   363 
   364 Clearly, this device of quoting facts by contents is only advisable
   365 for small formulae. In such cases it is superior to naming because the
   366 reader immediately sees what the fact is without needing to search for
   367 it in the preceding proof text. *}
   368 
   369 subsection{*Predicate calculus*}
   370 
   371 text{* Command \isakeyword{fix} introduces new local variables into a
   372 proof. The pair \isakeyword{fix}-\isakeyword{show} corresponds to @{text"\<And>"}
   373 (the universal quantifier at the
   374 meta-level) just like \isakeyword{assume}-\isakeyword{show} corresponds to
   375 @{text"\<Longrightarrow>"}. Here is a sample proof, annotated with the rules that are
   376 applied implicitly: *}
   377 lemma assumes P: "\<forall>x. P x" shows "\<forall>x. P(f x)"
   378 proof                       --"@{thm[source]allI}: @{thm allI}"
   379   fix a
   380   from P show "P(f a)" ..  --"@{thm[source]allE}: @{thm allE}"
   381 qed
   382 text{*\noindent Note that in the proof we have chosen to call the bound
   383 variable @{term a} instead of @{term x} merely to show that the choice of
   384 local names is irrelevant.
   385 
   386 Next we look at @{text"\<exists>"} which is a bit more tricky.
   387 *}
   388 
   389 lemma assumes Pf: "\<exists>x. P(f x)" shows "\<exists>y. P y"
   390 proof -
   391   from Pf show ?thesis
   392   proof              -- "@{thm[source]exE}: @{thm exE}"
   393     fix x
   394     assume "P(f x)"
   395     show ?thesis ..  -- "@{thm[source]exI}: @{thm exI}"
   396   qed
   397 qed
   398 text{*\noindent Explicit $\exists$-elimination as seen above can become
   399 cumbersome in practice.  The derived Isar language element
   400 \isakeyword{obtain} provides a more appealing form of generalised
   401 existence reasoning: *}
   402 
   403 lemma assumes Pf: "\<exists>x. P(f x)" shows "\<exists>y. P y"
   404 proof -
   405   from Pf obtain x where "P(f x)" ..
   406   thus "\<exists>y. P y" ..
   407 qed
   408 text{*\noindent Note how the proof text follows the usual mathematical style
   409 of concluding $P(x)$ from $\exists x. P(x)$, while carefully introducing $x$
   410 as a new local variable.  Technically, \isakeyword{obtain} is similar to
   411 \isakeyword{fix} and \isakeyword{assume} together with a soundness proof of
   412 the elimination involved.
   413 
   414 Here is a proof of a well known tautology.
   415 Which rule is used where?  *}
   416 
   417 lemma assumes ex: "\<exists>x. \<forall>y. P x y" shows "\<forall>y. \<exists>x. P x y"
   418 proof
   419   fix y
   420   from ex obtain x where "\<forall>y. P x y" ..
   421   hence "P x y" ..
   422   thus "\<exists>x. P x y" ..
   423 qed
   424 
   425 subsection{*Making bigger steps*}
   426 
   427 text{* So far we have confined ourselves to single step proofs. Of course
   428 powerful automatic methods can be used just as well. Here is an example,
   429 Cantor's theorem that there is no surjective function from a set to its
   430 powerset: *}
   431 theorem "\<exists>S. S \<notin> range (f :: 'a \<Rightarrow> 'a set)"
   432 proof
   433   let ?S = "{x. x \<notin> f x}"
   434   show "?S \<notin> range f"
   435   proof
   436     assume "?S \<in> range f"
   437     then obtain y where "?S = f y" ..
   438     show False
   439     proof cases
   440       assume "y \<in> ?S"
   441       with `?S = f y` show False by blast
   442     next
   443       assume "y \<notin> ?S"
   444       with `?S = f y` show False by blast
   445     qed
   446   qed
   447 qed
   448 text{*\noindent
   449 For a start, the example demonstrates two new constructs:
   450 \begin{itemize}
   451 \item \isakeyword{let} introduces an abbreviation for a term, in our case
   452 the witness for the claim.
   453 \item Proof by @{text"cases"} starts a proof by cases. Note that it remains
   454 implicit what the two cases are: it is merely expected that the two subproofs
   455 prove @{text"P \<Longrightarrow> ?thesis"} and @{text"\<not>P \<Longrightarrow> ?thesis"} (in that order)
   456 for some @{term P}.
   457 \end{itemize}
   458 If you wonder how to \isakeyword{obtain} @{term y}:
   459 via the predefined elimination rule @{thm rangeE[no_vars]}.
   460 
   461 Method @{text blast} is used because the contradiction does not follow easily
   462 by just a single rule. If you find the proof too cryptic for human
   463 consumption, here is a more detailed version; the beginning up to
   464 \isakeyword{obtain} stays unchanged. *}
   465 
   466 theorem "\<exists>S. S \<notin> range (f :: 'a \<Rightarrow> 'a set)"
   467 proof
   468   let ?S = "{x. x \<notin> f x}"
   469   show "?S \<notin> range f"
   470   proof
   471     assume "?S \<in> range f"
   472     then obtain y where "?S = f y" ..
   473     show False
   474     proof cases
   475       assume "y \<in> ?S"
   476       hence "y \<notin> f y"   by simp
   477       hence "y \<notin> ?S"    by(simp add: `?S = f y`)
   478       thus False         by contradiction
   479     next
   480       assume "y \<notin> ?S"
   481       hence "y \<in> f y"   by simp
   482       hence "y \<in> ?S"    by(simp add: `?S = f y`)
   483       thus False         by contradiction
   484     qed
   485   qed
   486 qed
   487 text{*\noindent Method @{text contradiction} succeeds if both $P$ and
   488 $\neg P$ are among the assumptions and the facts fed into that step, in any order.
   489 
   490 As it happens, Cantor's theorem can be proved automatically by best-first
   491 search. Depth-first search would diverge, but best-first search successfully
   492 navigates through the large search space:
   493 *}
   494 
   495 theorem "\<exists>S. S \<notin> range (f :: 'a \<Rightarrow> 'a set)"
   496 by best
   497 (* Of course this only works in the context of HOL's carefully
   498 constructed introduction and elimination rules for set theory.*)
   499 
   500 subsection{*Raw proof blocks*}
   501 
   502 text{* Although we have shown how to employ powerful automatic methods like
   503 @{text blast} to achieve bigger proof steps, there may still be the
   504 tendency to use the default introduction and elimination rules to
   505 decompose goals and facts. This can lead to very tedious proofs:
   506 *}
   507 (*<*)ML"set quick_and_dirty"(*>*)
   508 lemma "\<forall>x y. A x y \<and> B x y \<longrightarrow> C x y"
   509 proof
   510   fix x show "\<forall>y. A x y \<and> B x y \<longrightarrow> C x y"
   511   proof
   512     fix y show "A x y \<and> B x y \<longrightarrow> C x y"
   513     proof
   514       assume "A x y \<and> B x y"
   515       show "C x y" sorry
   516     qed
   517   qed
   518 qed
   519 text{*\noindent Since we are only interested in the decomposition and not the
   520 actual proof, the latter has been replaced by
   521 \isakeyword{sorry}. Command \isakeyword{sorry} proves anything but is
   522 only allowed in quick and dirty mode, the default interactive mode. It
   523 is very convenient for top down proof development.
   524 
   525 Luckily we can avoid this step by step decomposition very easily: *}
   526 
   527 lemma "\<forall>x y. A x y \<and> B x y \<longrightarrow> C x y"
   528 proof -
   529   have "\<And>x y. \<lbrakk> A x y; B x y \<rbrakk> \<Longrightarrow> C x y"
   530   proof -
   531     fix x y assume "A x y" "B x y"
   532     show "C x y" sorry
   533   qed
   534   thus ?thesis by blast
   535 qed
   536 
   537 text{*\noindent
   538 This can be simplified further by \emph{raw proof blocks}, i.e.\
   539 proofs enclosed in braces: *}
   540 
   541 lemma "\<forall>x y. A x y \<and> B x y \<longrightarrow> C x y"
   542 proof -
   543   { fix x y assume "A x y" "B x y"
   544     have "C x y" sorry }
   545   thus ?thesis by blast
   546 qed
   547 
   548 text{*\noindent The result of the raw proof block is the same theorem
   549 as above, namely @{prop"\<And>x y. \<lbrakk> A x y; B x y \<rbrakk> \<Longrightarrow> C x y"}.  Raw
   550 proof blocks are like ordinary proofs except that they do not prove
   551 some explicitly stated property but that the property emerges directly
   552 out of the \isakeyword{fixe}s, \isakeyword{assume}s and
   553 \isakeyword{have} in the block. Thus they again serve to avoid
   554 duplication. Note that the conclusion of a raw proof block is stated with
   555 \isakeyword{have} rather than \isakeyword{show} because it is not the
   556 conclusion of some pending goal but some independent claim.
   557 
   558 The general idea demonstrated in this subsection is very
   559 important in Isar and distinguishes it from tactic-style proofs:
   560 \begin{quote}\em
   561 Do not manipulate the proof state into a particular form by applying
   562 tactics but state the desired form explicitly and let the tactic verify
   563 that from this form the original goal follows.
   564 \end{quote}
   565 This yields more readable and also more robust proofs.
   566 
   567 \subsubsection{General case distinctions}
   568 
   569 As an important application of raw proof blocks we show how to deal
   570 with general case distinctions --- more specific kinds are treated in
   571 \S\ref{sec:CaseDistinction}. Imagine that you would like to prove some
   572 goal by distinguishing $n$ cases $P_1$, \dots, $P_n$. You show that
   573 the $n$ cases are exhaustive (i.e.\ $P_1 \lor \dots \lor P_n$) and
   574 that each case $P_i$ implies the goal. Taken together, this proves the
   575 goal. The corresponding Isar proof pattern (for $n = 3$) is very handy:
   576 *}
   577 text_raw{*\renewcommand{\isamarkupcmt}[1]{#1}*}
   578 (*<*)lemma "XXX"(*>*)
   579 proof -
   580   have "P\<^isub>1 \<or> P\<^isub>2 \<or> P\<^isub>3" (*<*)sorry(*>*) -- {*\dots*}
   581   moreover
   582   { assume P\<^isub>1
   583     -- {*\dots*}
   584     have ?thesis (*<*)sorry(*>*) -- {*\dots*} }
   585   moreover
   586   { assume P\<^isub>2
   587     -- {*\dots*}
   588     have ?thesis (*<*)sorry(*>*) -- {*\dots*} }
   589   moreover
   590   { assume P\<^isub>3
   591     -- {*\dots*}
   592     have ?thesis (*<*)sorry(*>*) -- {*\dots*} }
   593   ultimately show ?thesis by blast
   594 qed
   595 text_raw{*\renewcommand{\isamarkupcmt}[1]{{\isastylecmt--- #1}}*}
   596 
   597 
   598 subsection{*Further refinements*}
   599 
   600 text{* This subsection discusses some further tricks that can make
   601 life easier although they are not essential. *}
   602 
   603 subsubsection{*\isakeyword{and}*}
   604 
   605 text{* Propositions (following \isakeyword{assume} etc) may but need not be
   606 separated by \isakeyword{and}. This is not just for readability
   607 (\isakeyword{from} \isa{A} \isakeyword{and} \isa{B} looks nicer than
   608 \isakeyword{from} \isa{A} \isa{B}) but for structuring lists of propositions
   609 into possibly named blocks. In
   610 \begin{center}
   611 \isakeyword{assume} \isa{A:} $A_1$ $A_2$ \isakeyword{and} \isa{B:} $A_3$
   612 \isakeyword{and} $A_4$
   613 \end{center}
   614 label \isa{A} refers to the list of propositions $A_1$ $A_2$ and
   615 label \isa{B} to $A_3$. *}
   616 
   617 subsubsection{*\isakeyword{note}*}
   618 text{* If you want to remember intermediate fact(s) that cannot be
   619 named directly, use \isakeyword{note}. For example the result of raw
   620 proof block can be named by following it with
   621 \isakeyword{note}~@{text"some_name = this"}.  As a side effect,
   622 @{text this} is set to the list of facts on the right-hand side. You
   623 can also say @{text"note some_fact"}, which simply sets @{text this},
   624 i.e.\ recalls @{text"some_fact"}, e.g.\ in a \isakeyword{moreover} sequence. *}
   625 
   626 
   627 subsubsection{*\isakeyword{fixes}*}
   628 
   629 text{* Sometimes it is necessary to decorate a proposition with type
   630 constraints, as in Cantor's theorem above. These type constraints tend
   631 to make the theorem less readable. The situation can be improved a
   632 little by combining the type constraint with an outer @{text"\<And>"}: *}
   633 
   634 theorem "\<And>f :: 'a \<Rightarrow> 'a set. \<exists>S. S \<notin> range f"
   635 (*<*)oops(*>*)
   636 text{*\noindent However, now @{term f} is bound and we need a
   637 \isakeyword{fix}~\isa{f} in the proof before we can refer to @{term f}.
   638 This is avoided by \isakeyword{fixes}: *}
   639 
   640 theorem fixes f :: "'a \<Rightarrow> 'a set" shows "\<exists>S. S \<notin> range f"
   641 (*<*)oops(*>*)
   642 text{* \noindent
   643 Even better, \isakeyword{fixes} allows to introduce concrete syntax locally:*}
   644 
   645 lemma comm_mono:
   646   fixes r :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix ">" 60) and
   647        f :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"   (infixl "++" 70)
   648   assumes comm: "\<And>x y::'a. x ++ y = y ++ x" and
   649           mono: "\<And>x y z::'a. x > y \<Longrightarrow> x ++ z > y ++ z"
   650   shows "x > y \<Longrightarrow> z ++ x > z ++ y"
   651 by(simp add: comm mono)
   652 
   653 text{*\noindent The concrete syntax is dropped at the end of the proof and the
   654 theorem becomes @{thm[display,margin=60]comm_mono}
   655 \tweakskip *}
   656 
   657 subsubsection{*\isakeyword{obtain}*}
   658 
   659 text{* The \isakeyword{obtain} construct can introduce multiple
   660 witnesses and propositions as in the following proof fragment:*}
   661 
   662 lemma assumes A: "\<exists>x y. P x y \<and> Q x y" shows "R"
   663 proof -
   664   from A obtain x y where P: "P x y" and Q: "Q x y"  by blast
   665 (*<*)oops(*>*)
   666 text{* Remember also that one does not even need to start with a formula
   667 containing @{text"\<exists>"} as we saw in the proof of Cantor's theorem.
   668 *}
   669 
   670 subsubsection{*Combining proof styles*}
   671 
   672 text{* Finally, whole ``scripts'' (tactic-based proofs in the style of
   673 \cite{LNCS2283}) may appear in the leaves of the proof tree, although this is
   674 best avoided.  Here is a contrived example: *}
   675 
   676 lemma "A \<longrightarrow> (A \<longrightarrow> B) \<longrightarrow> B"
   677 proof
   678   assume a: "A"
   679   show "(A \<longrightarrow>B) \<longrightarrow> B"
   680     apply(rule impI)
   681     apply(erule impE)
   682     apply(rule a)
   683     apply assumption
   684     done
   685 qed
   686 
   687 text{*\noindent You may need to resort to this technique if an
   688 automatic step fails to prove the desired proposition.
   689 
   690 When converting a proof from tactic-style into Isar you can proceed
   691 in a top-down manner: parts of the proof can be left in script form
   692 while the outer structure is already expressed in Isar. *}
   693 
   694 (*<*)end(*>*)