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