doc-src/IsarImplementation/Thy/Isar.thy
author wenzelm
Wed Oct 13 13:05:23 2010 +0100 (2010-10-13)
changeset 39846 cb6634eb8926
parent 39845 50f42116ebdb
child 39847 da8c3fc5e314
permissions -rw-r--r--
examples in Isabelle/HOL;
tuned;
wenzelm@29755
     1
theory Isar
wenzelm@29755
     2
imports Base
wenzelm@29755
     3
begin
wenzelm@20472
     4
wenzelm@29759
     5
chapter {* Isar language elements *}
wenzelm@29759
     6
wenzelm@39844
     7
text {*
wenzelm@39844
     8
  The Isar proof language (see also \cite[\S2]{isabelle-isar-ref})
wenzelm@39844
     9
  consists of three main categories of language elements:
wenzelm@29759
    10
wenzelm@29759
    11
  \begin{enumerate}
wenzelm@29759
    12
wenzelm@39842
    13
  \item Proof \emph{commands} define the primary language of
wenzelm@39842
    14
  transactions of the underlying Isar/VM interpreter.  Typical
wenzelm@39842
    15
  examples are @{command "fix"}, @{command "assume"}, @{command
wenzelm@39844
    16
  "show"}, @{command "proof"}, and @{command "qed"}.
wenzelm@39842
    17
wenzelm@39844
    18
  Composing proof commands according to the rules of the Isar/VM leads
wenzelm@39844
    19
  to expressions of structured proof text, such that both the machine
wenzelm@39844
    20
  and the human reader can give it a meaning as formal reasoning.
wenzelm@20472
    21
wenzelm@39842
    22
  \item Proof \emph{methods} define a secondary language of mixed
wenzelm@39842
    23
  forward-backward refinement steps involving facts and goals.
wenzelm@39846
    24
  Typical examples are @{method rule}, @{method unfold}, and @{method
wenzelm@39844
    25
  simp}.
wenzelm@29759
    26
wenzelm@39842
    27
  Methods can occur in certain well-defined parts of the Isar proof
wenzelm@39842
    28
  language, say as arguments to @{command "proof"}, @{command "qed"},
wenzelm@39842
    29
  or @{command "by"}.
wenzelm@39842
    30
wenzelm@39842
    31
  \item \emph{Attributes} define a tertiary language of small
wenzelm@39844
    32
  annotations to facts being defined or referenced.  Attributes can
wenzelm@39844
    33
  modify both the fact and the context.
wenzelm@39842
    34
wenzelm@39844
    35
  Typical examples are @{attribute symmetric} (which affects the
wenzelm@39844
    36
  fact), and @{attribute intro} (which affects the context).
wenzelm@29759
    37
wenzelm@29759
    38
  \end{enumerate}
wenzelm@29759
    39
*}
wenzelm@29759
    40
wenzelm@29759
    41
wenzelm@29759
    42
section {* Proof commands *}
wenzelm@20520
    43
wenzelm@39842
    44
text {* In principle, Isar proof commands could be defined in
wenzelm@39842
    45
  user-space as well.  The system is built like that in the first
wenzelm@39842
    46
  place: part of the commands are primitive, the other part is defined
wenzelm@39842
    47
  as derived elements.  Adding to the genuine structured proof
wenzelm@39842
    48
  language requires profound understanding of the Isar/VM machinery,
wenzelm@39844
    49
  though, so this is beyond the scope of this manual.
wenzelm@39842
    50
wenzelm@39842
    51
  What can be done realistically is to define some diagnostic commands
wenzelm@39844
    52
  that inspect the general state of the Isar/VM, and report some
wenzelm@39844
    53
  feedback to the user.  Typically this involves checking of the
wenzelm@39842
    54
  linguistic \emph{mode} of a proof state, or peeking at the pending
wenzelm@39842
    55
  goals (if available).
wenzelm@39845
    56
wenzelm@39845
    57
  Another common application is to define a toplevel command that
wenzelm@39845
    58
  poses a problem to the user as Isar proof state and stores the final
wenzelm@39845
    59
  result in a suitable context data slot.  Thus a proof can be
wenzelm@39845
    60
  incorporated into the context of some user-space tool, without
wenzelm@39845
    61
  modifying the Isar proof language itself.
wenzelm@39842
    62
*}
wenzelm@39842
    63
wenzelm@39842
    64
text %mlref {*
wenzelm@39842
    65
  \begin{mldecls}
wenzelm@39842
    66
  @{index_ML_type Proof.state} \\
wenzelm@39842
    67
  @{index_ML Proof.assert_forward: "Proof.state -> Proof.state"} \\
wenzelm@39842
    68
  @{index_ML Proof.assert_chain: "Proof.state -> Proof.state"} \\
wenzelm@39842
    69
  @{index_ML Proof.assert_backward: "Proof.state -> Proof.state"} \\
wenzelm@39842
    70
  @{index_ML Proof.simple_goal: "Proof.state -> {context: Proof.context, goal: thm}"} \\
wenzelm@39842
    71
  @{index_ML Proof.goal: "Proof.state ->
wenzelm@39842
    72
  {context: Proof.context, facts: thm list, goal: thm}"} \\
wenzelm@39842
    73
  @{index_ML Proof.raw_goal: "Proof.state ->
wenzelm@39842
    74
  {context: Proof.context, facts: thm list, goal: thm}"} \\
wenzelm@39845
    75
  @{index_ML Proof.theorem: "Method.text option ->
wenzelm@39845
    76
  (thm list list -> Proof.context -> Proof.context) ->
wenzelm@39845
    77
  (term * term list) list list -> Proof.context -> Proof.state"} \\
wenzelm@39842
    78
  \end{mldecls}
wenzelm@39842
    79
wenzelm@39842
    80
  \begin{description}
wenzelm@39842
    81
wenzelm@39842
    82
  \item @{ML_type Proof.state} represents Isar proof states.  This is
wenzelm@39842
    83
  a block-structured configuration with proof context, linguistic
wenzelm@39844
    84
  mode, and optional goal.  The latter consists of goal context, goal
wenzelm@39844
    85
  facts (``@{text "using"}''), and tactical goal state (see
wenzelm@39844
    86
  \secref{sec:tactical-goals}).
wenzelm@39842
    87
wenzelm@39842
    88
  The general idea is that the facts shall contribute to the
wenzelm@39844
    89
  refinement of some parts of the tactical goal --- how exactly is
wenzelm@39844
    90
  defined by the proof method that is applied in that situation.
wenzelm@39842
    91
wenzelm@39842
    92
  \item @{ML Proof.assert_forward}, @{ML Proof.assert_chain}, @{ML
wenzelm@39842
    93
  Proof.assert_backward} are partial identity functions that fail
wenzelm@39842
    94
  unless a certain linguistic mode is active, namely ``@{text
wenzelm@39842
    95
  "proof(state)"}'', ``@{text "proof(chain)"}'', ``@{text
wenzelm@39842
    96
  "proof(prove)"}'', respectively (using the terminology of
wenzelm@39842
    97
  \cite{isabelle-isar-ref}).
wenzelm@39842
    98
wenzelm@39842
    99
  It is advisable study the implementations of existing proof commands
wenzelm@39842
   100
  for suitable modes to be asserted.
wenzelm@39842
   101
wenzelm@39842
   102
  \item @{ML Proof.simple_goal}~@{text "state"} returns the structured
wenzelm@39842
   103
  Isar goal (if available) in the form seen by ``simple'' methods
wenzelm@39846
   104
  (like @{method simp} or @{method blast}).  The Isar goal facts are
wenzelm@39842
   105
  already inserted as premises into the subgoals, which are presented
wenzelm@39844
   106
  individually as in @{ML Proof.goal}.
wenzelm@39842
   107
wenzelm@39842
   108
  \item @{ML Proof.goal}~@{text "state"} returns the structured Isar
wenzelm@39842
   109
  goal (if available) in the form seen by regular methods (like
wenzelm@39842
   110
  @{method rule}).  The auxiliary internal encoding of Pure
wenzelm@39842
   111
  conjunctions is split into individual subgoals as usual.
wenzelm@39842
   112
wenzelm@39842
   113
  \item @{ML Proof.raw_goal}~@{text "state"} returns the structured
wenzelm@39842
   114
  Isar goal (if available) in the raw internal form seen by ``raw''
wenzelm@39846
   115
  methods (like @{method induct}).  This form is rarely appropriate
wenzelm@39846
   116
  for dignostic tools; @{ML Proof.simple_goal} or @{ML Proof.goal}
wenzelm@39846
   117
  should be used in most situations.
wenzelm@39842
   118
wenzelm@39845
   119
  \item @{ML Proof.theorem}~@{text "before_qed after_qed statement ctxt"}
wenzelm@39845
   120
  initializes a toplevel Isar proof state within a given context.
wenzelm@39845
   121
wenzelm@39845
   122
  The optional @{text "before_qed"} method is applied at the end of
wenzelm@39845
   123
  the proof, just before extracting the result (this feature is rarely
wenzelm@39845
   124
  used).
wenzelm@39845
   125
wenzelm@39845
   126
  The @{text "after_qed"} continuation receives the extracted result
wenzelm@39845
   127
  in order to apply it to the final context in a suitable way (e.g.\
wenzelm@39845
   128
  storing named facts).  Note that at this generic level the target
wenzelm@39845
   129
  context is specified as @{ML_type Proof.context}, but the usual
wenzelm@39845
   130
  wrapping of toplevel proofs into command transactions will provide a
wenzelm@39845
   131
  @{ML_type local_theory} here (see also \chref{ch:local-theory}).
wenzelm@39845
   132
  This usually affects the way how results are stored.
wenzelm@39845
   133
wenzelm@39845
   134
  The @{text "statement"} is given as a nested list of terms, each
wenzelm@39845
   135
  associated with optional @{keyword "is"} patterns as usual in the
wenzelm@39845
   136
  Isar source language.  The original list structure over terms is
wenzelm@39845
   137
  turned into one over theorems when @{text "after_qed"} is invoked.
wenzelm@39845
   138
wenzelm@39842
   139
  \end{description}
wenzelm@39842
   140
*}
wenzelm@39842
   141
wenzelm@20520
   142
wenzelm@39843
   143
text %mlantiq {*
wenzelm@39843
   144
  \begin{matharray}{rcl}
wenzelm@39843
   145
  @{ML_antiquotation_def "Isar.goal"} & : & @{text ML_antiquotation} \\
wenzelm@39843
   146
  \end{matharray}
wenzelm@39843
   147
wenzelm@39843
   148
  \begin{description}
wenzelm@39843
   149
wenzelm@39843
   150
  \item @{text "@{Isar.goal}"} refers to the regular goal state (if
wenzelm@39843
   151
  available) of the current proof state managed by the Isar toplevel
wenzelm@39843
   152
  --- as abstract value.
wenzelm@39843
   153
wenzelm@39843
   154
  This only works for diagnostic ML commands, such as @{command
wenzelm@39843
   155
  ML_val} or @{command ML_command}.
wenzelm@39843
   156
wenzelm@39843
   157
  \end{description}
wenzelm@39843
   158
*}
wenzelm@39843
   159
wenzelm@39843
   160
text %mlex {* The following example peeks at a certain goal configuration. *}
wenzelm@39843
   161
wenzelm@39843
   162
example_proof
wenzelm@39846
   163
  have A and B and C
wenzelm@39843
   164
    ML_val {* Thm.nprems_of (#goal @{Isar.goal}) *}
wenzelm@39843
   165
    oops
wenzelm@39843
   166
wenzelm@39843
   167
text {* \noindent Here we see 3 individual subgoals in the same way as
wenzelm@39843
   168
  regular proof methods would do.
wenzelm@39843
   169
*}
wenzelm@39843
   170
wenzelm@20520
   171
wenzelm@20472
   172
section {* Proof methods *}
wenzelm@20472
   173
wenzelm@39843
   174
text {* Proof methods are syntactically embedded into the Isar proof
wenzelm@39843
   175
  language as arguments to certain commands, e.g.\ @{command "by"} or
wenzelm@39844
   176
  @{command apply}.  User-space additions are reasonably easy by
wenzelm@39844
   177
  plugging suitable method-valued parser functions into the framework.
wenzelm@39843
   178
wenzelm@39843
   179
  Operationally, a proof method is like a structurally enhanced
wenzelm@39843
   180
  tactic: it operates on the full Isar goal configuration with
wenzelm@39844
   181
  context, goal facts, and tactical goal state.  Like a tactic, it
wenzelm@39843
   182
  enumerates possible follow-up goal states, with the potential
wenzelm@39844
   183
  addition of named extensions of the proof context (\emph{cases}).
wenzelm@39843
   184
wenzelm@39844
   185
  To get a better idea about the range of possibilities, consider the
wenzelm@39844
   186
  following Isar proof schemes.  First some general form of structured
wenzelm@39844
   187
  proof text:
wenzelm@39843
   188
wenzelm@39843
   189
  \medskip
wenzelm@39843
   190
  \begin{tabular}{l}
wenzelm@39843
   191
  @{command from}~@{text "facts\<^sub>1"}~@{command have}~@{text "props"}~@{command using}~@{text "facts\<^sub>2"} \\
wenzelm@39843
   192
  @{command proof}~@{text "(initial_method)"} \\
wenzelm@39843
   193
  \quad@{text "body"} \\
wenzelm@39843
   194
  @{command qed}~@{text "(terminal_method)"} \\
wenzelm@39843
   195
  \end{tabular}
wenzelm@39843
   196
  \medskip
wenzelm@39843
   197
wenzelm@39843
   198
  \noindent The goal configuration consists of @{text "facts\<^sub>1"} and
wenzelm@39843
   199
  @{text "facts\<^sub>2"} appended in that order, and various @{text "props"}
wenzelm@39844
   200
  being claimed here.  The @{text "initial_method"} is invoked with
wenzelm@39844
   201
  facts and goals together and refines the problem to something that
wenzelm@39844
   202
  is handled recursively in the proof @{text "body"}.  The @{text
wenzelm@39843
   203
  "terminal_method"} has another chance to finish-off any remaining
wenzelm@39843
   204
  subgoals, but it does not see the facts of the initial step.
wenzelm@39843
   205
wenzelm@39844
   206
  \medskip The second pattern illustrates unstructured proof scripts:
wenzelm@39843
   207
wenzelm@39844
   208
  \medskip
wenzelm@39843
   209
  \begin{tabular}{l}
wenzelm@39843
   210
  @{command have}~@{text "props"} \\
wenzelm@39844
   211
  \quad@{command using}~@{text "facts\<^sub>1"}~@{command apply}~@{text "method\<^sub>1"} \\
wenzelm@39843
   212
  \quad@{command apply}~@{text "method\<^sub>2"} \\
wenzelm@39844
   213
  \quad@{command using}~@{text "facts\<^sub>3"}~@{command apply}~@{text "method\<^sub>3"} \\
wenzelm@39843
   214
  \quad@{command done} \\
wenzelm@39843
   215
  \end{tabular}
wenzelm@39843
   216
  \medskip
wenzelm@39843
   217
wenzelm@39843
   218
  \noindent The @{text "method\<^sub>1"} operates on the original claim
wenzelm@39843
   219
  together while using @{text "facts\<^bsub>1\<^esub>"}.  Since the @{command apply}
wenzelm@39843
   220
  command structurally resets the facts, the @{text "method\<^sub>2"} will
wenzelm@39843
   221
  operate on the remaining goal state without facts.  The @{text
wenzelm@39844
   222
  "method\<^sub>3"} will see again a collection of @{text "facts\<^sub>3"} that has
wenzelm@39844
   223
  been inserted into the script explicitly.
wenzelm@39843
   224
wenzelm@39843
   225
  \medskip Empirically, Isar proof methods can be categorized as follows:
wenzelm@39843
   226
wenzelm@39843
   227
  \begin{enumerate}
wenzelm@39843
   228
wenzelm@39846
   229
  \item structured method with cases, e.g.\ @{method "induct"}
wenzelm@39843
   230
wenzelm@39846
   231
  \item regular method: strong emphasis on facts, e.g.\ @{method "rule"}
wenzelm@39843
   232
wenzelm@39846
   233
  \item simple method: weak emphasis on facts, merely inserted into
wenzelm@39846
   234
  subgoals, e.g.\ @{method "simp"}
wenzelm@39843
   235
wenzelm@39846
   236
  \item old-style tactic emulation, e.g. @{method "rule_tac"}
wenzelm@39843
   237
wenzelm@39843
   238
  \begin{itemize}
wenzelm@39843
   239
wenzelm@39843
   240
  \item naming convention @{text "foo_tac"}
wenzelm@39843
   241
wenzelm@39843
   242
  \item numeric goal addressing
wenzelm@39843
   243
wenzelm@39843
   244
  \item explicit references to internal goal state (invisible from text!)
wenzelm@39843
   245
wenzelm@39843
   246
  \end{itemize}
wenzelm@39843
   247
wenzelm@39843
   248
  \end{enumerate}
wenzelm@39843
   249
wenzelm@39843
   250
  FIXME
wenzelm@39843
   251
*}
wenzelm@20472
   252
wenzelm@29759
   253
wenzelm@20472
   254
section {* Attributes *}
wenzelm@20472
   255
wenzelm@29759
   256
text FIXME
wenzelm@30272
   257
wenzelm@20472
   258
end