doc-src/IsarImplementation/Thy/Isar.thy
author wenzelm
Thu Oct 14 21:05:21 2010 +0100 (2010-10-14)
changeset 39848 fc88b943e1b2
parent 39847 da8c3fc5e314
child 39849 b7b1a9e8f7c2
permissions -rw-r--r--
misc tuning and clarification;
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@39847
   174
text {* A @{text "method"} is a function @{text "context \<rightarrow> thm\<^sup>* \<rightarrow> goal
wenzelm@39847
   175
  \<rightarrow> (cases \<times> goal)\<^sup>*\<^sup>*"} that operates on the full Isar goal
wenzelm@39847
   176
  configuration with context, goal facts, and tactical goal state and
wenzelm@39843
   177
  enumerates possible follow-up goal states, with the potential
wenzelm@39844
   178
  addition of named extensions of the proof context (\emph{cases}).
wenzelm@39847
   179
  The latter feature is rarely used.
wenzelm@39847
   180
wenzelm@39847
   181
  This means a proof method is like a structurally enhanced tactic
wenzelm@39847
   182
  (cf.\ \secref{sec:tactics}).  The well-formedness conditions for
wenzelm@39847
   183
  tactics need to hold for methods accordingly, with the following
wenzelm@39847
   184
  additions.
wenzelm@39847
   185
wenzelm@39847
   186
  \begin{itemize}
wenzelm@39847
   187
wenzelm@39847
   188
  \item Goal addressing is further limited either to operate either
wenzelm@39847
   189
  uniformly on \emph{all} subgoals, or specifically on the
wenzelm@39847
   190
  \emph{first} subgoal.
wenzelm@39847
   191
wenzelm@39847
   192
  Exception: old-style tactic emulations that are embedded into the
wenzelm@39847
   193
  method space, e.g.\ @{method rule_tac}.
wenzelm@39847
   194
wenzelm@39847
   195
  \item A non-trivial method always needs to make progress: an
wenzelm@39847
   196
  identical follow-up goal state has to be avoided.\footnote{This
wenzelm@39847
   197
  enables the user to write method expressions like @{text "meth\<^sup>+"}
wenzelm@39847
   198
  without looping, while the trivial do-nothing case can be recovered
wenzelm@39847
   199
  via @{text "meth\<^sup>?"}.}
wenzelm@39847
   200
wenzelm@39847
   201
  Exception: trivial stuttering steps, such as ``@{method -}'' or
wenzelm@39847
   202
  @{method succeed}.
wenzelm@39847
   203
wenzelm@39847
   204
  \item Goal facts passed to the method must not be ignored.  If there
wenzelm@39847
   205
  is no sensible use of facts outside the goal state, facts should be
wenzelm@39847
   206
  inserted into the subgoals that are addressed by the method.
wenzelm@39847
   207
wenzelm@39847
   208
  \end{itemize}
wenzelm@39847
   209
wenzelm@39847
   210
  \medskip Syntactically, the language of proof methods is embedded
wenzelm@39847
   211
  into Isar as arguments to certain commands, e.g.\ @{command "by"} or
wenzelm@39847
   212
  @{command apply}.  User-space additions are reasonably easy by
wenzelm@39847
   213
  plugging suitable method-valued parser functions into the framework,
wenzelm@39847
   214
  using the @{command method_setup} command, for example.
wenzelm@39843
   215
wenzelm@39844
   216
  To get a better idea about the range of possibilities, consider the
wenzelm@39844
   217
  following Isar proof schemes.  First some general form of structured
wenzelm@39844
   218
  proof text:
wenzelm@39843
   219
wenzelm@39843
   220
  \medskip
wenzelm@39843
   221
  \begin{tabular}{l}
wenzelm@39843
   222
  @{command from}~@{text "facts\<^sub>1"}~@{command have}~@{text "props"}~@{command using}~@{text "facts\<^sub>2"} \\
wenzelm@39843
   223
  @{command proof}~@{text "(initial_method)"} \\
wenzelm@39843
   224
  \quad@{text "body"} \\
wenzelm@39843
   225
  @{command qed}~@{text "(terminal_method)"} \\
wenzelm@39843
   226
  \end{tabular}
wenzelm@39843
   227
  \medskip
wenzelm@39843
   228
wenzelm@39843
   229
  \noindent The goal configuration consists of @{text "facts\<^sub>1"} and
wenzelm@39843
   230
  @{text "facts\<^sub>2"} appended in that order, and various @{text "props"}
wenzelm@39844
   231
  being claimed here.  The @{text "initial_method"} is invoked with
wenzelm@39844
   232
  facts and goals together and refines the problem to something that
wenzelm@39844
   233
  is handled recursively in the proof @{text "body"}.  The @{text
wenzelm@39843
   234
  "terminal_method"} has another chance to finish-off any remaining
wenzelm@39843
   235
  subgoals, but it does not see the facts of the initial step.
wenzelm@39843
   236
wenzelm@39844
   237
  \medskip The second pattern illustrates unstructured proof scripts:
wenzelm@39843
   238
wenzelm@39844
   239
  \medskip
wenzelm@39843
   240
  \begin{tabular}{l}
wenzelm@39843
   241
  @{command have}~@{text "props"} \\
wenzelm@39844
   242
  \quad@{command using}~@{text "facts\<^sub>1"}~@{command apply}~@{text "method\<^sub>1"} \\
wenzelm@39843
   243
  \quad@{command apply}~@{text "method\<^sub>2"} \\
wenzelm@39844
   244
  \quad@{command using}~@{text "facts\<^sub>3"}~@{command apply}~@{text "method\<^sub>3"} \\
wenzelm@39843
   245
  \quad@{command done} \\
wenzelm@39843
   246
  \end{tabular}
wenzelm@39843
   247
  \medskip
wenzelm@39843
   248
wenzelm@39843
   249
  \noindent The @{text "method\<^sub>1"} operates on the original claim
wenzelm@39847
   250
  together while using @{text "facts\<^sub>1"}.  Since the @{command apply}
wenzelm@39843
   251
  command structurally resets the facts, the @{text "method\<^sub>2"} will
wenzelm@39843
   252
  operate on the remaining goal state without facts.  The @{text
wenzelm@39844
   253
  "method\<^sub>3"} will see again a collection of @{text "facts\<^sub>3"} that has
wenzelm@39844
   254
  been inserted into the script explicitly.
wenzelm@39843
   255
wenzelm@39847
   256
  \medskip Empirically, Isar proof methods can be categorized as
wenzelm@39847
   257
  follows.
wenzelm@39843
   258
wenzelm@39843
   259
  \begin{enumerate}
wenzelm@39843
   260
wenzelm@39847
   261
  \item \emph{Special method with cases} with named context additions
wenzelm@39847
   262
  associated with the follow-up goal state.
wenzelm@39843
   263
wenzelm@39847
   264
  Example: @{method "induct"}, which is also a ``raw'' method since it
wenzelm@39847
   265
  operates on the internal representation of simultaneous claims as
wenzelm@39847
   266
  Pure conjunction, instead of separate subgoals.
wenzelm@39843
   267
wenzelm@39847
   268
  \item \emph{Structured method} with strong emphasis on facts outside
wenzelm@39847
   269
  the goal state.
wenzelm@39847
   270
wenzelm@39847
   271
  Example: @{method "rule"}, which captures the key ideas behind
wenzelm@39847
   272
  structured reasoning in Isar in purest form.
wenzelm@39843
   273
wenzelm@39847
   274
  \item \emph{Simple method} with weaker emphasis on facts, which are
wenzelm@39847
   275
  inserted into subgoals to emulate old-style tactical as
wenzelm@39847
   276
  ``premises''.
wenzelm@39843
   277
wenzelm@39847
   278
  Examples: @{method "simp"}, @{method "blast"}, @{method "auto"}.
wenzelm@39843
   279
wenzelm@39847
   280
  \item \emph{Old-style tactic emulation} with detailed numeric goal
wenzelm@39847
   281
  addressing and explicit references to entities of the internal goal
wenzelm@39847
   282
  state (which are otherwise invisible from proper Isar proof text).
wenzelm@39847
   283
  To make the special non-standard status clear, the naming convention
wenzelm@39847
   284
  @{text "foo_tac"} needs to be observed.
wenzelm@39843
   285
wenzelm@39847
   286
  Example: @{method "rule_tac"}.
wenzelm@39843
   287
wenzelm@39843
   288
  \end{enumerate}
wenzelm@39843
   289
wenzelm@39847
   290
  When implementing proof methods, it is advisable to study existing
wenzelm@39847
   291
  implementations carefully and imitate the typical ``boiler plate''
wenzelm@39847
   292
  for context-sensitive parsing and further combinators to wrap-up
wenzelm@39848
   293
  tactic expressions as methods.\footnote{Aliases or abbreviations of
wenzelm@39848
   294
  the standard method combinators should be avoided.  Note that from
wenzelm@39848
   295
  Isabelle99 until Isabelle2009 the system did provide various odd
wenzelm@39848
   296
  combinations of method wrappers that made user applications more
wenzelm@39848
   297
  complicated than necessary.}
wenzelm@39847
   298
*}
wenzelm@39847
   299
wenzelm@39847
   300
text %mlref {*
wenzelm@39847
   301
  \begin{mldecls}
wenzelm@39847
   302
  @{index_ML_type Proof.method} \\
wenzelm@39847
   303
  @{index_ML METHOD_CASES: "(thm list -> cases_tactic) -> Proof.method"} \\
wenzelm@39847
   304
  @{index_ML METHOD: "(thm list -> tactic) -> Proof.method"} \\
wenzelm@39847
   305
  @{index_ML SIMPLE_METHOD: "tactic -> Proof.method"} \\
wenzelm@39847
   306
  @{index_ML SIMPLE_METHOD': "(int -> tactic) -> Proof.method"} \\
wenzelm@39847
   307
  @{index_ML HEADGOAL: "(int -> tactic) -> tactic"} \\
wenzelm@39847
   308
  @{index_ML Method.insert_tac: "thm list -> int -> tactic"} \\
wenzelm@39847
   309
  @{index_ML Method.setup: "binding -> (Proof.context -> Proof.method) context_parser ->
wenzelm@39847
   310
  string -> theory -> theory"} \\
wenzelm@39847
   311
  \end{mldecls}
wenzelm@39847
   312
wenzelm@39847
   313
  \begin{description}
wenzelm@39847
   314
wenzelm@39847
   315
  \item @{ML_type Proof.method} represents proof methods as abstract type.
wenzelm@39847
   316
wenzelm@39847
   317
  \item @{ML METHOD_CASES}~@{text "(fn facts => cases_tactic)"} wraps
wenzelm@39847
   318
  @{text cases_tactic} depending on goal facts as proof method with
wenzelm@39847
   319
  cases; the goal context is passed via method syntax.
wenzelm@39847
   320
wenzelm@39847
   321
  \item @{ML METHOD}~@{text "(fn facts => tactic)"} wraps @{text
wenzelm@39847
   322
  tactic} depending on goal facts as regular proof method; the goal
wenzelm@39847
   323
  context is passed via method syntax.
wenzelm@39847
   324
wenzelm@39847
   325
  \item @{ML SIMPLE_METHOD}~@{text "tactic"} wraps a tactic that
wenzelm@39847
   326
  addresses all subgoals uniformly as simple proof method.  Goal facts
wenzelm@39847
   327
  are already inserted into all subgoals before @{text "tactic"} is
wenzelm@39847
   328
  applied.
wenzelm@39847
   329
wenzelm@39847
   330
  \item @{ML SIMPLE_METHOD'}~@{text "tactic"} wraps a tactic that
wenzelm@39847
   331
  addresses a specific subgoal as simple proof method.  Goal facts are
wenzelm@39847
   332
  already inserted into the first subgoal before @{text "tactic"} is
wenzelm@39847
   333
  applied to the same.
wenzelm@39847
   334
wenzelm@39847
   335
  \item @{ML HEADGOAL}~@{text "tactic"} applies @{text "tactic"} to
wenzelm@39847
   336
  the first subgoal.  This is convenient to reproduce part the @{ML
wenzelm@39847
   337
  SIMPLE_METHOD'} wrapping within regular @{ML METHOD}, for example.
wenzelm@39847
   338
wenzelm@39847
   339
  \item @{ML Method.insert_tac}~@{text "facts i"} inserts @{text
wenzelm@39847
   340
  "facts"} into subgoal @{text "i"}.  This is convenient to reproduce
wenzelm@39847
   341
  part of the @{ML SIMPLE_METHOD} or @{ML SIMPLE_METHOD'} wrapping
wenzelm@39847
   342
  within regular @{ML METHOD}, for example.
wenzelm@39847
   343
wenzelm@39847
   344
  \item @{ML Method.setup}~@{text "name parser description"} provides
wenzelm@39848
   345
  the functionality of the Isar command @{command method_setup} as ML
wenzelm@39848
   346
  function.
wenzelm@39847
   347
wenzelm@39847
   348
  \end{description}
wenzelm@39847
   349
*}
wenzelm@39847
   350
wenzelm@39848
   351
text %mlex {* The following toy examples illustrate how the goal facts
wenzelm@39848
   352
  and state are passed to proof methods.  The pre-defined proof method
wenzelm@39848
   353
  called ``@{method tactic}'' wraps ML source of type @{ML_type
wenzelm@39847
   354
  tactic} (abstracted over @{verbatim facts}).  This allows immediate
wenzelm@39848
   355
  experimentation without parsing of concrete syntax. *}
wenzelm@20472
   356
wenzelm@39847
   357
example_proof
wenzelm@39847
   358
  assume a: A and b: B
wenzelm@39847
   359
wenzelm@39847
   360
  have "A \<and> B"
wenzelm@39847
   361
    apply (tactic {* rtac @{thm conjI} 1 *})
wenzelm@39847
   362
    using a apply (tactic {* resolve_tac facts 1 *})
wenzelm@39847
   363
    using b apply (tactic {* resolve_tac facts 1 *})
wenzelm@39847
   364
    done
wenzelm@39847
   365
wenzelm@39847
   366
  have "A \<and> B"
wenzelm@39847
   367
    using a and b
wenzelm@39847
   368
    ML_val "@{Isar.goal}"
wenzelm@39847
   369
    apply (tactic {* Method.insert_tac facts 1 *})
wenzelm@39847
   370
    apply (tactic {* (rtac @{thm conjI} THEN_ALL_NEW atac) 1 *})
wenzelm@39847
   371
    done
wenzelm@39847
   372
qed
wenzelm@39847
   373
wenzelm@39848
   374
text {* \medskip The next example implements a method that simplifies
wenzelm@39848
   375
  the first subgoal by rewrite rules given as arguments.  *}
wenzelm@39848
   376
wenzelm@39848
   377
method_setup my_simp = {*
wenzelm@39848
   378
  Attrib.thms >> (fn thms => fn ctxt =>
wenzelm@39848
   379
    SIMPLE_METHOD' (fn i =>
wenzelm@39848
   380
      CHANGED (asm_full_simp_tac
wenzelm@39848
   381
        (HOL_basic_ss addsimps thms) i)))
wenzelm@39848
   382
*} "rewrite subgoal by given rules"
wenzelm@39848
   383
wenzelm@39848
   384
text {* The concrete syntax wrapping of @{command method_setup} always
wenzelm@39848
   385
  passes-through the proof context at the end of parsing, but it is
wenzelm@39848
   386
  not used in this example.
wenzelm@39848
   387
wenzelm@39848
   388
  The @{ML Attrib.thms} parser produces a list of theorems from the
wenzelm@39848
   389
  usual Isar syntax involving attribute expressions etc.\ (syntax
wenzelm@39848
   390
  category @{syntax thmrefs}).  The resulting @{verbatim thms} are
wenzelm@39848
   391
  added to @{ML HOL_basic_ss} which already contains the basic
wenzelm@39848
   392
  Simplifier setup for HOL.
wenzelm@39848
   393
wenzelm@39848
   394
  The tactic @{ML asm_full_simp_tac} is the one that is also used in
wenzelm@39848
   395
  method @{method simp} by default.  The extra wrapping by the @{ML
wenzelm@39848
   396
  CHANGED} tactical ensures progress of simplification: identical goal
wenzelm@39848
   397
  states are filtered out explicitly to make the raw tactic conform to
wenzelm@39848
   398
  standard Isar method behaviour.
wenzelm@39848
   399
wenzelm@39848
   400
  \medskip Method @{method my_simp} can be used in Isar proofs like
wenzelm@39848
   401
  this:
wenzelm@39847
   402
*}
wenzelm@39847
   403
wenzelm@39848
   404
example_proof
wenzelm@39848
   405
  fix a b c
wenzelm@39848
   406
  assume a: "a \<equiv> b"
wenzelm@39848
   407
  assume b: "b \<equiv> c"
wenzelm@39848
   408
  have "a \<equiv> c" by (my_simp a b)
wenzelm@39848
   409
qed
wenzelm@39848
   410
wenzelm@39848
   411
text {* \medskip Apart from explicit arguments, common proof methods
wenzelm@39848
   412
  typically work with a default configuration provided by the context.
wenzelm@39848
   413
  As a shortcut to rule management we use a cheap solution via functor
wenzelm@39848
   414
  @{ML_functor Named_Thms} (see also @{"file"
wenzelm@39848
   415
  "~~/src/Pure/Tools/named_thms.ML"}).  *}
wenzelm@39848
   416
wenzelm@39847
   417
ML {*
wenzelm@39847
   418
  structure My_Simps =
wenzelm@39847
   419
    Named_Thms
wenzelm@39847
   420
      (val name = "my_simp" val description = "my_simp rule")
wenzelm@39847
   421
*}
wenzelm@39847
   422
setup My_Simps.setup
wenzelm@39847
   423
wenzelm@39847
   424
text {* \medskip\noindent This provides ML access to a list of
wenzelm@39847
   425
  theorems in canonical declaration order via @{ML My_Simps.get}.  The
wenzelm@39847
   426
  user can add or delete rules via the attribute @{attribute my_simp}.
wenzelm@39848
   427
  The actual proof method is now defined as before, but we append the
wenzelm@39848
   428
  explicit arguments and the rules from the context.
wenzelm@39847
   429
*}
wenzelm@39847
   430
wenzelm@39848
   431
method_setup my_simp' = {*
wenzelm@39848
   432
  Attrib.thms >> (fn thms => fn ctxt =>
wenzelm@39847
   433
    SIMPLE_METHOD' (fn i =>
wenzelm@39847
   434
      CHANGED (asm_full_simp_tac
wenzelm@39848
   435
        (HOL_basic_ss addsimps (thms @ My_Simps.get ctxt)) i)))
wenzelm@39848
   436
*} "rewrite subgoal by given rules and my_simp rules from the context"
wenzelm@39847
   437
wenzelm@39848
   438
text {*
wenzelm@39848
   439
  \medskip Method @{method my_simp'} can be used in Isar proofs
wenzelm@39848
   440
  like this:
wenzelm@39848
   441
*}
wenzelm@39847
   442
wenzelm@39847
   443
example_proof
wenzelm@39847
   444
  fix a b c
wenzelm@39847
   445
  assume [my_simp]: "a \<equiv> b"
wenzelm@39847
   446
  assume [my_simp]: "b \<equiv> c"
wenzelm@39848
   447
  have "a \<equiv> c" by my_simp'
wenzelm@39847
   448
qed
wenzelm@39847
   449
wenzelm@39847
   450
text {* \medskip Both @{method my_simp} and @{method my_simp'} are
wenzelm@39847
   451
  simple methods, i.e.\ the goal facts are merely inserted as goal
wenzelm@39848
   452
  premises by the @{ML SIMPLE_METHOD'} wrapper.  For proof methods
wenzelm@39848
   453
  that are similar to the standard collection of @{method simp},
wenzelm@39848
   454
  @{method blast}, @{method auto} little more can be done here.
wenzelm@39847
   455
wenzelm@39847
   456
  Note that using the primary goal facts in the same manner as the
wenzelm@39848
   457
  method arguments obtained via concrete syntax or the context does
wenzelm@39848
   458
  not meet the requirement of ``strong emphasis on facts'' of regular
wenzelm@39848
   459
  proof methods, because rewrite rules as used above can be easily
wenzelm@39848
   460
  ignored.  A proof text ``@{command using}~@{text "foo"}~@{command
wenzelm@39848
   461
  "by"}~@{text "my_simp"}'' where @{text "foo"} is not used would
wenzelm@39848
   462
  deceive the reader.
wenzelm@39847
   463
wenzelm@39847
   464
  \medskip The technical treatment of rules from the context requires
wenzelm@39847
   465
  further attention.  Above we rebuild a fresh @{ML_type simpset} from
wenzelm@39848
   466
  the arguments and \emph{all} rules retrieved from the context on
wenzelm@39848
   467
  every invocation of the method.  This does not scale to really large
wenzelm@39848
   468
  collections of rules, which easily emerges in the context of a big
wenzelm@39848
   469
  theory library, for example.
wenzelm@39847
   470
wenzelm@39848
   471
  This is an inherent limitation of the simplistic rule management via
wenzelm@39848
   472
  functor @{ML_functor Named_Thms}, because it lacks tool-specific
wenzelm@39847
   473
  storage and retrieval.  More realistic applications require
wenzelm@39847
   474
  efficient index-structures that organize theorems in a customized
wenzelm@39847
   475
  manner, such as a discrimination net that is indexed by the
wenzelm@39848
   476
  left-hand sides of rewrite rules.  For variations on the Simplifier,
wenzelm@39848
   477
  re-use of the existing type @{ML_type simpset} is adequate, but
wenzelm@39847
   478
  scalability requires it be maintained statically within the context
wenzelm@39847
   479
  data, not dynamically on each tool invocation.  *}
wenzelm@39847
   480
wenzelm@29759
   481
wenzelm@20472
   482
section {* Attributes *}
wenzelm@20472
   483
wenzelm@29759
   484
text FIXME
wenzelm@30272
   485
wenzelm@20472
   486
end