doc-src/IsarRef/Thy/Framework.thy
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 15:03:34 +0200
branchisac-from-Isabelle2009-2
changeset 37913 20e3616b2d9c
parent 36367 641a521bfc19
child 40745 515eab39b6c2
permissions -rw-r--r--
prepare reactivation of isac-update-Isa09-2
     1 theory Framework
     2 imports Main
     3 begin
     4 
     5 chapter {* The Isabelle/Isar Framework \label{ch:isar-framework} *}
     6 
     7 text {*
     8   Isabelle/Isar
     9   \cite{Wenzel:1999:TPHOL,Wenzel-PhD,Nipkow-TYPES02,Wenzel-Paulson:2006,Wenzel:2006:Festschrift}
    10   is intended as a generic framework for developing formal
    11   mathematical documents with full proof checking.  Definitions and
    12   proofs are organized as theories.  An assembly of theory sources may
    13   be presented as a printed document; see also
    14   \chref{ch:document-prep}.
    15 
    16   The main objective of Isar is the design of a human-readable
    17   structured proof language, which is called the ``primary proof
    18   format'' in Isar terminology.  Such a primary proof language is
    19   somewhere in the middle between the extremes of primitive proof
    20   objects and actual natural language.  In this respect, Isar is a bit
    21   more formalistic than Mizar
    22   \cite{Trybulec:1993:MizarFeatures,Rudnicki:1992:MizarOverview,Wiedijk:1999:Mizar},
    23   using logical symbols for certain reasoning schemes where Mizar
    24   would prefer English words; see \cite{Wenzel-Wiedijk:2002} for
    25   further comparisons of these systems.
    26 
    27   So Isar challenges the traditional way of recording informal proofs
    28   in mathematical prose, as well as the common tendency to see fully
    29   formal proofs directly as objects of some logical calculus (e.g.\
    30   @{text "\<lambda>"}-terms in a version of type theory).  In fact, Isar is
    31   better understood as an interpreter of a simple block-structured
    32   language for describing the data flow of local facts and goals,
    33   interspersed with occasional invocations of proof methods.
    34   Everything is reduced to logical inferences internally, but these
    35   steps are somewhat marginal compared to the overall bookkeeping of
    36   the interpretation process.  Thanks to careful design of the syntax
    37   and semantics of Isar language elements, a formal record of Isar
    38   instructions may later appear as an intelligible text to the
    39   attentive reader.
    40 
    41   The Isar proof language has emerged from careful analysis of some
    42   inherent virtues of the existing logical framework of Isabelle/Pure
    43   \cite{paulson-found,paulson700}, notably composition of higher-order
    44   natural deduction rules, which is a generalization of Gentzen's
    45   original calculus \cite{Gentzen:1935}.  The approach of generic
    46   inference systems in Pure is continued by Isar towards actual proof
    47   texts.
    48 
    49   Concrete applications require another intermediate layer: an
    50   object-logic.  Isabelle/HOL \cite{isa-tutorial} (simply-typed
    51   set-theory) is being used most of the time; Isabelle/ZF
    52   \cite{isabelle-ZF} is less extensively developed, although it would
    53   probably fit better for classical mathematics.
    54 
    55   \medskip In order to illustrate natural deduction in Isar, we shall
    56   refer to the background theory and library of Isabelle/HOL.  This
    57   includes common notions of predicate logic, naive set-theory etc.\
    58   using fairly standard mathematical notation.  From the perspective
    59   of generic natural deduction there is nothing special about the
    60   logical connectives of HOL (@{text "\<and>"}, @{text "\<or>"}, @{text "\<forall>"},
    61   @{text "\<exists>"}, etc.), only the resulting reasoning principles are
    62   relevant to the user.  There are similar rules available for
    63   set-theory operators (@{text "\<inter>"}, @{text "\<union>"}, @{text "\<Inter>"}, @{text
    64   "\<Union>"}, etc.), or any other theory developed in the library (lattice
    65   theory, topology etc.).
    66 
    67   Subsequently we briefly review fragments of Isar proof texts
    68   corresponding directly to such general deduction schemes.  The
    69   examples shall refer to set-theory, to minimize the danger of
    70   understanding connectives of predicate logic as something special.
    71 
    72   \medskip The following deduction performs @{text "\<inter>"}-introduction,
    73   working forwards from assumptions towards the conclusion.  We give
    74   both the Isar text, and depict the primitive rule involved, as
    75   determined by unification of the problem against rules that are
    76   declared in the library context.
    77 *}
    78 
    79 text_raw {*\medskip\begin{minipage}{0.6\textwidth}*}
    80 
    81 (*<*)
    82 example_proof
    83 (*>*)
    84     assume "x \<in> A" and "x \<in> B"
    85     then have "x \<in> A \<inter> B" ..
    86 (*<*)
    87 qed
    88 (*>*)
    89 
    90 text_raw {*\end{minipage}\begin{minipage}{0.4\textwidth}*}
    91 
    92 text {*
    93   \infer{@{prop "x \<in> A \<inter> B"}}{@{prop "x \<in> A"} & @{prop "x \<in> B"}}
    94 *}
    95 
    96 text_raw {*\end{minipage}*}
    97 
    98 text {*
    99   \medskip\noindent Note that @{command assume} augments the proof
   100   context, @{command then} indicates that the current fact shall be
   101   used in the next step, and @{command have} states an intermediate
   102   goal.  The two dots ``@{command ".."}'' refer to a complete proof of
   103   this claim, using the indicated facts and a canonical rule from the
   104   context.  We could have been more explicit here by spelling out the
   105   final proof step via the @{command "by"} command:
   106 *}
   107 
   108 (*<*)
   109 example_proof
   110 (*>*)
   111     assume "x \<in> A" and "x \<in> B"
   112     then have "x \<in> A \<inter> B" by (rule IntI)
   113 (*<*)
   114 qed
   115 (*>*)
   116 
   117 text {*
   118   \noindent The format of the @{text "\<inter>"}-introduction rule represents
   119   the most basic inference, which proceeds from given premises to a
   120   conclusion, without any nested proof context involved.
   121 
   122   The next example performs backwards introduction on @{term "\<Inter>\<A>"},
   123   the intersection of all sets within a given set.  This requires a
   124   nested proof of set membership within a local context, where @{term
   125   A} is an arbitrary-but-fixed member of the collection:
   126 *}
   127 
   128 text_raw {*\medskip\begin{minipage}{0.6\textwidth}*}
   129 
   130 (*<*)
   131 example_proof
   132 (*>*)
   133     have "x \<in> \<Inter>\<A>"
   134     proof
   135       fix A
   136       assume "A \<in> \<A>"
   137       show "x \<in> A" sorry %noproof
   138     qed
   139 (*<*)
   140 qed
   141 (*>*)
   142 
   143 text_raw {*\end{minipage}\begin{minipage}{0.4\textwidth}*}
   144 
   145 text {*
   146   \infer{@{prop "x \<in> \<Inter>\<A>"}}{\infer*{@{prop "x \<in> A"}}{@{text "[A][A \<in> \<A>]"}}}
   147 *}
   148 
   149 text_raw {*\end{minipage}*}
   150 
   151 text {*
   152   \medskip\noindent This Isar reasoning pattern again refers to the
   153   primitive rule depicted above.  The system determines it in the
   154   ``@{command proof}'' step, which could have been spelt out more
   155   explicitly as ``@{command proof}~@{text "(rule InterI)"}''.  Note
   156   that the rule involves both a local parameter @{term "A"} and an
   157   assumption @{prop "A \<in> \<A>"} in the nested reasoning.  This kind of
   158   compound rule typically demands a genuine sub-proof in Isar, working
   159   backwards rather than forwards as seen before.  In the proof body we
   160   encounter the @{command fix}-@{command assume}-@{command show}
   161   outline of nested sub-proofs that is typical for Isar.  The final
   162   @{command show} is like @{command have} followed by an additional
   163   refinement of the enclosing claim, using the rule derived from the
   164   proof body.
   165 
   166   \medskip The next example involves @{term "\<Union>\<A>"}, which can be
   167   characterized as the set of all @{term "x"} such that @{prop "\<exists>A. x
   168   \<in> A \<and> A \<in> \<A>"}.  The elimination rule for @{prop "x \<in> \<Union>\<A>"} does
   169   not mention @{text "\<exists>"} and @{text "\<and>"} at all, but admits to obtain
   170   directly a local @{term "A"} such that @{prop "x \<in> A"} and @{prop "A
   171   \<in> \<A>"} hold.  This corresponds to the following Isar proof and
   172   inference rule, respectively:
   173 *}
   174 
   175 text_raw {*\medskip\begin{minipage}{0.6\textwidth}*}
   176 
   177 (*<*)
   178 example_proof
   179 (*>*)
   180     assume "x \<in> \<Union>\<A>"
   181     then have C
   182     proof
   183       fix A
   184       assume "x \<in> A" and "A \<in> \<A>"
   185       show C sorry %noproof
   186     qed
   187 (*<*)
   188 qed
   189 (*>*)
   190 
   191 text_raw {*\end{minipage}\begin{minipage}{0.4\textwidth}*}
   192 
   193 text {*
   194   \infer{@{prop "C"}}{@{prop "x \<in> \<Union>\<A>"} & \infer*{@{prop "C"}~}{@{text "[A][x \<in> A, A \<in> \<A>]"}}}
   195 *}
   196 
   197 text_raw {*\end{minipage}*}
   198 
   199 text {*
   200   \medskip\noindent Although the Isar proof follows the natural
   201   deduction rule closely, the text reads not as natural as
   202   anticipated.  There is a double occurrence of an arbitrary
   203   conclusion @{prop "C"}, which represents the final result, but is
   204   irrelevant for now.  This issue arises for any elimination rule
   205   involving local parameters.  Isar provides the derived language
   206   element @{command obtain}, which is able to perform the same
   207   elimination proof more conveniently:
   208 *}
   209 
   210 (*<*)
   211 example_proof
   212 (*>*)
   213     assume "x \<in> \<Union>\<A>"
   214     then obtain A where "x \<in> A" and "A \<in> \<A>" ..
   215 (*<*)
   216 qed
   217 (*>*)
   218 
   219 text {*
   220   \noindent Here we avoid to mention the final conclusion @{prop "C"}
   221   and return to plain forward reasoning.  The rule involved in the
   222   ``@{command ".."}'' proof is the same as before.
   223 *}
   224 
   225 
   226 section {* The Pure framework \label{sec:framework-pure} *}
   227 
   228 text {*
   229   The Pure logic \cite{paulson-found,paulson700} is an intuitionistic
   230   fragment of higher-order logic \cite{church40}.  In type-theoretic
   231   parlance, there are three levels of @{text "\<lambda>"}-calculus with
   232   corresponding arrows @{text "\<Rightarrow>"}/@{text "\<And>"}/@{text "\<Longrightarrow>"}:
   233 
   234   \medskip
   235   \begin{tabular}{ll}
   236   @{text "\<alpha> \<Rightarrow> \<beta>"} & syntactic function space (terms depending on terms) \\
   237   @{text "\<And>x. B(x)"} & universal quantification (proofs depending on terms) \\
   238   @{text "A \<Longrightarrow> B"} & implication (proofs depending on proofs) \\
   239   \end{tabular}
   240   \medskip
   241 
   242   \noindent Here only the types of syntactic terms, and the
   243   propositions of proof terms have been shown.  The @{text
   244   "\<lambda>"}-structure of proofs can be recorded as an optional feature of
   245   the Pure inference kernel \cite{Berghofer-Nipkow:2000:TPHOL}, but
   246   the formal system can never depend on them due to \emph{proof
   247   irrelevance}.
   248 
   249   On top of this most primitive layer of proofs, Pure implements a
   250   generic calculus for nested natural deduction rules, similar to
   251   \cite{Schroeder-Heister:1984}.  Here object-logic inferences are
   252   internalized as formulae over @{text "\<And>"} and @{text "\<Longrightarrow>"}.
   253   Combining such rule statements may involve higher-order unification
   254   \cite{paulson-natural}.
   255 *}
   256 
   257 
   258 subsection {* Primitive inferences *}
   259 
   260 text {*
   261   Term syntax provides explicit notation for abstraction @{text "\<lambda>x ::
   262   \<alpha>. b(x)"} and application @{text "b a"}, while types are usually
   263   implicit thanks to type-inference; terms of type @{text "prop"} are
   264   called propositions.  Logical statements are composed via @{text "\<And>x
   265   :: \<alpha>. B(x)"} and @{text "A \<Longrightarrow> B"}.  Primitive reasoning operates on
   266   judgments of the form @{text "\<Gamma> \<turnstile> \<phi>"}, with standard introduction
   267   and elimination rules for @{text "\<And>"} and @{text "\<Longrightarrow>"} that refer to
   268   fixed parameters @{text "x\<^isub>1, \<dots>, x\<^isub>m"} and hypotheses
   269   @{text "A\<^isub>1, \<dots>, A\<^isub>n"} from the context @{text "\<Gamma>"};
   270   the corresponding proof terms are left implicit.  The subsequent
   271   inference rules define @{text "\<Gamma> \<turnstile> \<phi>"} inductively, relative to a
   272   collection of axioms:
   273 
   274   \[
   275   \infer{@{text "\<turnstile> A"}}{(@{text "A"} \text{~axiom})}
   276   \qquad
   277   \infer{@{text "A \<turnstile> A"}}{}
   278   \]
   279 
   280   \[
   281   \infer{@{text "\<Gamma> \<turnstile> \<And>x. B(x)"}}{@{text "\<Gamma> \<turnstile> B(x)"} & @{text "x \<notin> \<Gamma>"}}
   282   \qquad
   283   \infer{@{text "\<Gamma> \<turnstile> B(a)"}}{@{text "\<Gamma> \<turnstile> \<And>x. B(x)"}}
   284   \]
   285 
   286   \[
   287   \infer{@{text "\<Gamma> - A \<turnstile> A \<Longrightarrow> B"}}{@{text "\<Gamma> \<turnstile> B"}}
   288   \qquad
   289   \infer{@{text "\<Gamma>\<^sub>1 \<union> \<Gamma>\<^sub>2 \<turnstile> B"}}{@{text "\<Gamma>\<^sub>1 \<turnstile> A \<Longrightarrow> B"} & @{text "\<Gamma>\<^sub>2 \<turnstile> A"}}
   290   \]
   291 
   292   Furthermore, Pure provides a built-in equality @{text "\<equiv> :: \<alpha> \<Rightarrow> \<alpha> \<Rightarrow>
   293   prop"} with axioms for reflexivity, substitution, extensionality,
   294   and @{text "\<alpha>\<beta>\<eta>"}-conversion on @{text "\<lambda>"}-terms.
   295 
   296   \medskip An object-logic introduces another layer on top of Pure,
   297   e.g.\ with types @{text "i"} for individuals and @{text "o"} for
   298   propositions, term constants @{text "Trueprop :: o \<Rightarrow> prop"} as
   299   (implicit) derivability judgment and connectives like @{text "\<and> :: o
   300   \<Rightarrow> o \<Rightarrow> o"} or @{text "\<forall> :: (i \<Rightarrow> o) \<Rightarrow> o"}, and axioms for object-level
   301   rules such as @{text "conjI: A \<Longrightarrow> B \<Longrightarrow> A \<and> B"} or @{text "allI: (\<And>x. B
   302   x) \<Longrightarrow> \<forall>x. B x"}.  Derived object rules are represented as theorems of
   303   Pure.  After the initial object-logic setup, further axiomatizations
   304   are usually avoided; plain definitions and derived principles are
   305   used exclusively.
   306 *}
   307 
   308 
   309 subsection {* Reasoning with rules \label{sec:framework-resolution} *}
   310 
   311 text {*
   312   Primitive inferences mostly serve foundational purposes.  The main
   313   reasoning mechanisms of Pure operate on nested natural deduction
   314   rules expressed as formulae, using @{text "\<And>"} to bind local
   315   parameters and @{text "\<Longrightarrow>"} to express entailment.  Multiple
   316   parameters and premises are represented by repeating these
   317   connectives in a right-associative manner.
   318 
   319   Since @{text "\<And>"} and @{text "\<Longrightarrow>"} commute thanks to the theorem
   320   @{prop "(A \<Longrightarrow> (\<And>x. B x)) \<equiv> (\<And>x. A \<Longrightarrow> B x)"}, we may assume w.l.o.g.\
   321   that rule statements always observe the normal form where
   322   quantifiers are pulled in front of implications at each level of
   323   nesting.  This means that any Pure proposition may be presented as a
   324   \emph{Hereditary Harrop Formula} \cite{Miller:1991} which is of the
   325   form @{text "\<And>x\<^isub>1 \<dots> x\<^isub>m. H\<^isub>1 \<Longrightarrow> \<dots> H\<^isub>n \<Longrightarrow>
   326   A"} for @{text "m, n \<ge> 0"}, and @{text "A"} atomic, and @{text
   327   "H\<^isub>1, \<dots>, H\<^isub>n"} being recursively of the same format.
   328   Following the convention that outermost quantifiers are implicit,
   329   Horn clauses @{text "A\<^isub>1 \<Longrightarrow> \<dots> A\<^isub>n \<Longrightarrow> A"} are a special
   330   case of this.
   331 
   332   For example, @{text "\<inter>"}-introduction rule encountered before is
   333   represented as a Pure theorem as follows:
   334   \[
   335   @{text "IntI:"}~@{prop "x \<in> A \<Longrightarrow> x \<in> B \<Longrightarrow> x \<in> A \<inter> B"}
   336   \]
   337 
   338   \noindent This is a plain Horn clause, since no further nesting on
   339   the left is involved.  The general @{text "\<Inter>"}-introduction
   340   corresponds to a Hereditary Harrop Formula with one additional level
   341   of nesting:
   342   \[
   343   @{text "InterI:"}~@{prop "(\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A) \<Longrightarrow> x \<in> \<Inter>\<A>"}
   344   \]
   345 
   346   \medskip Goals are also represented as rules: @{text "A\<^isub>1 \<Longrightarrow>
   347   \<dots> A\<^isub>n \<Longrightarrow> C"} states that the sub-goals @{text "A\<^isub>1, \<dots>,
   348   A\<^isub>n"} entail the result @{text "C"}; for @{text "n = 0"} the
   349   goal is finished.  To allow @{text "C"} being a rule statement
   350   itself, we introduce the protective marker @{text "# :: prop \<Rightarrow>
   351   prop"}, which is defined as identity and hidden from the user.  We
   352   initialize and finish goal states as follows:
   353 
   354   \[
   355   \begin{array}{c@ {\qquad}c}
   356   \infer[(@{inference_def init})]{@{text "C \<Longrightarrow> #C"}}{} &
   357   \infer[(@{inference_def finish})]{@{text C}}{@{text "#C"}}
   358   \end{array}
   359   \]
   360 
   361   \noindent Goal states are refined in intermediate proof steps until
   362   a finished form is achieved.  Here the two main reasoning principles
   363   are @{inference resolution}, for back-chaining a rule against a
   364   sub-goal (replacing it by zero or more sub-goals), and @{inference
   365   assumption}, for solving a sub-goal (finding a short-circuit with
   366   local assumptions).  Below @{text "\<^vec>x"} stands for @{text
   367   "x\<^isub>1, \<dots>, x\<^isub>n"} (@{text "n \<ge> 0"}).
   368 
   369   \[
   370   \infer[(@{inference_def resolution})]
   371   {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> \<^vec>A (\<^vec>a \<^vec>x))\<vartheta> \<Longrightarrow> C\<vartheta>"}}
   372   {\begin{tabular}{rl}
   373     @{text "rule:"} &
   374     @{text "\<^vec>A \<^vec>a \<Longrightarrow> B \<^vec>a"} \\
   375     @{text "goal:"} &
   376     @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> B' \<^vec>x) \<Longrightarrow> C"} \\
   377     @{text "goal unifier:"} &
   378     @{text "(\<lambda>\<^vec>x. B (\<^vec>a \<^vec>x))\<vartheta> = B'\<vartheta>"} \\
   379    \end{tabular}}
   380   \]
   381 
   382   \medskip
   383 
   384   \[
   385   \infer[(@{inference_def assumption})]{@{text "C\<vartheta>"}}
   386   {\begin{tabular}{rl}
   387     @{text "goal:"} &
   388     @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> A \<^vec>x) \<Longrightarrow> C"} \\
   389     @{text "assm unifier:"} & @{text "A\<vartheta> = H\<^sub>i\<vartheta>"}~~\text{(for some~@{text "H\<^sub>i"})} \\
   390    \end{tabular}}
   391   \]
   392 
   393   The following trace illustrates goal-oriented reasoning in
   394   Isabelle/Pure:
   395 
   396   {\footnotesize
   397   \medskip
   398   \begin{tabular}{r@ {\quad}l}
   399   @{text "(A \<and> B \<Longrightarrow> B \<and> A) \<Longrightarrow> #(A \<and> B \<Longrightarrow> B \<and> A)"} & @{text "(init)"} \\
   400   @{text "(A \<and> B \<Longrightarrow> B) \<Longrightarrow> (A \<and> B \<Longrightarrow> A) \<Longrightarrow> #\<dots>"} & @{text "(resolution B \<Longrightarrow> A \<Longrightarrow> B \<and> A)"} \\
   401   @{text "(A \<and> B \<Longrightarrow> A \<and> B) \<Longrightarrow> (A \<and> B \<Longrightarrow> A) \<Longrightarrow> #\<dots>"} & @{text "(resolution A \<and> B \<Longrightarrow> B)"} \\
   402   @{text "(A \<and> B \<Longrightarrow> A) \<Longrightarrow> #\<dots>"} & @{text "(assumption)"} \\
   403   @{text "(A \<and> B \<Longrightarrow> B \<and> A) \<Longrightarrow> #\<dots>"} & @{text "(resolution A \<and> B \<Longrightarrow> A)"} \\
   404   @{text "#\<dots>"} & @{text "(assumption)"} \\
   405   @{text "A \<and> B \<Longrightarrow> B \<and> A"} & @{text "(finish)"} \\
   406   \end{tabular}
   407   \medskip
   408   }
   409 
   410   Compositions of @{inference assumption} after @{inference
   411   resolution} occurs quite often, typically in elimination steps.
   412   Traditional Isabelle tactics accommodate this by a combined
   413   @{inference_def elim_resolution} principle.  In contrast, Isar uses
   414   a slightly more refined combination, where the assumptions to be
   415   closed are marked explicitly, using again the protective marker
   416   @{text "#"}:
   417 
   418   \[
   419   \infer[(@{inference refinement})]
   420   {@{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> \<^vec>G' (\<^vec>a \<^vec>x))\<vartheta> \<Longrightarrow> C\<vartheta>"}}
   421   {\begin{tabular}{rl}
   422     @{text "sub\<dash>proof:"} &
   423     @{text "\<^vec>G \<^vec>a \<Longrightarrow> B \<^vec>a"} \\
   424     @{text "goal:"} &
   425     @{text "(\<And>\<^vec>x. \<^vec>H \<^vec>x \<Longrightarrow> B' \<^vec>x) \<Longrightarrow> C"} \\
   426     @{text "goal unifier:"} &
   427     @{text "(\<lambda>\<^vec>x. B (\<^vec>a \<^vec>x))\<vartheta> = B'\<vartheta>"} \\
   428     @{text "assm unifiers:"} &
   429     @{text "(\<lambda>\<^vec>x. G\<^sub>j (\<^vec>a \<^vec>x))\<vartheta> = #H\<^sub>i\<vartheta>"} \\
   430     & \quad (for each marked @{text "G\<^sub>j"} some @{text "#H\<^sub>i"}) \\
   431    \end{tabular}}
   432   \]
   433 
   434   \noindent Here the @{text "sub\<dash>proof"} rule stems from the
   435   main @{command fix}-@{command assume}-@{command show} outline of
   436   Isar (cf.\ \secref{sec:framework-subproof}): each assumption
   437   indicated in the text results in a marked premise @{text "G"} above.
   438   The marking enforces resolution against one of the sub-goal's
   439   premises.  Consequently, @{command fix}-@{command assume}-@{command
   440   show} enables to fit the result of a sub-proof quite robustly into a
   441   pending sub-goal, while maintaining a good measure of flexibility.
   442 *}
   443 
   444 
   445 section {* The Isar proof language \label{sec:framework-isar} *}
   446 
   447 text {*
   448   Structured proofs are presented as high-level expressions for
   449   composing entities of Pure (propositions, facts, and goals).  The
   450   Isar proof language allows to organize reasoning within the
   451   underlying rule calculus of Pure, but Isar is not another logical
   452   calculus!
   453 
   454   Isar is an exercise in sound minimalism.  Approximately half of the
   455   language is introduced as primitive, the rest defined as derived
   456   concepts.  The following grammar describes the core language
   457   (category @{text "proof"}), which is embedded into theory
   458   specification elements such as @{command theorem}; see also
   459   \secref{sec:framework-stmt} for the separate category @{text
   460   "statement"}.
   461 
   462   \medskip
   463   \begin{tabular}{rcl}
   464     @{text "theory\<dash>stmt"} & = & @{command "theorem"}~@{text "statement proof  |"}~~@{command "definition"}~@{text "\<dots>  |  \<dots>"} \\[1ex]
   465 
   466     @{text "proof"} & = & @{text "prfx\<^sup>*"}~@{command "proof"}~@{text "method\<^sup>? stmt\<^sup>*"}~@{command "qed"}~@{text "method\<^sup>?"} \\[1ex]
   467 
   468     @{text prfx} & = & @{command "using"}~@{text "facts"} \\
   469     & @{text "|"} & @{command "unfolding"}~@{text "facts"} \\
   470 
   471     @{text stmt} & = & @{command "{"}~@{text "stmt\<^sup>*"}~@{command "}"} \\
   472     & @{text "|"} & @{command "next"} \\
   473     & @{text "|"} & @{command "note"}~@{text "name = facts"} \\
   474     & @{text "|"} & @{command "let"}~@{text "term = term"} \\
   475     & @{text "|"} & @{command "fix"}~@{text "var\<^sup>+"} \\
   476     & @{text "|"} & @{command assume}~@{text "\<guillemotleft>inference\<guillemotright> name: props"} \\
   477     & @{text "|"} & @{command "then"}@{text "\<^sup>?"}~@{text goal} \\
   478     @{text goal} & = & @{command "have"}~@{text "name: props proof"} \\
   479     & @{text "|"} & @{command "show"}~@{text "name: props proof"} \\
   480   \end{tabular}
   481 
   482   \medskip Simultaneous propositions or facts may be separated by the
   483   @{keyword "and"} keyword.
   484 
   485   \medskip The syntax for terms and propositions is inherited from
   486   Pure (and the object-logic).  A @{text "pattern"} is a @{text
   487   "term"} with schematic variables, to be bound by higher-order
   488   matching.
   489 
   490   \medskip Facts may be referenced by name or proposition.  For
   491   example, the result of ``@{command have}~@{text "a: A \<langle>proof\<rangle>"}''
   492   becomes available both as @{text "a"} and
   493   \isacharbackquoteopen@{text "A"}\isacharbackquoteclose.  Moreover,
   494   fact expressions may involve attributes that modify either the
   495   theorem or the background context.  For example, the expression
   496   ``@{text "a [OF b]"}'' refers to the composition of two facts
   497   according to the @{inference resolution} inference of
   498   \secref{sec:framework-resolution}, while ``@{text "a [intro]"}''
   499   declares a fact as introduction rule in the context.
   500 
   501   The special fact called ``@{fact this}'' always refers to the last
   502   result, as produced by @{command note}, @{command assume}, @{command
   503   have}, or @{command show}.  Since @{command note} occurs
   504   frequently together with @{command then} we provide some
   505   abbreviations:
   506 
   507   \medskip
   508   \begin{tabular}{rcl}
   509     @{command from}~@{text a} & @{text "\<equiv>"} & @{command note}~@{text a}~@{command then} \\
   510     @{command with}~@{text a} & @{text "\<equiv>"} & @{command from}~@{text "a \<AND> this"} \\
   511   \end{tabular}
   512   \medskip
   513 
   514   The @{text "method"} category is essentially a parameter and may be
   515   populated later.  Methods use the facts indicated by @{command
   516   "then"} or @{command using}, and then operate on the goal state.
   517   Some basic methods are predefined: ``@{method "-"}'' leaves the goal
   518   unchanged, ``@{method this}'' applies the facts as rules to the
   519   goal, ``@{method "rule"}'' applies the facts to another rule and the
   520   result to the goal (both ``@{method this}'' and ``@{method rule}''
   521   refer to @{inference resolution} of
   522   \secref{sec:framework-resolution}).  The secondary arguments to
   523   ``@{method rule}'' may be specified explicitly as in ``@{text "(rule
   524   a)"}'', or picked from the context.  In the latter case, the system
   525   first tries rules declared as @{attribute (Pure) elim} or
   526   @{attribute (Pure) dest}, followed by those declared as @{attribute
   527   (Pure) intro}.
   528 
   529   The default method for @{command proof} is ``@{method rule}''
   530   (arguments picked from the context), for @{command qed} it is
   531   ``@{method "-"}''.  Further abbreviations for terminal proof steps
   532   are ``@{command "by"}~@{text "method\<^sub>1 method\<^sub>2"}'' for
   533   ``@{command proof}~@{text "method\<^sub>1"}~@{command qed}~@{text
   534   "method\<^sub>2"}'', and ``@{command ".."}'' for ``@{command
   535   "by"}~@{method rule}, and ``@{command "."}'' for ``@{command
   536   "by"}~@{method this}''.  The @{command unfolding} element operates
   537   directly on the current facts and goal by applying equalities.
   538 
   539   \medskip Block structure can be indicated explicitly by ``@{command
   540   "{"}~@{text "\<dots>"}~@{command "}"}'', although the body of a sub-proof
   541   already involves implicit nesting.  In any case, @{command next}
   542   jumps into the next section of a block, i.e.\ it acts like closing
   543   an implicit block scope and opening another one; there is no direct
   544   correspondence to subgoals here.
   545 
   546   The remaining elements @{command fix} and @{command assume} build up
   547   a local context (see \secref{sec:framework-context}), while
   548   @{command show} refines a pending sub-goal by the rule resulting
   549   from a nested sub-proof (see \secref{sec:framework-subproof}).
   550   Further derived concepts will support calculational reasoning (see
   551   \secref{sec:framework-calc}).
   552 *}
   553 
   554 
   555 subsection {* Context elements \label{sec:framework-context} *}
   556 
   557 text {*
   558   In judgments @{text "\<Gamma> \<turnstile> \<phi>"} of the primitive framework, @{text "\<Gamma>"}
   559   essentially acts like a proof context.  Isar elaborates this idea
   560   towards a higher-level notion, with additional information for
   561   type-inference, term abbreviations, local facts, hypotheses etc.
   562 
   563   The element @{command fix}~@{text "x :: \<alpha>"} declares a local
   564   parameter, i.e.\ an arbitrary-but-fixed entity of a given type; in
   565   results exported from the context, @{text "x"} may become anything.
   566   The @{command assume}~@{text "\<guillemotleft>inference\<guillemotright>"} element provides a
   567   general interface to hypotheses: ``@{command assume}~@{text
   568   "\<guillemotleft>inference\<guillemotright> A"}'' produces @{text "A \<turnstile> A"} locally, while the
   569   included inference tells how to discharge @{text A} from results
   570   @{text "A \<turnstile> B"} later on.  There is no user-syntax for @{text
   571   "\<guillemotleft>inference\<guillemotright>"}, i.e.\ it may only occur internally when derived
   572   commands are defined in ML.
   573 
   574   At the user-level, the default inference for @{command assume} is
   575   @{inference discharge} as given below.  The additional variants
   576   @{command presume} and @{command def} are defined as follows:
   577 
   578   \medskip
   579   \begin{tabular}{rcl}
   580     @{command presume}~@{text A} & @{text "\<equiv>"} & @{command assume}~@{text "\<guillemotleft>weak\<dash>discharge\<guillemotright> A"} \\
   581     @{command def}~@{text "x \<equiv> a"} & @{text "\<equiv>"} & @{command fix}~@{text x}~@{command assume}~@{text "\<guillemotleft>expansion\<guillemotright> x \<equiv> a"} \\
   582   \end{tabular}
   583   \medskip
   584 
   585   \[
   586   \infer[(@{inference_def discharge})]{@{text "\<strut>\<Gamma> - A \<turnstile> #A \<Longrightarrow> B"}}{@{text "\<strut>\<Gamma> \<turnstile> B"}}
   587   \]
   588   \[
   589   \infer[(@{inference_def "weak\<dash>discharge"})]{@{text "\<strut>\<Gamma> - A \<turnstile> A \<Longrightarrow> B"}}{@{text "\<strut>\<Gamma> \<turnstile> B"}}
   590   \]
   591   \[
   592   \infer[(@{inference_def expansion})]{@{text "\<strut>\<Gamma> - (x \<equiv> a) \<turnstile> B a"}}{@{text "\<strut>\<Gamma> \<turnstile> B x"}}
   593   \]
   594 
   595   \medskip Note that @{inference discharge} and @{inference
   596   "weak\<dash>discharge"} differ in the marker for @{prop A}, which is
   597   relevant when the result of a @{command fix}-@{command
   598   assume}-@{command show} outline is composed with a pending goal,
   599   cf.\ \secref{sec:framework-subproof}.
   600 
   601   The most interesting derived context element in Isar is @{command
   602   obtain} \cite[\S5.3]{Wenzel-PhD}, which supports generalized
   603   elimination steps in a purely forward manner.  The @{command obtain}
   604   command takes a specification of parameters @{text "\<^vec>x"} and
   605   assumptions @{text "\<^vec>A"} to be added to the context, together
   606   with a proof of a case rule stating that this extension is
   607   conservative (i.e.\ may be removed from closed results later on):
   608 
   609   \medskip
   610   \begin{tabular}{l}
   611   @{text "\<langle>facts\<rangle>"}~~@{command obtain}~@{text "\<^vec>x \<WHERE> \<^vec>A \<^vec>x  \<langle>proof\<rangle> \<equiv>"} \\[0.5ex]
   612   \quad @{command have}~@{text "case: \<And>thesis. (\<And>\<^vec>x. \<^vec>A \<^vec>x \<Longrightarrow> thesis) \<Longrightarrow> thesis\<rangle>"} \\
   613   \quad @{command proof}~@{method "-"} \\
   614   \qquad @{command fix}~@{text thesis} \\
   615   \qquad @{command assume}~@{text "[intro]: \<And>\<^vec>x. \<^vec>A \<^vec>x \<Longrightarrow> thesis"} \\
   616   \qquad @{command show}~@{text thesis}~@{command using}~@{text "\<langle>facts\<rangle> \<langle>proof\<rangle>"} \\
   617   \quad @{command qed} \\
   618   \quad @{command fix}~@{text "\<^vec>x"}~@{command assume}~@{text "\<guillemotleft>elimination case\<guillemotright> \<^vec>A \<^vec>x"} \\
   619   \end{tabular}
   620   \medskip
   621 
   622   \[
   623   \infer[(@{inference elimination})]{@{text "\<Gamma> \<turnstile> B"}}{
   624     \begin{tabular}{rl}
   625     @{text "case:"} &
   626     @{text "\<Gamma> \<turnstile> \<And>thesis. (\<And>\<^vec>x. \<^vec>A \<^vec>x \<Longrightarrow> thesis) \<Longrightarrow> thesis"} \\[0.2ex]
   627     @{text "result:"} &
   628     @{text "\<Gamma> \<union> \<^vec>A \<^vec>y \<turnstile> B"} \\[0.2ex]
   629     \end{tabular}}
   630   \]
   631 
   632   \noindent Here the name ``@{text thesis}'' is a specific convention
   633   for an arbitrary-but-fixed proposition; in the primitive natural
   634   deduction rules shown before we have occasionally used @{text C}.
   635   The whole statement of ``@{command obtain}~@{text x}~@{keyword
   636   "where"}~@{text "A x"}'' may be read as a claim that @{text "A x"}
   637   may be assumed for some arbitrary-but-fixed @{text "x"}.  Also note
   638   that ``@{command obtain}~@{text "A \<AND> B"}'' without parameters
   639   is similar to ``@{command have}~@{text "A \<AND> B"}'', but the
   640   latter involves multiple sub-goals.
   641 
   642   \medskip The subsequent Isar proof texts explain all context
   643   elements introduced above using the formal proof language itself.
   644   After finishing a local proof within a block, we indicate the
   645   exported result via @{command note}.
   646 *}
   647 
   648 (*<*)
   649 theorem True
   650 proof
   651 (*>*)
   652   txt_raw {* \begin{minipage}[t]{0.4\textwidth} *}
   653   {
   654     fix x
   655     have "B x" sorry %noproof
   656   }
   657   note `\<And>x. B x`
   658   txt_raw {* \end{minipage}\quad\begin{minipage}[t]{0.4\textwidth} *}(*<*)next(*>*)
   659   {
   660     assume A
   661     have B sorry %noproof
   662   }
   663   note `A \<Longrightarrow> B`
   664   txt_raw {* \end{minipage}\\[3ex]\begin{minipage}[t]{0.4\textwidth} *}(*<*)next(*>*)
   665   {
   666     def x \<equiv> a
   667     have "B x" sorry %noproof
   668   }
   669   note `B a`
   670   txt_raw {* \end{minipage}\quad\begin{minipage}[t]{0.4\textwidth} *}(*<*)next(*>*)
   671   {
   672     obtain x where "A x" sorry %noproof
   673     have B sorry %noproof
   674   }
   675   note `B`
   676   txt_raw {* \end{minipage} *}
   677 (*<*)
   678 qed
   679 (*>*)
   680 
   681 text {*
   682   \bigskip\noindent This illustrates the meaning of Isar context
   683   elements without goals getting in between.
   684 *}
   685 
   686 subsection {* Structured statements \label{sec:framework-stmt} *}
   687 
   688 text {*
   689   The category @{text "statement"} of top-level theorem specifications
   690   is defined as follows:
   691 
   692   \medskip
   693   \begin{tabular}{rcl}
   694   @{text "statement"} & @{text "\<equiv>"} & @{text "name: props \<AND> \<dots>"} \\
   695   & @{text "|"} & @{text "context\<^sup>* conclusion"} \\[0.5ex]
   696 
   697   @{text "context"} & @{text "\<equiv>"} & @{text "\<FIXES> vars \<AND> \<dots>"} \\
   698   & @{text "|"} & @{text "\<ASSUMES> name: props \<AND> \<dots>"} \\
   699 
   700   @{text "conclusion"} & @{text "\<equiv>"} & @{text "\<SHOWS> name: props \<AND> \<dots>"} \\
   701   & @{text "|"} & @{text "\<OBTAINS> vars \<AND> \<dots> \<WHERE> name: props \<AND> \<dots>"} \\
   702   & & \quad @{text "\<BBAR> \<dots>"} \\
   703   \end{tabular}
   704 
   705   \medskip\noindent A simple @{text "statement"} consists of named
   706   propositions.  The full form admits local context elements followed
   707   by the actual conclusions, such as ``@{keyword "fixes"}~@{text
   708   x}~@{keyword "assumes"}~@{text "A x"}~@{keyword "shows"}~@{text "B
   709   x"}''.  The final result emerges as a Pure rule after discharging
   710   the context: @{prop "\<And>x. A x \<Longrightarrow> B x"}.
   711 
   712   The @{keyword "obtains"} variant is another abbreviation defined
   713   below; unlike @{command obtain} (cf.\
   714   \secref{sec:framework-context}) there may be several ``cases''
   715   separated by ``@{text "\<BBAR>"}'', each consisting of several
   716   parameters (@{text "vars"}) and several premises (@{text "props"}).
   717   This specifies multi-branch elimination rules.
   718 
   719   \medskip
   720   \begin{tabular}{l}
   721   @{text "\<OBTAINS> \<^vec>x \<WHERE> \<^vec>A \<^vec>x   \<BBAR>   \<dots>   \<equiv>"} \\[0.5ex]
   722   \quad @{text "\<FIXES> thesis"} \\
   723   \quad @{text "\<ASSUMES> [intro]: \<And>\<^vec>x. \<^vec>A \<^vec>x \<Longrightarrow> thesis  \<AND>  \<dots>"} \\
   724   \quad @{text "\<SHOWS> thesis"} \\
   725   \end{tabular}
   726   \medskip
   727 
   728   Presenting structured statements in such an ``open'' format usually
   729   simplifies the subsequent proof, because the outer structure of the
   730   problem is already laid out directly.  E.g.\ consider the following
   731   canonical patterns for @{text "\<SHOWS>"} and @{text "\<OBTAINS>"},
   732   respectively:
   733 *}
   734 
   735 text_raw {*\begin{minipage}{0.5\textwidth}*}
   736 
   737 theorem
   738   fixes x and y
   739   assumes "A x" and "B y"
   740   shows "C x y"
   741 proof -
   742   from `A x` and `B y`
   743   show "C x y" sorry %noproof
   744 qed
   745 
   746 text_raw {*\end{minipage}\begin{minipage}{0.5\textwidth}*}
   747 
   748 theorem
   749   obtains x and y
   750   where "A x" and "B y"
   751 proof -
   752   have "A a" and "B b" sorry %noproof
   753   then show thesis ..
   754 qed
   755 
   756 text_raw {*\end{minipage}*}
   757 
   758 text {*
   759   \medskip\noindent Here local facts \isacharbackquoteopen@{text "A
   760   x"}\isacharbackquoteclose\ and \isacharbackquoteopen@{text "B
   761   y"}\isacharbackquoteclose\ are referenced immediately; there is no
   762   need to decompose the logical rule structure again.  In the second
   763   proof the final ``@{command then}~@{command show}~@{text
   764   thesis}~@{command ".."}''  involves the local rule case @{text "\<And>x
   765   y. A x \<Longrightarrow> B y \<Longrightarrow> thesis"} for the particular instance of terms @{text
   766   "a"} and @{text "b"} produced in the body.
   767 *}
   768 
   769 
   770 subsection {* Structured proof refinement \label{sec:framework-subproof} *}
   771 
   772 text {*
   773   By breaking up the grammar for the Isar proof language, we may
   774   understand a proof text as a linear sequence of individual proof
   775   commands.  These are interpreted as transitions of the Isar virtual
   776   machine (Isar/VM), which operates on a block-structured
   777   configuration in single steps.  This allows users to write proof
   778   texts in an incremental manner, and inspect intermediate
   779   configurations for debugging.
   780 
   781   The basic idea is analogous to evaluating algebraic expressions on a
   782   stack machine: @{text "(a + b) \<cdot> c"} then corresponds to a sequence
   783   of single transitions for each symbol @{text "(, a, +, b, ), \<cdot>, c"}.
   784   In Isar the algebraic values are facts or goals, and the operations
   785   are inferences.
   786 
   787   \medskip The Isar/VM state maintains a stack of nodes, each node
   788   contains the local proof context, the linguistic mode, and a pending
   789   goal (optional).  The mode determines the type of transition that
   790   may be performed next, it essentially alternates between forward and
   791   backward reasoning, with an intermediate stage for chained facts
   792   (see \figref{fig:isar-vm}).
   793 
   794   \begin{figure}[htb]
   795   \begin{center}
   796   \includegraphics[width=0.8\textwidth]{Thy/document/isar-vm}
   797   \end{center}
   798   \caption{Isar/VM modes}\label{fig:isar-vm}
   799   \end{figure}
   800 
   801   For example, in @{text "state"} mode Isar acts like a mathematical
   802   scratch-pad, accepting declarations like @{command fix}, @{command
   803   assume}, and claims like @{command have}, @{command show}.  A goal
   804   statement changes the mode to @{text "prove"}, which means that we
   805   may now refine the problem via @{command unfolding} or @{command
   806   proof}.  Then we are again in @{text "state"} mode of a proof body,
   807   which may issue @{command show} statements to solve pending
   808   sub-goals.  A concluding @{command qed} will return to the original
   809   @{text "state"} mode one level upwards.  The subsequent Isar/VM
   810   trace indicates block structure, linguistic mode, goal state, and
   811   inferences:
   812 *}
   813 
   814 text_raw {* \begingroup\footnotesize *}
   815 (*<*)example_proof
   816 (*>*)
   817   txt_raw {* \begin{minipage}[t]{0.18\textwidth} *}
   818   have "A \<longrightarrow> B"
   819   proof
   820     assume A
   821     show B
   822       sorry %noproof
   823   qed
   824   txt_raw {* \end{minipage}\quad
   825 \begin{minipage}[t]{0.06\textwidth}
   826 @{text "begin"} \\
   827 \\
   828 \\
   829 @{text "begin"} \\
   830 @{text "end"} \\
   831 @{text "end"} \\
   832 \end{minipage}
   833 \begin{minipage}[t]{0.08\textwidth}
   834 @{text "prove"} \\
   835 @{text "state"} \\
   836 @{text "state"} \\
   837 @{text "prove"} \\
   838 @{text "state"} \\
   839 @{text "state"} \\
   840 \end{minipage}\begin{minipage}[t]{0.35\textwidth}
   841 @{text "(A \<longrightarrow> B) \<Longrightarrow> #(A \<longrightarrow> B)"} \\
   842 @{text "(A \<Longrightarrow> B) \<Longrightarrow> #(A \<longrightarrow> B)"} \\
   843 \\
   844 \\
   845 @{text "#(A \<longrightarrow> B)"} \\
   846 @{text "A \<longrightarrow> B"} \\
   847 \end{minipage}\begin{minipage}[t]{0.4\textwidth}
   848 @{text "(init)"} \\
   849 @{text "(resolution impI)"} \\
   850 \\
   851 \\
   852 @{text "(refinement #A \<Longrightarrow> B)"} \\
   853 @{text "(finish)"} \\
   854 \end{minipage} *}
   855 (*<*)
   856 qed
   857 (*>*)
   858 text_raw {* \endgroup *}
   859 
   860 text {*
   861   \noindent Here the @{inference refinement} inference from
   862   \secref{sec:framework-resolution} mediates composition of Isar
   863   sub-proofs nicely.  Observe that this principle incorporates some
   864   degree of freedom in proof composition.  In particular, the proof
   865   body allows parameters and assumptions to be re-ordered, or commuted
   866   according to Hereditary Harrop Form.  Moreover, context elements
   867   that are not used in a sub-proof may be omitted altogether.  For
   868   example:
   869 *}
   870 
   871 text_raw {*\begin{minipage}{0.5\textwidth}*}
   872 
   873 (*<*)
   874 example_proof
   875 (*>*)
   876   have "\<And>x y. A x \<Longrightarrow> B y \<Longrightarrow> C x y"
   877   proof -
   878     fix x and y
   879     assume "A x" and "B y"
   880     show "C x y" sorry %noproof
   881   qed
   882 
   883 txt_raw {*\end{minipage}\begin{minipage}{0.5\textwidth}*}
   884 
   885 (*<*)
   886 next
   887 (*>*)
   888   have "\<And>x y. A x \<Longrightarrow> B y \<Longrightarrow> C x y"
   889   proof -
   890     fix x assume "A x"
   891     fix y assume "B y"
   892     show "C x y" sorry %noproof
   893   qed
   894 
   895 txt_raw {*\end{minipage}\\[3ex]\begin{minipage}{0.5\textwidth}*}
   896 
   897 (*<*)
   898 next
   899 (*>*)
   900   have "\<And>x y. A x \<Longrightarrow> B y \<Longrightarrow> C x y"
   901   proof -
   902     fix y assume "B y"
   903     fix x assume "A x"
   904     show "C x y" sorry
   905   qed
   906 
   907 txt_raw {*\end{minipage}\begin{minipage}{0.5\textwidth}*}
   908 (*<*)
   909 next
   910 (*>*)
   911   have "\<And>x y. A x \<Longrightarrow> B y \<Longrightarrow> C x y"
   912   proof -
   913     fix y assume "B y"
   914     fix x
   915     show "C x y" sorry
   916   qed
   917 (*<*)
   918 qed
   919 (*>*)
   920 
   921 text_raw {*\end{minipage}*}
   922 
   923 text {*
   924   \medskip\noindent Such ``peephole optimizations'' of Isar texts are
   925   practically important to improve readability, by rearranging
   926   contexts elements according to the natural flow of reasoning in the
   927   body, while still observing the overall scoping rules.
   928 
   929   \medskip This illustrates the basic idea of structured proof
   930   processing in Isar.  The main mechanisms are based on natural
   931   deduction rule composition within the Pure framework.  In
   932   particular, there are no direct operations on goal states within the
   933   proof body.  Moreover, there is no hidden automated reasoning
   934   involved, just plain unification.
   935 *}
   936 
   937 
   938 subsection {* Calculational reasoning \label{sec:framework-calc} *}
   939 
   940 text {*
   941   The existing Isar infrastructure is sufficiently flexible to support
   942   calculational reasoning (chains of transitivity steps) as derived
   943   concept.  The generic proof elements introduced below depend on
   944   rules declared as @{attribute trans} in the context.  It is left to
   945   the object-logic to provide a suitable rule collection for mixed
   946   relations of @{text "="}, @{text "<"}, @{text "\<le>"}, @{text "\<subset>"},
   947   @{text "\<subseteq>"} etc.  Due to the flexibility of rule composition
   948   (\secref{sec:framework-resolution}), substitution of equals by
   949   equals is covered as well, even substitution of inequalities
   950   involving monotonicity conditions; see also \cite[\S6]{Wenzel-PhD}
   951   and \cite{Bauer-Wenzel:2001}.
   952 
   953   The generic calculational mechanism is based on the observation that
   954   rules such as @{text "trans:"}~@{prop "x = y \<Longrightarrow> y = z \<Longrightarrow> x = z"}
   955   proceed from the premises towards the conclusion in a deterministic
   956   fashion.  Thus we may reason in forward mode, feeding intermediate
   957   results into rules selected from the context.  The course of
   958   reasoning is organized by maintaining a secondary fact called
   959   ``@{fact calculation}'', apart from the primary ``@{fact this}''
   960   already provided by the Isar primitives.  In the definitions below,
   961   @{attribute OF} refers to @{inference resolution}
   962   (\secref{sec:framework-resolution}) with multiple rule arguments,
   963   and @{text "trans"} represents to a suitable rule from the context:
   964 
   965   \begin{matharray}{rcl}
   966     @{command "also"}@{text "\<^sub>0"} & \equiv & @{command "note"}~@{text "calculation = this"} \\
   967     @{command "also"}@{text "\<^sub>n\<^sub>+\<^sub>1"} & \equiv & @{command "note"}~@{text "calculation = trans [OF calculation this]"} \\[0.5ex]
   968     @{command "finally"} & \equiv & @{command "also"}~@{command "from"}~@{text calculation} \\
   969   \end{matharray}
   970 
   971   \noindent The start of a calculation is determined implicitly in the
   972   text: here @{command also} sets @{fact calculation} to the current
   973   result; any subsequent occurrence will update @{fact calculation} by
   974   combination with the next result and a transitivity rule.  The
   975   calculational sequence is concluded via @{command finally}, where
   976   the final result is exposed for use in a concluding claim.
   977 
   978   Here is a canonical proof pattern, using @{command have} to
   979   establish the intermediate results:
   980 *}
   981 
   982 (*<*)
   983 example_proof
   984 (*>*)
   985   have "a = b" sorry
   986   also have "\<dots> = c" sorry
   987   also have "\<dots> = d" sorry
   988   finally have "a = d" .
   989 (*<*)
   990 qed
   991 (*>*)
   992 
   993 text {*
   994   \noindent The term ``@{text "\<dots>"}'' above is a special abbreviation
   995   provided by the Isabelle/Isar syntax layer: it statically refers to
   996   the right-hand side argument of the previous statement given in the
   997   text.  Thus it happens to coincide with relevant sub-expressions in
   998   the calculational chain, but the exact correspondence is dependent
   999   on the transitivity rules being involved.
  1000 
  1001   \medskip Symmetry rules such as @{prop "x = y \<Longrightarrow> y = x"} are like
  1002   transitivities with only one premise.  Isar maintains a separate
  1003   rule collection declared via the @{attribute sym} attribute, to be
  1004   used in fact expressions ``@{text "a [symmetric]"}'', or single-step
  1005   proofs ``@{command assume}~@{text "x = y"}~@{command then}~@{command
  1006   have}~@{text "y = x"}~@{command ".."}''.
  1007 *}
  1008 
  1009 end