src/Doc/Isar_Ref/Generic.thy
author wenzelm
Wed Apr 13 18:01:05 2016 +0200 (2016-04-13)
changeset 62969 9f394a16c557
parent 62913 13252110a6fe
child 63531 847eefdca90d
permissions -rw-r--r--
eliminated "xname" and variants;
wenzelm@61656
     1
(*:maxLineLen=78:*)
wenzelm@61656
     2
wenzelm@26782
     3
theory Generic
wenzelm@42651
     4
imports Base Main
wenzelm@26782
     5
begin
wenzelm@26782
     6
wenzelm@58618
     7
chapter \<open>Generic tools and packages \label{ch:gen-tools}\<close>
wenzelm@26782
     8
wenzelm@58618
     9
section \<open>Configuration options \label{sec:config}\<close>
wenzelm@26782
    10
wenzelm@58618
    11
text \<open>Isabelle/Pure maintains a record of named configuration
wenzelm@40291
    12
  options within the theory or proof context, with values of type
wenzelm@40291
    13
  @{ML_type bool}, @{ML_type int}, @{ML_type real}, or @{ML_type
wenzelm@40291
    14
  string}.  Tools may declare options in ML, and then refer to these
wenzelm@40291
    15
  values (relative to the context).  Thus global reference variables
wenzelm@40291
    16
  are easily avoided.  The user may change the value of a
wenzelm@40291
    17
  configuration option by means of an associated attribute of the same
wenzelm@40291
    18
  name.  This form of context declaration works particularly well with
wenzelm@42655
    19
  commands such as @{command "declare"} or @{command "using"} like
wenzelm@42655
    20
  this:
wenzelm@58618
    21
\<close>
wenzelm@42655
    22
wenzelm@59905
    23
(*<*)experiment begin(*>*)
wenzelm@42655
    24
declare [[show_main_goal = false]]
wenzelm@26782
    25
wenzelm@42655
    26
notepad
wenzelm@42655
    27
begin
wenzelm@42655
    28
  note [[show_main_goal = true]]
wenzelm@42655
    29
end
wenzelm@59905
    30
(*<*)end(*>*)
wenzelm@42655
    31
wenzelm@59921
    32
text \<open>
wenzelm@26782
    33
  \begin{matharray}{rcll}
wenzelm@61493
    34
    @{command_def "print_options"} & : & \<open>context \<rightarrow>\<close> \\
wenzelm@26782
    35
  \end{matharray}
wenzelm@26782
    36
wenzelm@55112
    37
  @{rail \<open>
wenzelm@59917
    38
    @@{command print_options} ('!'?)
wenzelm@59917
    39
    ;
wenzelm@42596
    40
    @{syntax name} ('=' ('true' | 'false' | @{syntax int} | @{syntax float} | @{syntax name}))?
wenzelm@55112
    41
  \<close>}
wenzelm@26782
    42
wenzelm@61439
    43
  \<^descr> @{command "print_options"} prints the available configuration
wenzelm@61493
    44
  options, with names, types, and current values; the ``\<open>!\<close>'' option
wenzelm@59917
    45
  indicates extra verbosity.
wenzelm@26782
    46
  
wenzelm@61493
    47
  \<^descr> \<open>name = value\<close> as an attribute expression modifies the
wenzelm@28760
    48
  named option, with the syntax of the value depending on the option's
wenzelm@61493
    49
  type.  For @{ML_type bool} the default value is \<open>true\<close>.  Any
wenzelm@28760
    50
  attempt to change a global option in a local context is ignored.
wenzelm@58618
    51
\<close>
wenzelm@26782
    52
wenzelm@26782
    53
wenzelm@58618
    54
section \<open>Basic proof tools\<close>
wenzelm@26782
    55
wenzelm@58618
    56
subsection \<open>Miscellaneous methods and attributes \label{sec:misc-meth-att}\<close>
wenzelm@26782
    57
wenzelm@58618
    58
text \<open>
wenzelm@26782
    59
  \begin{matharray}{rcl}
wenzelm@61493
    60
    @{method_def unfold} & : & \<open>method\<close> \\
wenzelm@61493
    61
    @{method_def fold} & : & \<open>method\<close> \\
wenzelm@61493
    62
    @{method_def insert} & : & \<open>method\<close> \\[0.5ex]
wenzelm@61493
    63
    @{method_def erule}\<open>\<^sup>*\<close> & : & \<open>method\<close> \\
wenzelm@61493
    64
    @{method_def drule}\<open>\<^sup>*\<close> & : & \<open>method\<close> \\
wenzelm@61493
    65
    @{method_def frule}\<open>\<^sup>*\<close> & : & \<open>method\<close> \\
wenzelm@61493
    66
    @{method_def intro} & : & \<open>method\<close> \\
wenzelm@61493
    67
    @{method_def elim} & : & \<open>method\<close> \\
wenzelm@61493
    68
    @{method_def fail} & : & \<open>method\<close> \\
wenzelm@61493
    69
    @{method_def succeed} & : & \<open>method\<close> \\
wenzelm@61493
    70
    @{method_def sleep} & : & \<open>method\<close> \\
wenzelm@26782
    71
  \end{matharray}
wenzelm@26782
    72
wenzelm@55112
    73
  @{rail \<open>
wenzelm@62969
    74
    (@@{method fold} | @@{method unfold} | @@{method insert}) @{syntax thms}
wenzelm@26782
    75
    ;
wenzelm@42596
    76
    (@@{method erule} | @@{method drule} | @@{method frule})
wenzelm@62969
    77
      ('(' @{syntax nat} ')')? @{syntax thms}
wenzelm@43365
    78
    ;
wenzelm@62969
    79
    (@@{method intro} | @@{method elim}) @{syntax thms}?
wenzelm@60556
    80
    ;
wenzelm@60556
    81
    @@{method sleep} @{syntax real}
wenzelm@55112
    82
  \<close>}
wenzelm@26782
    83
wenzelm@61493
    84
  \<^descr> @{method unfold}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> and @{method fold}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> expand (or fold back) the given definitions throughout
wenzelm@28760
    85
  all goals; any chained facts provided are inserted into the goal and
wenzelm@28760
    86
  subject to rewriting as well.
wenzelm@26782
    87
wenzelm@61493
    88
  \<^descr> @{method insert}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> inserts theorems as facts
wenzelm@28760
    89
  into all goals of the proof state.  Note that current facts
wenzelm@28760
    90
  indicated for forward chaining are ignored.
wenzelm@26782
    91
wenzelm@61493
    92
  \<^descr> @{method erule}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close>, @{method
wenzelm@61493
    93
  drule}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close>, and @{method frule}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> are similar to the basic @{method rule}
wenzelm@30397
    94
  method (see \secref{sec:pure-meth-att}), but apply rules by
wenzelm@30397
    95
  elim-resolution, destruct-resolution, and forward-resolution,
wenzelm@58552
    96
  respectively @{cite "isabelle-implementation"}.  The optional natural
wenzelm@30397
    97
  number argument (default 0) specifies additional assumption steps to
wenzelm@30397
    98
  be performed here.
wenzelm@26782
    99
wenzelm@26782
   100
  Note that these methods are improper ones, mainly serving for
wenzelm@26782
   101
  experimentation and tactic script emulation.  Different modes of
wenzelm@26782
   102
  basic rule application are usually expressed in Isar at the proof
wenzelm@26782
   103
  language level, rather than via implicit proof state manipulations.
wenzelm@26782
   104
  For example, a proper single-step elimination would be done using
wenzelm@26782
   105
  the plain @{method rule} method, with forward chaining of current
wenzelm@26782
   106
  facts.
wenzelm@26782
   107
wenzelm@61439
   108
  \<^descr> @{method intro} and @{method elim} repeatedly refine some goal
wenzelm@43365
   109
  by intro- or elim-resolution, after having inserted any chained
wenzelm@43365
   110
  facts.  Exactly the rules given as arguments are taken into account;
wenzelm@43365
   111
  this allows fine-tuned decomposition of a proof problem, in contrast
wenzelm@43365
   112
  to common automated tools.
wenzelm@43365
   113
wenzelm@61439
   114
  \<^descr> @{method fail} yields an empty result sequence; it is the
wenzelm@61493
   115
  identity of the ``\<open>|\<close>'' method combinator (cf.\
wenzelm@60554
   116
  \secref{sec:proof-meth}).
wenzelm@60554
   117
wenzelm@61439
   118
  \<^descr> @{method succeed} yields a single (unchanged) result; it is
wenzelm@61493
   119
  the identity of the ``\<open>,\<close>'' method combinator (cf.\
wenzelm@28754
   120
  \secref{sec:proof-meth}).
wenzelm@26782
   121
wenzelm@61493
   122
  \<^descr> @{method sleep}~\<open>s\<close> succeeds after a real-time delay of \<open>s\<close> seconds. This is occasionally useful for demonstration and testing
wenzelm@60554
   123
  purposes.
wenzelm@26782
   124
wenzelm@26782
   125
wenzelm@26782
   126
  \begin{matharray}{rcl}
wenzelm@61493
   127
    @{attribute_def tagged} & : & \<open>attribute\<close> \\
wenzelm@61493
   128
    @{attribute_def untagged} & : & \<open>attribute\<close> \\[0.5ex]
wenzelm@61493
   129
    @{attribute_def THEN} & : & \<open>attribute\<close> \\
wenzelm@61493
   130
    @{attribute_def unfolded} & : & \<open>attribute\<close> \\
wenzelm@61493
   131
    @{attribute_def folded} & : & \<open>attribute\<close> \\
wenzelm@61493
   132
    @{attribute_def abs_def} & : & \<open>attribute\<close> \\[0.5ex]
wenzelm@61493
   133
    @{attribute_def rotated} & : & \<open>attribute\<close> \\
wenzelm@61493
   134
    @{attribute_def (Pure) elim_format} & : & \<open>attribute\<close> \\
wenzelm@61493
   135
    @{attribute_def no_vars}\<open>\<^sup>*\<close> & : & \<open>attribute\<close> \\
wenzelm@26782
   136
  \end{matharray}
wenzelm@26782
   137
wenzelm@55112
   138
  @{rail \<open>
wenzelm@42596
   139
    @@{attribute tagged} @{syntax name} @{syntax name}
wenzelm@26782
   140
    ;
wenzelm@42596
   141
    @@{attribute untagged} @{syntax name}
wenzelm@26782
   142
    ;
wenzelm@62969
   143
    @@{attribute THEN} ('[' @{syntax nat} ']')? @{syntax thm}
wenzelm@26782
   144
    ;
wenzelm@62969
   145
    (@@{attribute unfolded} | @@{attribute folded}) @{syntax thms}
wenzelm@26782
   146
    ;
wenzelm@42596
   147
    @@{attribute rotated} @{syntax int}?
wenzelm@55112
   148
  \<close>}
wenzelm@26782
   149
wenzelm@61493
   150
  \<^descr> @{attribute tagged}~\<open>name value\<close> and @{attribute
wenzelm@61493
   151
  untagged}~\<open>name\<close> add and remove \<^emph>\<open>tags\<close> of some theorem.
wenzelm@26782
   152
  Tags may be any list of string pairs that serve as formal comment.
wenzelm@28764
   153
  The first string is considered the tag name, the second its value.
wenzelm@28764
   154
  Note that @{attribute untagged} removes any tags of the same name.
wenzelm@26782
   155
wenzelm@61493
   156
  \<^descr> @{attribute THEN}~\<open>a\<close> composes rules by resolution; it
wenzelm@61493
   157
  resolves with the first premise of \<open>a\<close> (an alternative
wenzelm@48205
   158
  position may be also specified).  See also @{ML_op "RS"} in
wenzelm@58552
   159
  @{cite "isabelle-implementation"}.
wenzelm@26782
   160
  
wenzelm@61493
   161
  \<^descr> @{attribute unfolded}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> and @{attribute
wenzelm@61493
   162
  folded}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> expand and fold back again the given
wenzelm@28760
   163
  definitions throughout a rule.
wenzelm@26782
   164
wenzelm@61439
   165
  \<^descr> @{attribute abs_def} turns an equation of the form @{prop "f x
wenzelm@47497
   166
  y \<equiv> t"} into @{prop "f \<equiv> \<lambda>x y. t"}, which ensures that @{method
wenzelm@47497
   167
  simp} or @{method unfold} steps always expand it.  This also works
wenzelm@47497
   168
  for object-logic equality.
wenzelm@47497
   169
wenzelm@61493
   170
  \<^descr> @{attribute rotated}~\<open>n\<close> rotate the premises of a
wenzelm@61493
   171
  theorem by \<open>n\<close> (default 1).
wenzelm@26782
   172
wenzelm@61439
   173
  \<^descr> @{attribute (Pure) elim_format} turns a destruction rule into
wenzelm@28760
   174
  elimination rule format, by resolving with the rule @{prop "PROP A \<Longrightarrow>
wenzelm@28760
   175
  (PROP A \<Longrightarrow> PROP B) \<Longrightarrow> PROP B"}.
wenzelm@26782
   176
  
wenzelm@26782
   177
  Note that the Classical Reasoner (\secref{sec:classical}) provides
wenzelm@26782
   178
  its own version of this operation.
wenzelm@26782
   179
wenzelm@61439
   180
  \<^descr> @{attribute no_vars} replaces schematic variables by free
wenzelm@26782
   181
  ones; this is mainly for tuning output of pretty printed theorems.
wenzelm@58618
   182
\<close>
wenzelm@26782
   183
wenzelm@26782
   184
wenzelm@58618
   185
subsection \<open>Low-level equational reasoning\<close>
wenzelm@27044
   186
wenzelm@58618
   187
text \<open>
wenzelm@27044
   188
  \begin{matharray}{rcl}
wenzelm@61493
   189
    @{method_def subst} & : & \<open>method\<close> \\
wenzelm@61493
   190
    @{method_def hypsubst} & : & \<open>method\<close> \\
wenzelm@61493
   191
    @{method_def split} & : & \<open>method\<close> \\
wenzelm@27044
   192
  \end{matharray}
wenzelm@27044
   193
wenzelm@55112
   194
  @{rail \<open>
wenzelm@62969
   195
    @@{method subst} ('(' 'asm' ')')? \<newline> ('(' (@{syntax nat}+) ')')? @{syntax thm}
wenzelm@27044
   196
    ;
wenzelm@62969
   197
    @@{method split} @{syntax thms}
wenzelm@55112
   198
  \<close>}
wenzelm@27044
   199
wenzelm@27044
   200
  These methods provide low-level facilities for equational reasoning
wenzelm@27044
   201
  that are intended for specialized applications only.  Normally,
wenzelm@27044
   202
  single step calculations would be performed in a structured text
wenzelm@27044
   203
  (see also \secref{sec:calculation}), while the Simplifier methods
wenzelm@27044
   204
  provide the canonical way for automated normalization (see
wenzelm@27044
   205
  \secref{sec:simplifier}).
wenzelm@27044
   206
wenzelm@61493
   207
  \<^descr> @{method subst}~\<open>eq\<close> performs a single substitution step
wenzelm@61493
   208
  using rule \<open>eq\<close>, which may be either a meta or object
wenzelm@27044
   209
  equality.
wenzelm@27044
   210
wenzelm@61493
   211
  \<^descr> @{method subst}~\<open>(asm) eq\<close> substitutes in an
wenzelm@27044
   212
  assumption.
wenzelm@27044
   213
wenzelm@61493
   214
  \<^descr> @{method subst}~\<open>(i \<dots> j) eq\<close> performs several
wenzelm@61493
   215
  substitutions in the conclusion. The numbers \<open>i\<close> to \<open>j\<close>
wenzelm@27044
   216
  indicate the positions to substitute at.  Positions are ordered from
wenzelm@27044
   217
  the top of the term tree moving down from left to right. For
wenzelm@61493
   218
  example, in \<open>(a + b) + (c + d)\<close> there are three positions
wenzelm@61493
   219
  where commutativity of \<open>+\<close> is applicable: 1 refers to \<open>a + b\<close>, 2 to the whole term, and 3 to \<open>c + d\<close>.
wenzelm@27044
   220
wenzelm@61493
   221
  If the positions in the list \<open>(i \<dots> j)\<close> are non-overlapping
wenzelm@61493
   222
  (e.g.\ \<open>(2 3)\<close> in \<open>(a + b) + (c + d)\<close>) you may
wenzelm@27044
   223
  assume all substitutions are performed simultaneously.  Otherwise
wenzelm@61493
   224
  the behaviour of \<open>subst\<close> is not specified.
wenzelm@27044
   225
wenzelm@61493
   226
  \<^descr> @{method subst}~\<open>(asm) (i \<dots> j) eq\<close> performs the
wenzelm@27071
   227
  substitutions in the assumptions. The positions refer to the
wenzelm@27071
   228
  assumptions in order from left to right.  For example, given in a
wenzelm@61493
   229
  goal of the form \<open>P (a + b) \<Longrightarrow> P (c + d) \<Longrightarrow> \<dots>\<close>, position 1 of
wenzelm@61493
   230
  commutativity of \<open>+\<close> is the subterm \<open>a + b\<close> and
wenzelm@61493
   231
  position 2 is the subterm \<open>c + d\<close>.
wenzelm@27044
   232
wenzelm@61439
   233
  \<^descr> @{method hypsubst} performs substitution using some
wenzelm@61493
   234
  assumption; this only works for equations of the form \<open>x =
wenzelm@61493
   235
  t\<close> where \<open>x\<close> is a free or bound variable.
wenzelm@27044
   236
wenzelm@61493
   237
  \<^descr> @{method split}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> performs single-step case
wenzelm@44094
   238
  splitting using the given rules.  Splitting is performed in the
wenzelm@44094
   239
  conclusion or some assumption of the subgoal, depending of the
wenzelm@44094
   240
  structure of the rule.
wenzelm@27044
   241
  
wenzelm@27044
   242
  Note that the @{method simp} method already involves repeated
wenzelm@44094
   243
  application of split rules as declared in the current context, using
wenzelm@44094
   244
  @{attribute split}, for example.
wenzelm@58618
   245
\<close>
wenzelm@27044
   246
wenzelm@27044
   247
wenzelm@58618
   248
section \<open>The Simplifier \label{sec:simplifier}\<close>
wenzelm@26782
   249
wenzelm@62655
   250
text \<open>
wenzelm@62655
   251
  The Simplifier performs conditional and unconditional rewriting and uses
wenzelm@62655
   252
  contextual information: rule declarations in the background theory or local
wenzelm@62655
   253
  proof context are taken into account, as well as chained facts and subgoal
wenzelm@62655
   254
  premises (``local assumptions''). There are several general hooks that allow
wenzelm@62655
   255
  to modify the simplification strategy, or incorporate other proof tools that
wenzelm@62655
   256
  solve sub-problems, produce rewrite rules on demand etc.
wenzelm@50063
   257
wenzelm@62655
   258
  The rewriting strategy is always strictly bottom up, except for congruence
wenzelm@62655
   259
  rules, which are applied while descending into a term. Conditions in
wenzelm@62655
   260
  conditional rewrite rules are solved recursively before the rewrite rule is
wenzelm@62655
   261
  applied.
wenzelm@50075
   262
wenzelm@62655
   263
  The default Simplifier setup of major object logics (HOL, HOLCF, FOL, ZF)
wenzelm@62655
   264
  makes the Simplifier ready for immediate use, without engaging into the
wenzelm@62655
   265
  internal structures. Thus it serves as general-purpose proof tool with the
wenzelm@62655
   266
  main focus on equational reasoning, and a bit more than that.
wenzelm@58618
   267
\<close>
wenzelm@50063
   268
wenzelm@50063
   269
wenzelm@58618
   270
subsection \<open>Simplification methods \label{sec:simp-meth}\<close>
wenzelm@26782
   271
wenzelm@58618
   272
text \<open>
wenzelm@57591
   273
  \begin{tabular}{rcll}
wenzelm@61493
   274
    @{method_def simp} & : & \<open>method\<close> \\
wenzelm@61493
   275
    @{method_def simp_all} & : & \<open>method\<close> \\
wenzelm@61493
   276
    @{attribute_def simp_depth_limit} & : & \<open>attribute\<close> & default \<open>100\<close> \\
wenzelm@57591
   277
  \end{tabular}
wenzelm@61421
   278
  \<^medskip>
wenzelm@26782
   279
wenzelm@55112
   280
  @{rail \<open>
wenzelm@42596
   281
    (@@{method simp} | @@{method simp_all}) opt? (@{syntax simpmod} * )
wenzelm@26782
   282
    ;
wenzelm@26782
   283
wenzelm@40255
   284
    opt: '(' ('no_asm' | 'no_asm_simp' | 'no_asm_use' | 'asm_lr' ) ')'
wenzelm@26782
   285
    ;
wenzelm@50063
   286
    @{syntax_def simpmod}: ('add' | 'del' | 'only' | 'split' (() | 'add' | 'del') |
wenzelm@62969
   287
      'cong' (() | 'add' | 'del')) ':' @{syntax thms}
wenzelm@55112
   288
  \<close>}
wenzelm@26782
   289
wenzelm@62655
   290
  \<^descr> @{method simp} invokes the Simplifier on the first subgoal, after
wenzelm@62655
   291
  inserting chained facts as additional goal premises; further rule
wenzelm@62655
   292
  declarations may be included via \<open>(simp add: facts)\<close>. The proof method fails
wenzelm@62655
   293
  if the subgoal remains unchanged after simplification.
wenzelm@26782
   294
wenzelm@62655
   295
  Note that the original goal premises and chained facts are subject to
wenzelm@62655
   296
  simplification themselves, while declarations via \<open>add\<close>/\<open>del\<close> merely follow
wenzelm@62655
   297
  the policies of the object-logic to extract rewrite rules from theorems,
wenzelm@62655
   298
  without further simplification. This may lead to slightly different behavior
wenzelm@62655
   299
  in either case, which might be required precisely like that in some boundary
wenzelm@62655
   300
  situations to perform the intended simplification step!
wenzelm@50063
   301
wenzelm@61421
   302
  \<^medskip>
wenzelm@62655
   303
  The \<open>only\<close> modifier first removes all other rewrite rules, looper tactics
wenzelm@62655
   304
  (including split rules), congruence rules, and then behaves like \<open>add\<close>.
wenzelm@62655
   305
  Implicit solvers remain, which means that trivial rules like reflexivity or
wenzelm@62655
   306
  introduction of \<open>True\<close> are available to solve the simplified subgoals, but
wenzelm@62655
   307
  also non-trivial tools like linear arithmetic in HOL. The latter may lead to
wenzelm@62655
   308
  some surprise of the meaning of ``only'' in Isabelle/HOL compared to
wenzelm@62655
   309
  English!
wenzelm@26782
   310
wenzelm@61421
   311
  \<^medskip>
wenzelm@62655
   312
  The \<open>split\<close> modifiers add or delete rules for the Splitter (see also
wenzelm@62655
   313
  \secref{sec:simp-strategies} on the looper). This works only if the
wenzelm@62655
   314
  Simplifier method has been properly setup to include the Splitter (all major
wenzelm@62655
   315
  object logics such HOL, HOLCF, FOL, ZF do this already).
wenzelm@26782
   316
wenzelm@50065
   317
  There is also a separate @{method_ref split} method available for
wenzelm@62655
   318
  single-step case splitting. The effect of repeatedly applying \<open>(split thms)\<close>
wenzelm@62655
   319
  can be imitated by ``\<open>(simp only: split: thms)\<close>''.
wenzelm@50065
   320
wenzelm@61421
   321
  \<^medskip>
wenzelm@62655
   322
  The \<open>cong\<close> modifiers add or delete Simplifier congruence rules (see also
wenzelm@62655
   323
  \secref{sec:simp-rules}); the default is to add.
wenzelm@50063
   324
wenzelm@62655
   325
  \<^descr> @{method simp_all} is similar to @{method simp}, but acts on all goals,
wenzelm@62655
   326
  working backwards from the last to the first one as usual in Isabelle.\<^footnote>\<open>The
wenzelm@62655
   327
  order is irrelevant for goals without schematic variables, so simplification
wenzelm@62655
   328
  might actually be performed in parallel here.\<close>
wenzelm@50063
   329
wenzelm@62655
   330
  Chained facts are inserted into all subgoals, before the simplification
wenzelm@62655
   331
  process starts. Further rule declarations are the same as for @{method
wenzelm@62655
   332
  simp}.
wenzelm@50063
   333
wenzelm@50063
   334
  The proof method fails if all subgoals remain unchanged after
wenzelm@50063
   335
  simplification.
wenzelm@26782
   336
wenzelm@62655
   337
  \<^descr> @{attribute simp_depth_limit} limits the number of recursive invocations
wenzelm@62655
   338
  of the Simplifier during conditional rewriting.
wenzelm@57591
   339
wenzelm@26782
   340
wenzelm@62655
   341
  By default the Simplifier methods above take local assumptions fully into
wenzelm@62655
   342
  account, using equational assumptions in the subsequent normalization
wenzelm@62655
   343
  process, or simplifying assumptions themselves. Further options allow to
wenzelm@62655
   344
  fine-tune the behavior of the Simplifier in this respect, corresponding to a
wenzelm@62655
   345
  variety of ML tactics as follows.\<^footnote>\<open>Unlike the corresponding Isar proof
wenzelm@62655
   346
  methods, the ML tactics do not insist in changing the goal state.\<close>
wenzelm@50063
   347
wenzelm@50063
   348
  \begin{center}
wenzelm@50063
   349
  \small
wenzelm@59782
   350
  \begin{tabular}{|l|l|p{0.3\textwidth}|}
wenzelm@50063
   351
  \hline
wenzelm@50063
   352
  Isar method & ML tactic & behavior \\\hline
wenzelm@50063
   353
wenzelm@62655
   354
  \<open>(simp (no_asm))\<close> & @{ML simp_tac} & assumptions are ignored completely
wenzelm@62655
   355
  \\\hline
wenzelm@26782
   356
wenzelm@62655
   357
  \<open>(simp (no_asm_simp))\<close> & @{ML asm_simp_tac} & assumptions are used in the
wenzelm@62655
   358
  simplification of the conclusion but are not themselves simplified \\\hline
wenzelm@50063
   359
wenzelm@62655
   360
  \<open>(simp (no_asm_use))\<close> & @{ML full_simp_tac} & assumptions are simplified but
wenzelm@62655
   361
  are not used in the simplification of each other or the conclusion \\\hline
wenzelm@26782
   362
wenzelm@62655
   363
  \<open>(simp)\<close> & @{ML asm_full_simp_tac} & assumptions are used in the
wenzelm@62655
   364
  simplification of the conclusion and to simplify other assumptions \\\hline
wenzelm@50063
   365
wenzelm@62655
   366
  \<open>(simp (asm_lr))\<close> & @{ML asm_lr_simp_tac} & compatibility mode: an
wenzelm@62655
   367
  assumption is only used for simplifying assumptions which are to the right
wenzelm@62655
   368
  of it \\\hline
wenzelm@50063
   369
wenzelm@59782
   370
  \end{tabular}
wenzelm@50063
   371
  \end{center}
wenzelm@58618
   372
\<close>
wenzelm@26782
   373
wenzelm@26782
   374
wenzelm@58618
   375
subsubsection \<open>Examples\<close>
wenzelm@50064
   376
wenzelm@62655
   377
text \<open>
wenzelm@62655
   378
  We consider basic algebraic simplifications in Isabelle/HOL. The rather
wenzelm@62655
   379
  trivial goal @{prop "0 + (x + 0) = x + 0 + 0"} looks like a good candidate
wenzelm@62655
   380
  to be solved by a single call of @{method simp}:
wenzelm@58618
   381
\<close>
wenzelm@50064
   382
wenzelm@50064
   383
lemma "0 + (x + 0) = x + 0 + 0" apply simp? oops
wenzelm@50064
   384
wenzelm@62655
   385
text \<open>
wenzelm@62655
   386
  The above attempt \<^emph>\<open>fails\<close>, because @{term "0"} and @{term "op +"} in the
wenzelm@62655
   387
  HOL library are declared as generic type class operations, without stating
wenzelm@62655
   388
  any algebraic laws yet. More specific types are required to get access to
wenzelm@62655
   389
  certain standard simplifications of the theory context, e.g.\ like this:\<close>
wenzelm@50064
   390
wenzelm@50064
   391
lemma fixes x :: nat shows "0 + (x + 0) = x + 0 + 0" by simp
wenzelm@50064
   392
lemma fixes x :: int shows "0 + (x + 0) = x + 0 + 0" by simp
wenzelm@50064
   393
lemma fixes x :: "'a :: monoid_add" shows "0 + (x + 0) = x + 0 + 0" by simp
wenzelm@50064
   394
wenzelm@58618
   395
text \<open>
wenzelm@61421
   396
  \<^medskip>
wenzelm@62655
   397
  In many cases, assumptions of a subgoal are also needed in the
wenzelm@62655
   398
  simplification process. For example:
wenzelm@58618
   399
\<close>
wenzelm@50064
   400
wenzelm@50064
   401
lemma fixes x :: nat shows "x = 0 \<Longrightarrow> x + x = 0" by simp
wenzelm@50064
   402
lemma fixes x :: nat assumes "x = 0" shows "x + x = 0" apply simp oops
wenzelm@50064
   403
lemma fixes x :: nat assumes "x = 0" shows "x + x = 0" using assms by simp
wenzelm@50064
   404
wenzelm@62655
   405
text \<open>
wenzelm@62655
   406
  As seen above, local assumptions that shall contribute to simplification
wenzelm@62655
   407
  need to be part of the subgoal already, or indicated explicitly for use by
wenzelm@62655
   408
  the subsequent method invocation. Both too little or too much information
wenzelm@62655
   409
  can make simplification fail, for different reasons.
wenzelm@50064
   410
wenzelm@62655
   411
  In the next example the malicious assumption @{prop "\<And>x::nat. f x = g (f (g
wenzelm@62655
   412
  x))"} does not contribute to solve the problem, but makes the default
wenzelm@62655
   413
  @{method simp} method loop: the rewrite rule \<open>f ?x \<equiv> g (f (g ?x))\<close> extracted
wenzelm@62655
   414
  from the assumption does not terminate. The Simplifier notices certain
wenzelm@62655
   415
  simple forms of nontermination, but not this one. The problem can be solved
wenzelm@62655
   416
  nonetheless, by ignoring assumptions via special options as explained
wenzelm@62655
   417
  before:
wenzelm@58618
   418
\<close>
wenzelm@50064
   419
wenzelm@50064
   420
lemma "(\<And>x::nat. f x = g (f (g x))) \<Longrightarrow> f 0 = f 0 + 0"
wenzelm@50064
   421
  by (simp (no_asm))
wenzelm@50064
   422
wenzelm@62655
   423
text \<open>
wenzelm@62655
   424
  The latter form is typical for long unstructured proof scripts, where the
wenzelm@62655
   425
  control over the goal content is limited. In structured proofs it is usually
wenzelm@62655
   426
  better to avoid pushing too many facts into the goal state in the first
wenzelm@62655
   427
  place. Assumptions in the Isar proof context do not intrude the reasoning if
wenzelm@62655
   428
  not used explicitly. This is illustrated for a toplevel statement and a
wenzelm@50064
   429
  local proof body as follows:
wenzelm@58618
   430
\<close>
wenzelm@50064
   431
wenzelm@50064
   432
lemma
wenzelm@50064
   433
  assumes "\<And>x::nat. f x = g (f (g x))"
wenzelm@50064
   434
  shows "f 0 = f 0 + 0" by simp
wenzelm@50064
   435
wenzelm@50064
   436
notepad
wenzelm@50064
   437
begin
wenzelm@50064
   438
  assume "\<And>x::nat. f x = g (f (g x))"
wenzelm@50064
   439
  have "f 0 = f 0 + 0" by simp
wenzelm@50064
   440
end
wenzelm@50064
   441
wenzelm@61421
   442
text \<open>
wenzelm@61421
   443
  \<^medskip>
wenzelm@62655
   444
  Because assumptions may simplify each other, there can be very subtle cases
wenzelm@62655
   445
  of nontermination. For example, the regular @{method simp} method applied to
wenzelm@62655
   446
  @{prop "P (f x) \<Longrightarrow> y = x \<Longrightarrow> f x = f y \<Longrightarrow> Q"} gives rise to the infinite
wenzelm@62655
   447
  reduction sequence
wenzelm@50064
   448
  \[
wenzelm@61493
   449
  \<open>P (f x)\<close> \stackrel{\<open>f x \<equiv> f y\<close>}{\longmapsto}
wenzelm@61493
   450
  \<open>P (f y)\<close> \stackrel{\<open>y \<equiv> x\<close>}{\longmapsto}
wenzelm@61493
   451
  \<open>P (f x)\<close> \stackrel{\<open>f x \<equiv> f y\<close>}{\longmapsto} \cdots
wenzelm@50064
   452
  \]
wenzelm@62655
   453
  whereas applying the same to @{prop "y = x \<Longrightarrow> f x = f y \<Longrightarrow> P (f x) \<Longrightarrow> Q"}
wenzelm@62655
   454
  terminates (without solving the goal):
wenzelm@58618
   455
\<close>
wenzelm@50064
   456
wenzelm@50064
   457
lemma "y = x \<Longrightarrow> f x = f y \<Longrightarrow> P (f x) \<Longrightarrow> Q"
wenzelm@50064
   458
  apply simp
wenzelm@50064
   459
  oops
wenzelm@50064
   460
wenzelm@62655
   461
text \<open>
wenzelm@62655
   462
  See also \secref{sec:simp-trace} for options to enable Simplifier trace
wenzelm@62655
   463
  mode, which often helps to diagnose problems with rewrite systems.
wenzelm@58618
   464
\<close>
wenzelm@50064
   465
wenzelm@50064
   466
wenzelm@58618
   467
subsection \<open>Declaring rules \label{sec:simp-rules}\<close>
wenzelm@26782
   468
wenzelm@58618
   469
text \<open>
wenzelm@26782
   470
  \begin{matharray}{rcl}
wenzelm@61493
   471
    @{attribute_def simp} & : & \<open>attribute\<close> \\
wenzelm@61493
   472
    @{attribute_def split} & : & \<open>attribute\<close> \\
wenzelm@61493
   473
    @{attribute_def cong} & : & \<open>attribute\<close> \\
wenzelm@61493
   474
    @{command_def "print_simpset"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@26782
   475
  \end{matharray}
wenzelm@26782
   476
wenzelm@55112
   477
  @{rail \<open>
wenzelm@50063
   478
    (@@{attribute simp} | @@{attribute split} | @@{attribute cong})
wenzelm@50063
   479
      (() | 'add' | 'del')
wenzelm@59917
   480
    ;
wenzelm@59917
   481
    @@{command print_simpset} ('!'?)
wenzelm@55112
   482
  \<close>}
wenzelm@26782
   483
wenzelm@62655
   484
  \<^descr> @{attribute simp} declares rewrite rules, by adding or deleting them from
wenzelm@62655
   485
  the simpset within the theory or proof context. Rewrite rules are theorems
wenzelm@62655
   486
  expressing some form of equality, for example:
wenzelm@50076
   487
wenzelm@61493
   488
  \<open>Suc ?m + ?n = ?m + Suc ?n\<close> \\
wenzelm@61493
   489
  \<open>?P \<and> ?P \<longleftrightarrow> ?P\<close> \\
wenzelm@61493
   490
  \<open>?A \<union> ?B \<equiv> {x. x \<in> ?A \<or> x \<in> ?B}\<close>
wenzelm@50076
   491
wenzelm@61421
   492
  \<^medskip>
wenzelm@62655
   493
  Conditional rewrites such as \<open>?m < ?n \<Longrightarrow> ?m div ?n = 0\<close> are also permitted;
wenzelm@62655
   494
  the conditions can be arbitrary formulas.
wenzelm@50076
   495
wenzelm@61421
   496
  \<^medskip>
wenzelm@62655
   497
  Internally, all rewrite rules are translated into Pure equalities, theorems
wenzelm@62655
   498
  with conclusion \<open>lhs \<equiv> rhs\<close>. The simpset contains a function for extracting
wenzelm@62655
   499
  equalities from arbitrary theorems, which is usually installed when the
wenzelm@62655
   500
  object-logic is configured initially. For example, \<open>\<not> ?x \<in> {}\<close> could be
wenzelm@62655
   501
  turned into \<open>?x \<in> {} \<equiv> False\<close>. Theorems that are declared as @{attribute
wenzelm@62655
   502
  simp} and local assumptions within a goal are treated uniformly in this
wenzelm@62655
   503
  respect.
wenzelm@50076
   504
wenzelm@62655
   505
  The Simplifier accepts the following formats for the \<open>lhs\<close> term:
wenzelm@50076
   506
wenzelm@62655
   507
    \<^enum> First-order patterns, considering the sublanguage of application of
wenzelm@62655
   508
    constant operators to variable operands, without \<open>\<lambda>\<close>-abstractions or
wenzelm@62655
   509
    functional variables. For example:
wenzelm@50076
   510
wenzelm@61493
   511
    \<open>(?x + ?y) + ?z \<equiv> ?x + (?y + ?z)\<close> \\
wenzelm@61493
   512
    \<open>f (f ?x ?y) ?z \<equiv> f ?x (f ?y ?z)\<close>
wenzelm@50076
   513
wenzelm@62655
   514
    \<^enum> Higher-order patterns in the sense of @{cite "nipkow-patterns"}. These
wenzelm@62655
   515
    are terms in \<open>\<beta>\<close>-normal form (this will always be the case unless you have
wenzelm@62655
   516
    done something strange) where each occurrence of an unknown is of the form
wenzelm@62655
   517
    \<open>?F x\<^sub>1 \<dots> x\<^sub>n\<close>, where the \<open>x\<^sub>i\<close> are distinct bound variables.
wenzelm@50076
   518
wenzelm@62655
   519
    For example, \<open>(\<forall>x. ?P x \<and> ?Q x) \<equiv> (\<forall>x. ?P x) \<and> (\<forall>x. ?Q x)\<close> or its
wenzelm@62655
   520
    symmetric form, since the \<open>rhs\<close> is also a higher-order pattern.
wenzelm@50076
   521
wenzelm@62655
   522
    \<^enum> Physical first-order patterns over raw \<open>\<lambda>\<close>-term structure without
wenzelm@62655
   523
    \<open>\<alpha>\<beta>\<eta>\<close>-equality; abstractions and bound variables are treated like
wenzelm@62655
   524
    quasi-constant term material.
wenzelm@50076
   525
wenzelm@62655
   526
    For example, the rule \<open>?f ?x \<in> range ?f = True\<close> rewrites the term \<open>g a \<in>
wenzelm@62655
   527
    range g\<close> to \<open>True\<close>, but will fail to match \<open>g (h b) \<in> range (\<lambda>x. g (h
wenzelm@62655
   528
    x))\<close>. However, offending subterms (in our case \<open>?f ?x\<close>, which is not a
wenzelm@62655
   529
    pattern) can be replaced by adding new variables and conditions like this:
wenzelm@62655
   530
    \<open>?y = ?f ?x \<Longrightarrow> ?y \<in> range ?f = True\<close> is acceptable as a conditional rewrite
wenzelm@62655
   531
    rule of the second category since conditions can be arbitrary terms.
wenzelm@50076
   532
wenzelm@61439
   533
  \<^descr> @{attribute split} declares case split rules.
wenzelm@26782
   534
wenzelm@62655
   535
  \<^descr> @{attribute cong} declares congruence rules to the Simplifier context.
wenzelm@45645
   536
wenzelm@45645
   537
  Congruence rules are equalities of the form @{text [display]
wenzelm@45645
   538
  "\<dots> \<Longrightarrow> f ?x\<^sub>1 \<dots> ?x\<^sub>n = f ?y\<^sub>1 \<dots> ?y\<^sub>n"}
wenzelm@45645
   539
wenzelm@62655
   540
  This controls the simplification of the arguments of \<open>f\<close>. For example, some
wenzelm@62655
   541
  arguments can be simplified under additional assumptions:
wenzelm@62655
   542
  @{text [display]
wenzelm@62655
   543
    "?P\<^sub>1 \<longleftrightarrow> ?Q\<^sub>1 \<Longrightarrow>
wenzelm@62655
   544
    (?Q\<^sub>1 \<Longrightarrow> ?P\<^sub>2 \<longleftrightarrow> ?Q\<^sub>2) \<Longrightarrow>
wenzelm@62655
   545
    (?P\<^sub>1 \<longrightarrow> ?P\<^sub>2) \<longleftrightarrow> (?Q\<^sub>1 \<longrightarrow> ?Q\<^sub>2)"}
wenzelm@45645
   546
wenzelm@62655
   547
  Given this rule, the Simplifier assumes \<open>?Q\<^sub>1\<close> and extracts rewrite rules
wenzelm@62655
   548
  from it when simplifying \<open>?P\<^sub>2\<close>. Such local assumptions are effective for
wenzelm@62655
   549
  rewriting formulae such as \<open>x = 0 \<longrightarrow> y + x = y\<close>.
wenzelm@45645
   550
wenzelm@45645
   551
  %FIXME
wenzelm@45645
   552
  %The local assumptions are also provided as theorems to the solver;
wenzelm@45645
   553
  %see \secref{sec:simp-solver} below.
wenzelm@45645
   554
wenzelm@61421
   555
  \<^medskip>
wenzelm@62655
   556
  The following congruence rule for bounded quantifiers also supplies
wenzelm@62655
   557
  contextual information --- about the bound variable: @{text [display]
wenzelm@62655
   558
  "(?A = ?B) \<Longrightarrow>
wenzelm@62655
   559
    (\<And>x. x \<in> ?B \<Longrightarrow> ?P x \<longleftrightarrow> ?Q x) \<Longrightarrow>
wenzelm@45645
   560
    (\<forall>x \<in> ?A. ?P x) \<longleftrightarrow> (\<forall>x \<in> ?B. ?Q x)"}
wenzelm@45645
   561
wenzelm@61421
   562
  \<^medskip>
wenzelm@62655
   563
  This congruence rule for conditional expressions can supply contextual
wenzelm@62655
   564
  information for simplifying the arms: @{text [display]
wenzelm@62655
   565
  "?p = ?q \<Longrightarrow>
wenzelm@62655
   566
    (?q \<Longrightarrow> ?a = ?c) \<Longrightarrow>
wenzelm@62655
   567
    (\<not> ?q \<Longrightarrow> ?b = ?d) \<Longrightarrow>
wenzelm@45645
   568
    (if ?p then ?a else ?b) = (if ?q then ?c else ?d)"}
wenzelm@45645
   569
wenzelm@62655
   570
  A congruence rule can also \<^emph>\<open>prevent\<close> simplification of some arguments. Here
wenzelm@62655
   571
  is an alternative congruence rule for conditional expressions that conforms
wenzelm@62655
   572
  to non-strict functional evaluation: @{text [display]
wenzelm@62655
   573
  "?p = ?q \<Longrightarrow>
wenzelm@62655
   574
    (if ?p then ?a else ?b) = (if ?q then ?a else ?b)"}
wenzelm@45645
   575
wenzelm@62655
   576
  Only the first argument is simplified; the others remain unchanged. This can
wenzelm@62655
   577
  make simplification much faster, but may require an extra case split over
wenzelm@62655
   578
  the condition \<open>?q\<close> to prove the goal.
wenzelm@50063
   579
wenzelm@62655
   580
  \<^descr> @{command "print_simpset"} prints the collection of rules declared to the
wenzelm@62655
   581
  Simplifier, which is also known as ``simpset'' internally; the ``\<open>!\<close>''
wenzelm@62655
   582
  option indicates extra verbosity.
wenzelm@50077
   583
wenzelm@62655
   584
  The implicit simpset of the theory context is propagated monotonically
wenzelm@62655
   585
  through the theory hierarchy: forming a new theory, the union of the
wenzelm@62655
   586
  simpsets of its imports are taken as starting point. Also note that
wenzelm@62655
   587
  definitional packages like @{command "datatype"}, @{command "primrec"},
wenzelm@62655
   588
  @{command "fun"} routinely declare Simplifier rules to the target context,
wenzelm@62655
   589
  while plain @{command "definition"} is an exception in \<^emph>\<open>not\<close> declaring
wenzelm@50065
   590
  anything.
wenzelm@50065
   591
wenzelm@61421
   592
  \<^medskip>
wenzelm@62655
   593
  It is up the user to manipulate the current simpset further by explicitly
wenzelm@62655
   594
  adding or deleting theorems as simplification rules, or installing other
wenzelm@62655
   595
  tools via simplification procedures (\secref{sec:simproc}). Good simpsets
wenzelm@62655
   596
  are hard to design. Rules that obviously simplify, like \<open>?n + 0 \<equiv> ?n\<close> are
wenzelm@62655
   597
  good candidates for the implicit simpset, unless a special non-normalizing
wenzelm@62655
   598
  behavior of certain operations is intended. More specific rules (such as
wenzelm@62655
   599
  distributive laws, which duplicate subterms) should be added only for
wenzelm@62655
   600
  specific proof steps. Conversely, sometimes a rule needs to be deleted just
wenzelm@62655
   601
  for some part of a proof. The need of frequent additions or deletions may
wenzelm@62655
   602
  indicate a poorly designed simpset.
wenzelm@50065
   603
wenzelm@50065
   604
  \begin{warn}
wenzelm@62655
   605
  The union of simpsets from theory imports (as described above) is not always
wenzelm@62655
   606
  a good starting point for the new theory. If some ancestors have deleted
wenzelm@62655
   607
  simplification rules because they are no longer wanted, while others have
wenzelm@62655
   608
  left those rules in, then the union will contain the unwanted rules, and
wenzelm@62655
   609
  thus have to be deleted again in the theory body.
wenzelm@50065
   610
  \end{warn}
wenzelm@58618
   611
\<close>
wenzelm@45645
   612
wenzelm@45645
   613
wenzelm@58618
   614
subsection \<open>Ordered rewriting with permutative rules\<close>
wenzelm@50080
   615
wenzelm@62655
   616
text \<open>
wenzelm@62655
   617
  A rewrite rule is \<^emph>\<open>permutative\<close> if the left-hand side and right-hand side
wenzelm@62655
   618
  are the equal up to renaming of variables. The most common permutative rule
wenzelm@62655
   619
  is commutativity: \<open>?x + ?y = ?y + ?x\<close>. Other examples include \<open>(?x - ?y) -
wenzelm@62655
   620
  ?z = (?x - ?z) - ?y\<close> in arithmetic and \<open>insert ?x (insert ?y ?A) = insert ?y
wenzelm@62655
   621
  (insert ?x ?A)\<close> for sets. Such rules are common enough to merit special
wenzelm@62655
   622
  attention.
wenzelm@50080
   623
wenzelm@62655
   624
  Because ordinary rewriting loops given such rules, the Simplifier employs a
wenzelm@62655
   625
  special strategy, called \<^emph>\<open>ordered rewriting\<close>. Permutative rules are
wenzelm@62655
   626
  detected and only applied if the rewriting step decreases the redex wrt.\ a
wenzelm@62655
   627
  given term ordering. For example, commutativity rewrites \<open>b + a\<close> to \<open>a + b\<close>,
wenzelm@62655
   628
  but then stops, because the redex cannot be decreased further in the sense
wenzelm@62655
   629
  of the term ordering.
wenzelm@50080
   630
wenzelm@62655
   631
  The default is lexicographic ordering of term structure, but this could be
wenzelm@62655
   632
  also changed locally for special applications via @{index_ML
wenzelm@62655
   633
  Simplifier.set_termless} in Isabelle/ML.
wenzelm@50080
   634
wenzelm@61421
   635
  \<^medskip>
wenzelm@62655
   636
  Permutative rewrite rules are declared to the Simplifier just like other
wenzelm@62655
   637
  rewrite rules. Their special status is recognized automatically, and their
wenzelm@62655
   638
  application is guarded by the term ordering accordingly.
wenzelm@62655
   639
\<close>
wenzelm@50080
   640
wenzelm@50080
   641
wenzelm@58618
   642
subsubsection \<open>Rewriting with AC operators\<close>
wenzelm@50080
   643
wenzelm@62655
   644
text \<open>
wenzelm@62655
   645
  Ordered rewriting is particularly effective in the case of
wenzelm@62655
   646
  associative-commutative operators. (Associativity by itself is not
wenzelm@62655
   647
  permutative.) When dealing with an AC-operator \<open>f\<close>, keep the following
wenzelm@62655
   648
  points in mind:
wenzelm@50080
   649
wenzelm@62655
   650
    \<^item> The associative law must always be oriented from left to right, namely
wenzelm@62655
   651
    \<open>f (f x y) z = f x (f y z)\<close>. The opposite orientation, if used with
wenzelm@62655
   652
    commutativity, leads to looping in conjunction with the standard term
wenzelm@62655
   653
    order.
wenzelm@50080
   654
wenzelm@62655
   655
    \<^item> To complete your set of rewrite rules, you must add not just
wenzelm@62655
   656
    associativity (A) and commutativity (C) but also a derived rule
wenzelm@62655
   657
    \<^emph>\<open>left-commutativity\<close> (LC): \<open>f x (f y z) = f y (f x z)\<close>.
wenzelm@50080
   658
wenzelm@50080
   659
  Ordered rewriting with the combination of A, C, and LC sorts a term
wenzelm@50080
   660
  lexicographically --- the rewriting engine imitates bubble-sort.
wenzelm@58618
   661
\<close>
wenzelm@50080
   662
wenzelm@59905
   663
experiment
wenzelm@50080
   664
  fixes f :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infix "\<bullet>" 60)
wenzelm@50080
   665
  assumes assoc: "(x \<bullet> y) \<bullet> z = x \<bullet> (y \<bullet> z)"
wenzelm@50080
   666
  assumes commute: "x \<bullet> y = y \<bullet> x"
wenzelm@50080
   667
begin
wenzelm@50080
   668
wenzelm@50080
   669
lemma left_commute: "x \<bullet> (y \<bullet> z) = y \<bullet> (x \<bullet> z)"
wenzelm@50080
   670
proof -
wenzelm@50080
   671
  have "(x \<bullet> y) \<bullet> z = (y \<bullet> x) \<bullet> z" by (simp only: commute)
wenzelm@50080
   672
  then show ?thesis by (simp only: assoc)
wenzelm@50080
   673
qed
wenzelm@50080
   674
wenzelm@50080
   675
lemmas AC_rules = assoc commute left_commute
wenzelm@50080
   676
wenzelm@62655
   677
text \<open>
wenzelm@62655
   678
  Thus the Simplifier is able to establish equalities with arbitrary
wenzelm@62655
   679
  permutations of subterms, by normalizing to a common standard form. For
wenzelm@62655
   680
  example:
wenzelm@62655
   681
\<close>
wenzelm@50080
   682
wenzelm@50080
   683
lemma "(b \<bullet> c) \<bullet> a = xxx"
wenzelm@50080
   684
  apply (simp only: AC_rules)
wenzelm@58618
   685
  txt \<open>@{subgoals}\<close>
wenzelm@50080
   686
  oops
wenzelm@50080
   687
wenzelm@50080
   688
lemma "(b \<bullet> c) \<bullet> a = a \<bullet> (b \<bullet> c)" by (simp only: AC_rules)
wenzelm@50080
   689
lemma "(b \<bullet> c) \<bullet> a = c \<bullet> (b \<bullet> a)" by (simp only: AC_rules)
wenzelm@50080
   690
lemma "(b \<bullet> c) \<bullet> a = (c \<bullet> b) \<bullet> a" by (simp only: AC_rules)
wenzelm@50080
   691
wenzelm@50080
   692
end
wenzelm@50080
   693
wenzelm@62655
   694
text \<open>
wenzelm@62655
   695
  Martin and Nipkow @{cite "martin-nipkow"} discuss the theory and give many
wenzelm@62655
   696
  examples; other algebraic structures are amenable to ordered rewriting, such
wenzelm@62655
   697
  as Boolean rings. The Boyer-Moore theorem prover @{cite bm88book} also
wenzelm@62655
   698
  employs ordered rewriting.
wenzelm@58618
   699
\<close>
wenzelm@50080
   700
wenzelm@50080
   701
wenzelm@58618
   702
subsubsection \<open>Re-orienting equalities\<close>
wenzelm@50080
   703
wenzelm@58618
   704
text \<open>Another application of ordered rewriting uses the derived rule
wenzelm@50080
   705
  @{thm [source] eq_commute}: @{thm [source = false] eq_commute} to
wenzelm@50080
   706
  reverse equations.
wenzelm@50080
   707
wenzelm@50080
   708
  This is occasionally useful to re-orient local assumptions according
wenzelm@50080
   709
  to the term ordering, when other built-in mechanisms of
wenzelm@58618
   710
  reorientation and mutual simplification fail to apply.\<close>
wenzelm@50080
   711
wenzelm@50080
   712
wenzelm@58618
   713
subsection \<open>Simplifier tracing and debugging \label{sec:simp-trace}\<close>
wenzelm@50063
   714
wenzelm@58618
   715
text \<open>
wenzelm@50063
   716
  \begin{tabular}{rcll}
wenzelm@61493
   717
    @{attribute_def simp_trace} & : & \<open>attribute\<close> & default \<open>false\<close> \\
wenzelm@61493
   718
    @{attribute_def simp_trace_depth_limit} & : & \<open>attribute\<close> & default \<open>1\<close> \\
wenzelm@61493
   719
    @{attribute_def simp_debug} & : & \<open>attribute\<close> & default \<open>false\<close> \\
wenzelm@61493
   720
    @{attribute_def simp_trace_new} & : & \<open>attribute\<close> \\
wenzelm@61493
   721
    @{attribute_def simp_break} & : & \<open>attribute\<close> \\
wenzelm@50063
   722
  \end{tabular}
wenzelm@61421
   723
  \<^medskip>
wenzelm@50063
   724
wenzelm@57591
   725
  @{rail \<open>
wenzelm@57591
   726
    @@{attribute simp_trace_new} ('interactive')? \<newline>
wenzelm@57591
   727
      ('mode' '=' ('full' | 'normal'))? \<newline>
wenzelm@57591
   728
      ('depth' '=' @{syntax nat})?
wenzelm@57591
   729
    ;
wenzelm@57591
   730
wenzelm@57591
   731
    @@{attribute simp_break} (@{syntax term}*)
wenzelm@57591
   732
  \<close>}
wenzelm@57591
   733
wenzelm@57591
   734
  These attributes and configurations options control various aspects of
wenzelm@57591
   735
  Simplifier tracing and debugging.
wenzelm@50063
   736
wenzelm@62655
   737
  \<^descr> @{attribute simp_trace} makes the Simplifier output internal operations.
wenzelm@62655
   738
  This includes rewrite steps, but also bookkeeping like modifications of the
wenzelm@62655
   739
  simpset.
wenzelm@50063
   740
wenzelm@62655
   741
  \<^descr> @{attribute simp_trace_depth_limit} limits the effect of @{attribute
wenzelm@62655
   742
  simp_trace} to the given depth of recursive Simplifier invocations (when
wenzelm@62655
   743
  solving conditions of rewrite rules).
wenzelm@50063
   744
wenzelm@62655
   745
  \<^descr> @{attribute simp_debug} makes the Simplifier output some extra information
wenzelm@62655
   746
  about internal operations. This includes any attempted invocation of
wenzelm@62655
   747
  simplification procedures.
wenzelm@50063
   748
wenzelm@61439
   749
  \<^descr> @{attribute simp_trace_new} controls Simplifier tracing within
wenzelm@58552
   750
  Isabelle/PIDE applications, notably Isabelle/jEdit @{cite "isabelle-jedit"}.
wenzelm@62655
   751
  This provides a hierarchical representation of the rewriting steps performed
wenzelm@62655
   752
  by the Simplifier.
wenzelm@57591
   753
wenzelm@57591
   754
  Users can configure the behaviour by specifying breakpoints, verbosity and
wenzelm@57591
   755
  enabling or disabling the interactive mode. In normal verbosity (the
wenzelm@62655
   756
  default), only rule applications matching a breakpoint will be shown to the
wenzelm@62655
   757
  user. In full verbosity, all rule applications will be logged. Interactive
wenzelm@62655
   758
  mode interrupts the normal flow of the Simplifier and defers the decision
wenzelm@62655
   759
  how to continue to the user via some GUI dialog.
wenzelm@57591
   760
wenzelm@61439
   761
  \<^descr> @{attribute simp_break} declares term or theorem breakpoints for
wenzelm@57591
   762
  @{attribute simp_trace_new} as described above. Term breakpoints are
wenzelm@57591
   763
  patterns which are checked for matches on the redex of a rule application.
wenzelm@57591
   764
  Theorem breakpoints trigger when the corresponding theorem is applied in a
wenzelm@57591
   765
  rewrite step. For example:
wenzelm@58618
   766
\<close>
wenzelm@50063
   767
wenzelm@59905
   768
(*<*)experiment begin(*>*)
wenzelm@57591
   769
declare conjI [simp_break]
wenzelm@57590
   770
declare [[simp_break "?x \<and> ?y"]]
wenzelm@59905
   771
(*<*)end(*>*)
wenzelm@57590
   772
wenzelm@50063
   773
wenzelm@58618
   774
subsection \<open>Simplification procedures \label{sec:simproc}\<close>
wenzelm@26782
   775
wenzelm@62655
   776
text \<open>
wenzelm@62655
   777
  Simplification procedures are ML functions that produce proven rewrite rules
wenzelm@62655
   778
  on demand. They are associated with higher-order patterns that approximate
wenzelm@62655
   779
  the left-hand sides of equations. The Simplifier first matches the current
wenzelm@62655
   780
  redex against one of the LHS patterns; if this succeeds, the corresponding
wenzelm@62655
   781
  ML function is invoked, passing the Simplifier context and redex term. Thus
wenzelm@62655
   782
  rules may be specifically fashioned for particular situations, resulting in
wenzelm@62655
   783
  a more powerful mechanism than term rewriting by a fixed set of rules.
wenzelm@42925
   784
wenzelm@62655
   785
  Any successful result needs to be a (possibly conditional) rewrite rule \<open>t \<equiv>
wenzelm@62655
   786
  u\<close> that is applicable to the current redex. The rule will be applied just as
wenzelm@62655
   787
  any ordinary rewrite rule. It is expected to be already in \<^emph>\<open>internal form\<close>,
wenzelm@62655
   788
  bypassing the automatic preprocessing of object-level equivalences.
wenzelm@42925
   789
wenzelm@26782
   790
  \begin{matharray}{rcl}
wenzelm@61493
   791
    @{command_def "simproc_setup"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
wenzelm@61493
   792
    simproc & : & \<open>attribute\<close> \\
wenzelm@26782
   793
  \end{matharray}
wenzelm@26782
   794
wenzelm@55112
   795
  @{rail \<open>
wenzelm@42596
   796
    @@{command simproc_setup} @{syntax name} '(' (@{syntax term} + '|') ')' '='
wenzelm@62913
   797
      @{syntax text};
wenzelm@26782
   798
wenzelm@42596
   799
    @@{attribute simproc} (('add' ':')? | 'del' ':') (@{syntax name}+)
wenzelm@55112
   800
  \<close>}
wenzelm@26782
   801
wenzelm@62655
   802
  \<^descr> @{command "simproc_setup"} defines a named simplification procedure that
wenzelm@62655
   803
  is invoked by the Simplifier whenever any of the given term patterns match
wenzelm@62655
   804
  the current redex. The implementation, which is provided as ML source text,
wenzelm@62655
   805
  needs to be of type
wenzelm@62655
   806
  @{ML_type "morphism -> Proof.context -> cterm -> thm option"}, where the
wenzelm@62655
   807
  @{ML_type cterm} represents the current redex \<open>r\<close> and the result is supposed
wenzelm@62655
   808
  to be some proven rewrite rule \<open>r \<equiv> r'\<close> (or a generalized version), or @{ML
wenzelm@62655
   809
  NONE} to indicate failure. The @{ML_type Proof.context} argument holds the
wenzelm@62655
   810
  full context of the current Simplifier invocation. The @{ML_type morphism}
wenzelm@62655
   811
  informs about the difference of the original compilation context wrt.\ the
wenzelm@62913
   812
  one of the actual application later on.
wenzelm@26782
   813
wenzelm@62913
   814
  Morphisms are only relevant for simprocs that are defined within a local
wenzelm@62913
   815
  target context, e.g.\ in a locale.
wenzelm@26782
   816
wenzelm@62655
   817
  \<^descr> \<open>simproc add: name\<close> and \<open>simproc del: name\<close> add or delete named simprocs
wenzelm@62655
   818
  to the current Simplifier context. The default is to add a simproc. Note
wenzelm@62655
   819
  that @{command "simproc_setup"} already adds the new simproc to the
wenzelm@62655
   820
  subsequent context.
wenzelm@58618
   821
\<close>
wenzelm@26782
   822
wenzelm@26782
   823
wenzelm@58618
   824
subsubsection \<open>Example\<close>
wenzelm@42925
   825
wenzelm@62655
   826
text \<open>
wenzelm@62655
   827
  The following simplification procedure for @{thm [source = false,
wenzelm@62655
   828
  show_types] unit_eq} in HOL performs fine-grained control over rule
wenzelm@62655
   829
  application, beyond higher-order pattern matching. Declaring @{thm unit_eq}
wenzelm@62655
   830
  as @{attribute simp} directly would make the Simplifier loop! Note that a
wenzelm@62655
   831
  version of this simplification procedure is already active in Isabelle/HOL.
wenzelm@62655
   832
\<close>
wenzelm@42925
   833
wenzelm@59905
   834
(*<*)experiment begin(*>*)
wenzelm@59782
   835
simproc_setup unit ("x::unit") =
wenzelm@59782
   836
  \<open>fn _ => fn _ => fn ct =>
wenzelm@59582
   837
    if HOLogic.is_unit (Thm.term_of ct) then NONE
wenzelm@59782
   838
    else SOME (mk_meta_eq @{thm unit_eq})\<close>
wenzelm@59905
   839
(*<*)end(*>*)
wenzelm@42925
   840
wenzelm@62655
   841
text \<open>
wenzelm@62655
   842
  Since the Simplifier applies simplification procedures frequently, it is
wenzelm@62655
   843
  important to make the failure check in ML reasonably fast.\<close>
wenzelm@42925
   844
wenzelm@42925
   845
wenzelm@58618
   846
subsection \<open>Configurable Simplifier strategies \label{sec:simp-strategies}\<close>
wenzelm@50079
   847
wenzelm@62655
   848
text \<open>
wenzelm@62655
   849
  The core term-rewriting engine of the Simplifier is normally used in
wenzelm@62655
   850
  combination with some add-on components that modify the strategy and allow
wenzelm@62655
   851
  to integrate other non-Simplifier proof tools. These may be reconfigured in
wenzelm@62655
   852
  ML as explained below. Even if the default strategies of object-logics like
wenzelm@62655
   853
  Isabelle/HOL are used unchanged, it helps to understand how the standard
wenzelm@62655
   854
  Simplifier strategies work.\<close>
wenzelm@50079
   855
wenzelm@50079
   856
wenzelm@58618
   857
subsubsection \<open>The subgoaler\<close>
wenzelm@50079
   858
wenzelm@58618
   859
text \<open>
wenzelm@50079
   860
  \begin{mldecls}
wenzelm@51717
   861
  @{index_ML Simplifier.set_subgoaler: "(Proof.context -> int -> tactic) ->
wenzelm@51717
   862
  Proof.context -> Proof.context"} \\
wenzelm@51717
   863
  @{index_ML Simplifier.prems_of: "Proof.context -> thm list"} \\
wenzelm@50079
   864
  \end{mldecls}
wenzelm@50079
   865
wenzelm@50079
   866
  The subgoaler is the tactic used to solve subgoals arising out of
wenzelm@62655
   867
  conditional rewrite rules or congruence rules. The default should be
wenzelm@62655
   868
  simplification itself. In rare situations, this strategy may need to be
wenzelm@62655
   869
  changed. For example, if the premise of a conditional rule is an instance of
wenzelm@62655
   870
  its conclusion, as in \<open>Suc ?m < ?n \<Longrightarrow> ?m < ?n\<close>, the default strategy could
wenzelm@62655
   871
  loop. % FIXME !??
wenzelm@50079
   872
wenzelm@62655
   873
    \<^descr> @{ML Simplifier.set_subgoaler}~\<open>tac ctxt\<close> sets the subgoaler of the
wenzelm@62655
   874
    context to \<open>tac\<close>. The tactic will be applied to the context of the running
wenzelm@62655
   875
    Simplifier instance.
wenzelm@50079
   876
wenzelm@62655
   877
    \<^descr> @{ML Simplifier.prems_of}~\<open>ctxt\<close> retrieves the current set of premises
wenzelm@62655
   878
    from the context. This may be non-empty only if the Simplifier has been
wenzelm@62655
   879
    told to utilize local assumptions in the first place (cf.\ the options in
wenzelm@62655
   880
    \secref{sec:simp-meth}).
wenzelm@50079
   881
wenzelm@50079
   882
  As an example, consider the following alternative subgoaler:
wenzelm@58618
   883
\<close>
wenzelm@50079
   884
wenzelm@59905
   885
ML_val \<open>
wenzelm@51717
   886
  fun subgoaler_tac ctxt =
wenzelm@58963
   887
    assume_tac ctxt ORELSE'
wenzelm@59498
   888
    resolve_tac ctxt (Simplifier.prems_of ctxt) ORELSE'
wenzelm@51717
   889
    asm_simp_tac ctxt
wenzelm@58618
   890
\<close>
wenzelm@50079
   891
wenzelm@62655
   892
text \<open>
wenzelm@62655
   893
  This tactic first tries to solve the subgoal by assumption or by resolving
wenzelm@62655
   894
  with with one of the premises, calling simplification only if that fails.\<close>
wenzelm@50079
   895
wenzelm@50079
   896
wenzelm@58618
   897
subsubsection \<open>The solver\<close>
wenzelm@50079
   898
wenzelm@58618
   899
text \<open>
wenzelm@50079
   900
  \begin{mldecls}
wenzelm@50079
   901
  @{index_ML_type solver} \\
wenzelm@51717
   902
  @{index_ML Simplifier.mk_solver: "string ->
wenzelm@51717
   903
  (Proof.context -> int -> tactic) -> solver"} \\
wenzelm@51717
   904
  @{index_ML_op setSolver: "Proof.context * solver -> Proof.context"} \\
wenzelm@51717
   905
  @{index_ML_op addSolver: "Proof.context * solver -> Proof.context"} \\
wenzelm@51717
   906
  @{index_ML_op setSSolver: "Proof.context * solver -> Proof.context"} \\
wenzelm@51717
   907
  @{index_ML_op addSSolver: "Proof.context * solver -> Proof.context"} \\
wenzelm@50079
   908
  \end{mldecls}
wenzelm@50079
   909
wenzelm@62655
   910
  A solver is a tactic that attempts to solve a subgoal after simplification.
wenzelm@62655
   911
  Its core functionality is to prove trivial subgoals such as @{prop "True"}
wenzelm@62655
   912
  and \<open>t = t\<close>, but object-logics might be more ambitious. For example,
wenzelm@62655
   913
  Isabelle/HOL performs a restricted version of linear arithmetic here.
wenzelm@50079
   914
wenzelm@62655
   915
  Solvers are packaged up in abstract type @{ML_type solver}, with @{ML
wenzelm@62655
   916
  Simplifier.mk_solver} as the only operation to create a solver.
wenzelm@50079
   917
wenzelm@61421
   918
  \<^medskip>
wenzelm@62655
   919
  Rewriting does not instantiate unknowns. For example, rewriting alone cannot
wenzelm@62655
   920
  prove \<open>a \<in> ?A\<close> since this requires instantiating \<open>?A\<close>. The solver, however,
wenzelm@62655
   921
  is an arbitrary tactic and may instantiate unknowns as it pleases. This is
wenzelm@62655
   922
  the only way the Simplifier can handle a conditional rewrite rule whose
wenzelm@62655
   923
  condition contains extra variables. When a simplification tactic is to be
wenzelm@62655
   924
  combined with other provers, especially with the Classical Reasoner, it is
wenzelm@62655
   925
  important whether it can be considered safe or not. For this reason a
wenzelm@62655
   926
  simpset contains two solvers: safe and unsafe.
wenzelm@50079
   927
wenzelm@62655
   928
  The standard simplification strategy solely uses the unsafe solver, which is
wenzelm@62655
   929
  appropriate in most cases. For special applications where the simplification
wenzelm@62655
   930
  process is not allowed to instantiate unknowns within the goal,
wenzelm@62655
   931
  simplification starts with the safe solver, but may still apply the ordinary
wenzelm@62655
   932
  unsafe one in nested simplifications for conditional rules or congruences.
wenzelm@62655
   933
  Note that in this way the overall tactic is not totally safe: it may
wenzelm@62655
   934
  instantiate unknowns that appear also in other subgoals.
wenzelm@50079
   935
wenzelm@62655
   936
  \<^descr> @{ML Simplifier.mk_solver}~\<open>name tac\<close> turns \<open>tac\<close> into a solver; the
wenzelm@62655
   937
  \<open>name\<close> is only attached as a comment and has no further significance.
wenzelm@50079
   938
wenzelm@62655
   939
  \<^descr> \<open>ctxt setSSolver solver\<close> installs \<open>solver\<close> as the safe solver of \<open>ctxt\<close>.
wenzelm@50079
   940
wenzelm@62655
   941
  \<^descr> \<open>ctxt addSSolver solver\<close> adds \<open>solver\<close> as an additional safe solver; it
wenzelm@62655
   942
  will be tried after the solvers which had already been present in \<open>ctxt\<close>.
wenzelm@50079
   943
wenzelm@62655
   944
  \<^descr> \<open>ctxt setSolver solver\<close> installs \<open>solver\<close> as the unsafe solver of \<open>ctxt\<close>.
wenzelm@62655
   945
wenzelm@62655
   946
  \<^descr> \<open>ctxt addSolver solver\<close> adds \<open>solver\<close> as an additional unsafe solver; it
wenzelm@62655
   947
  will be tried after the solvers which had already been present in \<open>ctxt\<close>.
wenzelm@50079
   948
wenzelm@50079
   949
wenzelm@61421
   950
  \<^medskip>
wenzelm@62655
   951
  The solver tactic is invoked with the context of the running Simplifier.
wenzelm@62655
   952
  Further operations may be used to retrieve relevant information, such as the
wenzelm@62655
   953
  list of local Simplifier premises via @{ML Simplifier.prems_of} --- this
wenzelm@62655
   954
  list may be non-empty only if the Simplifier runs in a mode that utilizes
wenzelm@62655
   955
  local assumptions (see also \secref{sec:simp-meth}). The solver is also
wenzelm@62655
   956
  presented the full goal including its assumptions in any case. Thus it can
wenzelm@62655
   957
  use these (e.g.\ by calling @{ML assume_tac}), even if the Simplifier proper
wenzelm@62655
   958
  happens to ignore local premises at the moment.
wenzelm@50079
   959
wenzelm@61421
   960
  \<^medskip>
wenzelm@62655
   961
  As explained before, the subgoaler is also used to solve the premises of
wenzelm@62655
   962
  congruence rules. These are usually of the form \<open>s = ?x\<close>, where \<open>s\<close> needs to
wenzelm@62655
   963
  be simplified and \<open>?x\<close> needs to be instantiated with the result. Typically,
wenzelm@50079
   964
  the subgoaler will invoke the Simplifier at some point, which will
wenzelm@62655
   965
  eventually call the solver. For this reason, solver tactics must be prepared
wenzelm@62655
   966
  to solve goals of the form \<open>t = ?x\<close>, usually by reflexivity. In particular,
wenzelm@62655
   967
  reflexivity should be tried before any of the fancy automated proof tools.
wenzelm@50079
   968
wenzelm@62655
   969
  It may even happen that due to simplification the subgoal is no longer an
wenzelm@62655
   970
  equality. For example, \<open>False \<longleftrightarrow> ?Q\<close> could be rewritten to \<open>\<not> ?Q\<close>. To cover
wenzelm@62655
   971
  this case, the solver could try resolving with the theorem \<open>\<not> False\<close> of the
wenzelm@50079
   972
  object-logic.
wenzelm@50079
   973
wenzelm@61421
   974
  \<^medskip>
wenzelm@50079
   975
  \begin{warn}
wenzelm@62655
   976
  If a premise of a congruence rule cannot be proved, then the congruence is
wenzelm@62655
   977
  ignored. This should only happen if the rule is \<^emph>\<open>conditional\<close> --- that is,
wenzelm@62655
   978
  contains premises not of the form \<open>t = ?x\<close>. Otherwise it indicates that some
wenzelm@62655
   979
  congruence rule, or possibly the subgoaler or solver, is faulty.
wenzelm@50079
   980
  \end{warn}
wenzelm@58618
   981
\<close>
wenzelm@50079
   982
wenzelm@50079
   983
wenzelm@58618
   984
subsubsection \<open>The looper\<close>
wenzelm@50079
   985
wenzelm@58618
   986
text \<open>
wenzelm@50079
   987
  \begin{mldecls}
wenzelm@51717
   988
  @{index_ML_op setloop: "Proof.context *
wenzelm@51717
   989
  (Proof.context -> int -> tactic) -> Proof.context"} \\
wenzelm@51717
   990
  @{index_ML_op addloop: "Proof.context *
wenzelm@51717
   991
  (string * (Proof.context -> int -> tactic))
wenzelm@51717
   992
  -> Proof.context"} \\
wenzelm@51717
   993
  @{index_ML_op delloop: "Proof.context * string -> Proof.context"} \\
wenzelm@51717
   994
  @{index_ML Splitter.add_split: "thm -> Proof.context -> Proof.context"} \\
wenzelm@51717
   995
  @{index_ML Splitter.del_split: "thm -> Proof.context -> Proof.context"} \\
wenzelm@50079
   996
  \end{mldecls}
wenzelm@50079
   997
wenzelm@62655
   998
  The looper is a list of tactics that are applied after simplification, in
wenzelm@62655
   999
  case the solver failed to solve the simplified goal. If the looper succeeds,
wenzelm@62655
  1000
  the simplification process is started all over again. Each of the subgoals
wenzelm@62655
  1001
  generated by the looper is attacked in turn, in reverse order.
wenzelm@50079
  1002
wenzelm@62655
  1003
  A typical looper is \<^emph>\<open>case splitting\<close>: the expansion of a conditional.
wenzelm@62655
  1004
  Another possibility is to apply an elimination rule on the assumptions. More
wenzelm@62655
  1005
  adventurous loopers could start an induction.
wenzelm@50079
  1006
wenzelm@62655
  1007
    \<^descr> \<open>ctxt setloop tac\<close> installs \<open>tac\<close> as the only looper tactic of \<open>ctxt\<close>.
wenzelm@50079
  1008
wenzelm@62655
  1009
    \<^descr> \<open>ctxt addloop (name, tac)\<close> adds \<open>tac\<close> as an additional looper tactic
wenzelm@62655
  1010
    with name \<open>name\<close>, which is significant for managing the collection of
wenzelm@62655
  1011
    loopers. The tactic will be tried after the looper tactics that had
wenzelm@62655
  1012
    already been present in \<open>ctxt\<close>.
wenzelm@50079
  1013
wenzelm@62655
  1014
    \<^descr> \<open>ctxt delloop name\<close> deletes the looper tactic that was associated with
wenzelm@62655
  1015
    \<open>name\<close> from \<open>ctxt\<close>.
wenzelm@50079
  1016
wenzelm@62655
  1017
    \<^descr> @{ML Splitter.add_split}~\<open>thm ctxt\<close> adds split tactics for \<open>thm\<close> as
wenzelm@62655
  1018
    additional looper tactics of \<open>ctxt\<close>.
wenzelm@50079
  1019
wenzelm@62655
  1020
    \<^descr> @{ML Splitter.del_split}~\<open>thm ctxt\<close> deletes the split tactic
wenzelm@62655
  1021
    corresponding to \<open>thm\<close> from the looper tactics of \<open>ctxt\<close>.
wenzelm@50079
  1022
wenzelm@62655
  1023
  The splitter replaces applications of a given function; the right-hand side
wenzelm@62655
  1024
  of the replacement can be anything. For example, here is a splitting rule
wenzelm@62655
  1025
  for conditional expressions:
wenzelm@50079
  1026
wenzelm@50079
  1027
  @{text [display] "?P (if ?Q ?x ?y) \<longleftrightarrow> (?Q \<longrightarrow> ?P ?x) \<and> (\<not> ?Q \<longrightarrow> ?P ?y)"}
wenzelm@50079
  1028
wenzelm@62655
  1029
  Another example is the elimination operator for Cartesian products (which
wenzelm@62655
  1030
  happens to be called @{const case_prod} in Isabelle/HOL:
wenzelm@50079
  1031
haftmann@61424
  1032
  @{text [display] "?P (case_prod ?f ?p) \<longleftrightarrow> (\<forall>a b. ?p = (a, b) \<longrightarrow> ?P (f a b))"}
wenzelm@50079
  1033
wenzelm@62655
  1034
  For technical reasons, there is a distinction between case splitting in the
wenzelm@62655
  1035
  conclusion and in the premises of a subgoal. The former is done by @{ML
wenzelm@62655
  1036
  Splitter.split_tac} with rules like @{thm [source] if_split} or @{thm
wenzelm@62655
  1037
  [source] option.split}, which do not split the subgoal, while the latter is
wenzelm@62655
  1038
  done by @{ML Splitter.split_asm_tac} with rules like @{thm [source]
wenzelm@62655
  1039
  if_split_asm} or @{thm [source] option.split_asm}, which split the subgoal.
wenzelm@62655
  1040
  The function @{ML Splitter.add_split} automatically takes care of which
wenzelm@62655
  1041
  tactic to call, analyzing the form of the rules given as argument; it is the
wenzelm@62655
  1042
  same operation behind \<open>split\<close> attribute or method modifier syntax in the
wenzelm@62655
  1043
  Isar source language.
wenzelm@50079
  1044
wenzelm@62655
  1045
  Case splits should be allowed only when necessary; they are expensive and
wenzelm@62655
  1046
  hard to control. Case-splitting on if-expressions in the conclusion is
wenzelm@62655
  1047
  usually beneficial, so it is enabled by default in Isabelle/HOL and
wenzelm@62655
  1048
  Isabelle/FOL/ZF.
wenzelm@50079
  1049
wenzelm@50079
  1050
  \begin{warn}
wenzelm@62655
  1051
  With @{ML Splitter.split_asm_tac} as looper component, the Simplifier may
wenzelm@62655
  1052
  split subgoals! This might cause unexpected problems in tactic expressions
wenzelm@62655
  1053
  that silently assume 0 or 1 subgoals after simplification.
wenzelm@50079
  1054
  \end{warn}
wenzelm@58618
  1055
\<close>
wenzelm@50079
  1056
wenzelm@50079
  1057
wenzelm@58618
  1058
subsection \<open>Forward simplification \label{sec:simp-forward}\<close>
wenzelm@26782
  1059
wenzelm@58618
  1060
text \<open>
wenzelm@26782
  1061
  \begin{matharray}{rcl}
wenzelm@61493
  1062
    @{attribute_def simplified} & : & \<open>attribute\<close> \\
wenzelm@26782
  1063
  \end{matharray}
wenzelm@26782
  1064
wenzelm@55112
  1065
  @{rail \<open>
wenzelm@62969
  1066
    @@{attribute simplified} opt? @{syntax thms}?
wenzelm@26782
  1067
    ;
wenzelm@26782
  1068
wenzelm@40255
  1069
    opt: '(' ('no_asm' | 'no_asm_simp' | 'no_asm_use') ')'
wenzelm@55112
  1070
  \<close>}
wenzelm@26782
  1071
wenzelm@62655
  1072
  \<^descr> @{attribute simplified}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> causes a theorem to be simplified,
wenzelm@62655
  1073
  either by exactly the specified rules \<open>a\<^sub>1, \<dots>, a\<^sub>n\<close>, or the implicit
wenzelm@62655
  1074
  Simplifier context if no arguments are given. The result is fully simplified
wenzelm@62655
  1075
  by default, including assumptions and conclusion; the options \<open>no_asm\<close> etc.\
wenzelm@62655
  1076
  tune the Simplifier in the same way as the for the \<open>simp\<close> method.
wenzelm@26782
  1077
wenzelm@62655
  1078
  Note that forward simplification restricts the Simplifier to its most basic
wenzelm@62655
  1079
  operation of term rewriting; solver and looper tactics
wenzelm@62655
  1080
  (\secref{sec:simp-strategies}) are \<^emph>\<open>not\<close> involved here. The @{attribute
wenzelm@62655
  1081
  simplified} attribute should be only rarely required under normal
wenzelm@62655
  1082
  circumstances.
wenzelm@58618
  1083
\<close>
wenzelm@26782
  1084
wenzelm@26782
  1085
wenzelm@58618
  1086
section \<open>The Classical Reasoner \label{sec:classical}\<close>
wenzelm@26782
  1087
wenzelm@58618
  1088
subsection \<open>Basic concepts\<close>
wenzelm@42927
  1089
wenzelm@58618
  1090
text \<open>Although Isabelle is generic, many users will be working in
wenzelm@42927
  1091
  some extension of classical first-order logic.  Isabelle/ZF is built
wenzelm@42927
  1092
  upon theory FOL, while Isabelle/HOL conceptually contains
wenzelm@42927
  1093
  first-order logic as a fragment.  Theorem-proving in predicate logic
wenzelm@42927
  1094
  is undecidable, but many automated strategies have been developed to
wenzelm@42927
  1095
  assist in this task.
wenzelm@42927
  1096
wenzelm@42927
  1097
  Isabelle's classical reasoner is a generic package that accepts
wenzelm@42927
  1098
  certain information about a logic and delivers a suite of automatic
wenzelm@42927
  1099
  proof tools, based on rules that are classified and declared in the
wenzelm@42927
  1100
  context.  These proof procedures are slow and simplistic compared
wenzelm@42927
  1101
  with high-end automated theorem provers, but they can save
wenzelm@42927
  1102
  considerable time and effort in practice.  They can prove theorems
wenzelm@58552
  1103
  such as Pelletier's @{cite pelletier86} problems 40 and 41 in a few
wenzelm@58618
  1104
  milliseconds (including full proof reconstruction):\<close>
wenzelm@42927
  1105
wenzelm@42927
  1106
lemma "(\<exists>y. \<forall>x. F x y \<longleftrightarrow> F x x) \<longrightarrow> \<not> (\<forall>x. \<exists>y. \<forall>z. F z y \<longleftrightarrow> \<not> F z x)"
wenzelm@42927
  1107
  by blast
wenzelm@42927
  1108
wenzelm@42927
  1109
lemma "(\<forall>z. \<exists>y. \<forall>x. f x y \<longleftrightarrow> f x z \<and> \<not> f x x) \<longrightarrow> \<not> (\<exists>z. \<forall>x. f x z)"
wenzelm@42927
  1110
  by blast
wenzelm@42927
  1111
wenzelm@58618
  1112
text \<open>The proof tools are generic.  They are not restricted to
wenzelm@42927
  1113
  first-order logic, and have been heavily used in the development of
wenzelm@42927
  1114
  the Isabelle/HOL library and applications.  The tactics can be
wenzelm@42927
  1115
  traced, and their components can be called directly; in this manner,
wenzelm@58618
  1116
  any proof can be viewed interactively.\<close>
wenzelm@42927
  1117
wenzelm@42927
  1118
wenzelm@58618
  1119
subsubsection \<open>The sequent calculus\<close>
wenzelm@42927
  1120
wenzelm@58618
  1121
text \<open>Isabelle supports natural deduction, which is easy to use for
wenzelm@42927
  1122
  interactive proof.  But natural deduction does not easily lend
wenzelm@42927
  1123
  itself to automation, and has a bias towards intuitionism.  For
wenzelm@42927
  1124
  certain proofs in classical logic, it can not be called natural.
wenzelm@61477
  1125
  The \<^emph>\<open>sequent calculus\<close>, a generalization of natural deduction,
wenzelm@42927
  1126
  is easier to automate.
wenzelm@42927
  1127
wenzelm@61493
  1128
  A \<^bold>\<open>sequent\<close> has the form \<open>\<Gamma> \<turnstile> \<Delta>\<close>, where \<open>\<Gamma>\<close>
wenzelm@61572
  1129
  and \<open>\<Delta>\<close> are sets of formulae.\<^footnote>\<open>For first-order
wenzelm@42927
  1130
  logic, sequents can equivalently be made from lists or multisets of
wenzelm@61572
  1131
  formulae.\<close> The sequent \<open>P\<^sub>1, \<dots>, P\<^sub>m \<turnstile> Q\<^sub>1, \<dots>, Q\<^sub>n\<close> is
wenzelm@61493
  1132
  \<^bold>\<open>valid\<close> if \<open>P\<^sub>1 \<and> \<dots> \<and> P\<^sub>m\<close> implies \<open>Q\<^sub>1 \<or> \<dots> \<or>
wenzelm@61493
  1133
  Q\<^sub>n\<close>.  Thus \<open>P\<^sub>1, \<dots>, P\<^sub>m\<close> represent assumptions, each of which
wenzelm@61493
  1134
  is true, while \<open>Q\<^sub>1, \<dots>, Q\<^sub>n\<close> represent alternative goals.  A
wenzelm@61477
  1135
  sequent is \<^bold>\<open>basic\<close> if its left and right sides have a common
wenzelm@61493
  1136
  formula, as in \<open>P, Q \<turnstile> Q, R\<close>; basic sequents are trivially
wenzelm@42927
  1137
  valid.
wenzelm@42927
  1138
wenzelm@61477
  1139
  Sequent rules are classified as \<^bold>\<open>right\<close> or \<^bold>\<open>left\<close>,
wenzelm@61493
  1140
  indicating which side of the \<open>\<turnstile>\<close> symbol they operate on.
wenzelm@42927
  1141
  Rules that operate on the right side are analogous to natural
wenzelm@42927
  1142
  deduction's introduction rules, and left rules are analogous to
wenzelm@61493
  1143
  elimination rules.  The sequent calculus analogue of \<open>(\<longrightarrow>I)\<close>
wenzelm@42927
  1144
  is the rule
wenzelm@42927
  1145
  \[
wenzelm@61493
  1146
  \infer[\<open>(\<longrightarrow>R)\<close>]{\<open>\<Gamma> \<turnstile> \<Delta>, P \<longrightarrow> Q\<close>}{\<open>P, \<Gamma> \<turnstile> \<Delta>, Q\<close>}
wenzelm@42927
  1147
  \]
wenzelm@42927
  1148
  Applying the rule backwards, this breaks down some implication on
wenzelm@61493
  1149
  the right side of a sequent; \<open>\<Gamma>\<close> and \<open>\<Delta>\<close> stand for
wenzelm@42927
  1150
  the sets of formulae that are unaffected by the inference.  The
wenzelm@61493
  1151
  analogue of the pair \<open>(\<or>I1)\<close> and \<open>(\<or>I2)\<close> is the
wenzelm@42927
  1152
  single rule
wenzelm@42927
  1153
  \[
wenzelm@61493
  1154
  \infer[\<open>(\<or>R)\<close>]{\<open>\<Gamma> \<turnstile> \<Delta>, P \<or> Q\<close>}{\<open>\<Gamma> \<turnstile> \<Delta>, P, Q\<close>}
wenzelm@42927
  1155
  \]
wenzelm@42927
  1156
  This breaks down some disjunction on the right side, replacing it by
wenzelm@42927
  1157
  both disjuncts.  Thus, the sequent calculus is a kind of
wenzelm@42927
  1158
  multiple-conclusion logic.
wenzelm@42927
  1159
wenzelm@42927
  1160
  To illustrate the use of multiple formulae on the right, let us
wenzelm@61493
  1161
  prove the classical theorem \<open>(P \<longrightarrow> Q) \<or> (Q \<longrightarrow> P)\<close>.  Working
wenzelm@42927
  1162
  backwards, we reduce this formula to a basic sequent:
wenzelm@42927
  1163
  \[
wenzelm@61493
  1164
  \infer[\<open>(\<or>R)\<close>]{\<open>\<turnstile> (P \<longrightarrow> Q) \<or> (Q \<longrightarrow> P)\<close>}
wenzelm@61493
  1165
    {\infer[\<open>(\<longrightarrow>R)\<close>]{\<open>\<turnstile> (P \<longrightarrow> Q), (Q \<longrightarrow> P)\<close>}
wenzelm@61493
  1166
      {\infer[\<open>(\<longrightarrow>R)\<close>]{\<open>P \<turnstile> Q, (Q \<longrightarrow> P)\<close>}
wenzelm@61493
  1167
        {\<open>P, Q \<turnstile> Q, P\<close>}}}
wenzelm@42927
  1168
  \]
wenzelm@42927
  1169
wenzelm@42927
  1170
  This example is typical of the sequent calculus: start with the
wenzelm@42927
  1171
  desired theorem and apply rules backwards in a fairly arbitrary
wenzelm@42927
  1172
  manner.  This yields a surprisingly effective proof procedure.
wenzelm@42927
  1173
  Quantifiers add only few complications, since Isabelle handles
wenzelm@58552
  1174
  parameters and schematic variables.  See @{cite \<open>Chapter 10\<close>
wenzelm@58618
  1175
  "paulson-ml2"} for further discussion.\<close>
wenzelm@42927
  1176
wenzelm@42927
  1177
wenzelm@58618
  1178
subsubsection \<open>Simulating sequents by natural deduction\<close>
wenzelm@42927
  1179
wenzelm@58618
  1180
text \<open>Isabelle can represent sequents directly, as in the
wenzelm@42927
  1181
  object-logic LK.  But natural deduction is easier to work with, and
wenzelm@42927
  1182
  most object-logics employ it.  Fortunately, we can simulate the
wenzelm@61493
  1183
  sequent \<open>P\<^sub>1, \<dots>, P\<^sub>m \<turnstile> Q\<^sub>1, \<dots>, Q\<^sub>n\<close> by the Isabelle formula
wenzelm@61493
  1184
  \<open>P\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> P\<^sub>m \<Longrightarrow> \<not> Q\<^sub>2 \<Longrightarrow> ... \<Longrightarrow> \<not> Q\<^sub>n \<Longrightarrow> Q\<^sub>1\<close> where the order of
wenzelm@61493
  1185
  the assumptions and the choice of \<open>Q\<^sub>1\<close> are arbitrary.
wenzelm@42927
  1186
  Elim-resolution plays a key role in simulating sequent proofs.
wenzelm@42927
  1187
wenzelm@42927
  1188
  We can easily handle reasoning on the left.  Elim-resolution with
wenzelm@61493
  1189
  the rules \<open>(\<or>E)\<close>, \<open>(\<bottom>E)\<close> and \<open>(\<exists>E)\<close> achieves
wenzelm@42927
  1190
  a similar effect as the corresponding sequent rules.  For the other
wenzelm@42927
  1191
  connectives, we use sequent-style elimination rules instead of
wenzelm@61493
  1192
  destruction rules such as \<open>(\<and>E1, 2)\<close> and \<open>(\<forall>E)\<close>.
wenzelm@61493
  1193
  But note that the rule \<open>(\<not>L)\<close> has no effect under our
wenzelm@42927
  1194
  representation of sequents!
wenzelm@42927
  1195
  \[
wenzelm@61493
  1196
  \infer[\<open>(\<not>L)\<close>]{\<open>\<not> P, \<Gamma> \<turnstile> \<Delta>\<close>}{\<open>\<Gamma> \<turnstile> \<Delta>, P\<close>}
wenzelm@42927
  1197
  \]
wenzelm@42927
  1198
wenzelm@42927
  1199
  What about reasoning on the right?  Introduction rules can only
wenzelm@61493
  1200
  affect the formula in the conclusion, namely \<open>Q\<^sub>1\<close>.  The
wenzelm@42927
  1201
  other right-side formulae are represented as negated assumptions,
wenzelm@61493
  1202
  \<open>\<not> Q\<^sub>2, \<dots>, \<not> Q\<^sub>n\<close>.  In order to operate on one of these, it
wenzelm@61493
  1203
  must first be exchanged with \<open>Q\<^sub>1\<close>.  Elim-resolution with the
wenzelm@61493
  1204
  \<open>swap\<close> rule has this effect: \<open>\<not> P \<Longrightarrow> (\<not> R \<Longrightarrow> P) \<Longrightarrow> R\<close>
wenzelm@42927
  1205
wenzelm@42927
  1206
  To ensure that swaps occur only when necessary, each introduction
wenzelm@42927
  1207
  rule is converted into a swapped form: it is resolved with the
wenzelm@61493
  1208
  second premise of \<open>(swap)\<close>.  The swapped form of \<open>(\<and>I)\<close>, which might be called \<open>(\<not>\<and>E)\<close>, is
wenzelm@42927
  1209
  @{text [display] "\<not> (P \<and> Q) \<Longrightarrow> (\<not> R \<Longrightarrow> P) \<Longrightarrow> (\<not> R \<Longrightarrow> Q) \<Longrightarrow> R"}
wenzelm@42927
  1210
wenzelm@61493
  1211
  Similarly, the swapped form of \<open>(\<longrightarrow>I)\<close> is
wenzelm@42927
  1212
  @{text [display] "\<not> (P \<longrightarrow> Q) \<Longrightarrow> (\<not> R \<Longrightarrow> P \<Longrightarrow> Q) \<Longrightarrow> R"}
wenzelm@42927
  1213
wenzelm@42927
  1214
  Swapped introduction rules are applied using elim-resolution, which
wenzelm@42927
  1215
  deletes the negated formula.  Our representation of sequents also
wenzelm@42927
  1216
  requires the use of ordinary introduction rules.  If we had no
wenzelm@42927
  1217
  regard for readability of intermediate goal states, we could treat
wenzelm@42927
  1218
  the right side more uniformly by representing sequents as @{text
wenzelm@42927
  1219
  [display] "P\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> P\<^sub>m \<Longrightarrow> \<not> Q\<^sub>1 \<Longrightarrow> \<dots> \<Longrightarrow> \<not> Q\<^sub>n \<Longrightarrow> \<bottom>"}
wenzelm@58618
  1220
\<close>
wenzelm@42927
  1221
wenzelm@42927
  1222
wenzelm@58618
  1223
subsubsection \<open>Extra rules for the sequent calculus\<close>
wenzelm@42927
  1224
wenzelm@61493
  1225
text \<open>As mentioned, destruction rules such as \<open>(\<and>E1, 2)\<close> and
wenzelm@61493
  1226
  \<open>(\<forall>E)\<close> must be replaced by sequent-style elimination rules.
wenzelm@42927
  1227
  In addition, we need rules to embody the classical equivalence
wenzelm@61493
  1228
  between \<open>P \<longrightarrow> Q\<close> and \<open>\<not> P \<or> Q\<close>.  The introduction
wenzelm@61493
  1229
  rules \<open>(\<or>I1, 2)\<close> are replaced by a rule that simulates
wenzelm@61493
  1230
  \<open>(\<or>R)\<close>: @{text [display] "(\<not> Q \<Longrightarrow> P) \<Longrightarrow> P \<or> Q"}
wenzelm@42927
  1231
wenzelm@61493
  1232
  The destruction rule \<open>(\<longrightarrow>E)\<close> is replaced by @{text [display]
wenzelm@42927
  1233
  "(P \<longrightarrow> Q) \<Longrightarrow> (\<not> P \<Longrightarrow> R) \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"}
wenzelm@42927
  1234
wenzelm@42927
  1235
  Quantifier replication also requires special rules.  In classical
wenzelm@61493
  1236
  logic, \<open>\<exists>x. P x\<close> is equivalent to \<open>\<not> (\<forall>x. \<not> P x)\<close>;
wenzelm@61493
  1237
  the rules \<open>(\<exists>R)\<close> and \<open>(\<forall>L)\<close> are dual:
wenzelm@42927
  1238
  \[
wenzelm@61493
  1239
  \infer[\<open>(\<exists>R)\<close>]{\<open>\<Gamma> \<turnstile> \<Delta>, \<exists>x. P x\<close>}{\<open>\<Gamma> \<turnstile> \<Delta>, \<exists>x. P x, P t\<close>}
wenzelm@42927
  1240
  \qquad
wenzelm@61493
  1241
  \infer[\<open>(\<forall>L)\<close>]{\<open>\<forall>x. P x, \<Gamma> \<turnstile> \<Delta>\<close>}{\<open>P t, \<forall>x. P x, \<Gamma> \<turnstile> \<Delta>\<close>}
wenzelm@42927
  1242
  \]
wenzelm@42927
  1243
  Thus both kinds of quantifier may be replicated.  Theorems requiring
wenzelm@42927
  1244
  multiple uses of a universal formula are easy to invent; consider
wenzelm@42927
  1245
  @{text [display] "(\<forall>x. P x \<longrightarrow> P (f x)) \<and> P a \<longrightarrow> P (f\<^sup>n a)"} for any
wenzelm@61493
  1246
  \<open>n > 1\<close>.  Natural examples of the multiple use of an
wenzelm@61493
  1247
  existential formula are rare; a standard one is \<open>\<exists>x. \<forall>y. P x
wenzelm@61493
  1248
  \<longrightarrow> P y\<close>.
wenzelm@42927
  1249
wenzelm@42927
  1250
  Forgoing quantifier replication loses completeness, but gains
wenzelm@42927
  1251
  decidability, since the search space becomes finite.  Many useful
wenzelm@42927
  1252
  theorems can be proved without replication, and the search generally
wenzelm@42927
  1253
  delivers its verdict in a reasonable time.  To adopt this approach,
wenzelm@61493
  1254
  represent the sequent rules \<open>(\<exists>R)\<close>, \<open>(\<exists>L)\<close> and
wenzelm@61493
  1255
  \<open>(\<forall>R)\<close> by \<open>(\<exists>I)\<close>, \<open>(\<exists>E)\<close> and \<open>(\<forall>I)\<close>,
wenzelm@61493
  1256
  respectively, and put \<open>(\<forall>E)\<close> into elimination form: @{text
wenzelm@42927
  1257
  [display] "\<forall>x. P x \<Longrightarrow> (P t \<Longrightarrow> Q) \<Longrightarrow> Q"}
wenzelm@42927
  1258
wenzelm@42927
  1259
  Elim-resolution with this rule will delete the universal formula
wenzelm@42927
  1260
  after a single use.  To replicate universal quantifiers, replace the
wenzelm@42927
  1261
  rule by @{text [display] "\<forall>x. P x \<Longrightarrow> (P t \<Longrightarrow> \<forall>x. P x \<Longrightarrow> Q) \<Longrightarrow> Q"}
wenzelm@42927
  1262
wenzelm@61493
  1263
  To replicate existential quantifiers, replace \<open>(\<exists>I)\<close> by
wenzelm@42927
  1264
  @{text [display] "(\<not> (\<exists>x. P x) \<Longrightarrow> P t) \<Longrightarrow> \<exists>x. P x"}
wenzelm@42927
  1265
wenzelm@42927
  1266
  All introduction rules mentioned above are also useful in swapped
wenzelm@42927
  1267
  form.
wenzelm@42927
  1268
wenzelm@42927
  1269
  Replication makes the search space infinite; we must apply the rules
wenzelm@42927
  1270
  with care.  The classical reasoner distinguishes between safe and
wenzelm@42927
  1271
  unsafe rules, applying the latter only when there is no alternative.
wenzelm@42927
  1272
  Depth-first search may well go down a blind alley; best-first search
wenzelm@42927
  1273
  is better behaved in an infinite search space.  However, quantifier
wenzelm@42927
  1274
  replication is too expensive to prove any but the simplest theorems.
wenzelm@58618
  1275
\<close>
wenzelm@42927
  1276
wenzelm@42927
  1277
wenzelm@58618
  1278
subsection \<open>Rule declarations\<close>
wenzelm@42928
  1279
wenzelm@58618
  1280
text \<open>The proof tools of the Classical Reasoner depend on
wenzelm@42928
  1281
  collections of rules declared in the context, which are classified
wenzelm@61477
  1282
  as introduction, elimination or destruction and as \<^emph>\<open>safe\<close> or
wenzelm@61477
  1283
  \<^emph>\<open>unsafe\<close>.  In general, safe rules can be attempted blindly,
wenzelm@42928
  1284
  while unsafe rules must be used with care.  A safe rule must never
wenzelm@42928
  1285
  reduce a provable goal to an unprovable set of subgoals.
wenzelm@42928
  1286
wenzelm@61493
  1287
  The rule \<open>P \<Longrightarrow> P \<or> Q\<close> is unsafe because it reduces \<open>P
wenzelm@61493
  1288
  \<or> Q\<close> to \<open>P\<close>, which might turn out as premature choice of an
wenzelm@42928
  1289
  unprovable subgoal.  Any rule is unsafe whose premises contain new
wenzelm@61493
  1290
  unknowns.  The elimination rule \<open>\<forall>x. P x \<Longrightarrow> (P t \<Longrightarrow> Q) \<Longrightarrow> Q\<close> is
wenzelm@42928
  1291
  unsafe, since it is applied via elim-resolution, which discards the
wenzelm@61493
  1292
  assumption \<open>\<forall>x. P x\<close> and replaces it by the weaker
wenzelm@61493
  1293
  assumption \<open>P t\<close>.  The rule \<open>P t \<Longrightarrow> \<exists>x. P x\<close> is
wenzelm@61493
  1294
  unsafe for similar reasons.  The quantifier duplication rule \<open>\<forall>x. P x \<Longrightarrow> (P t \<Longrightarrow> \<forall>x. P x \<Longrightarrow> Q) \<Longrightarrow> Q\<close> is unsafe in a different sense:
wenzelm@61493
  1295
  since it keeps the assumption \<open>\<forall>x. P x\<close>, it is prone to
wenzelm@42928
  1296
  looping.  In classical first-order logic, all rules are safe except
wenzelm@42928
  1297
  those mentioned above.
wenzelm@42928
  1298
wenzelm@42928
  1299
  The safe~/ unsafe distinction is vague, and may be regarded merely
wenzelm@42928
  1300
  as a way of giving some rules priority over others.  One could argue
wenzelm@61493
  1301
  that \<open>(\<or>E)\<close> is unsafe, because repeated application of it
wenzelm@42928
  1302
  could generate exponentially many subgoals.  Induction rules are
wenzelm@42928
  1303
  unsafe because inductive proofs are difficult to set up
wenzelm@42928
  1304
  automatically.  Any inference is unsafe that instantiates an unknown
wenzelm@42928
  1305
  in the proof state --- thus matching must be used, rather than
wenzelm@42928
  1306
  unification.  Even proof by assumption is unsafe if it instantiates
wenzelm@42928
  1307
  unknowns shared with other subgoals.
wenzelm@42928
  1308
wenzelm@42928
  1309
  \begin{matharray}{rcl}
wenzelm@61493
  1310
    @{command_def "print_claset"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
  1311
    @{attribute_def intro} & : & \<open>attribute\<close> \\
wenzelm@61493
  1312
    @{attribute_def elim} & : & \<open>attribute\<close> \\
wenzelm@61493
  1313
    @{attribute_def dest} & : & \<open>attribute\<close> \\
wenzelm@61493
  1314
    @{attribute_def rule} & : & \<open>attribute\<close> \\
wenzelm@61493
  1315
    @{attribute_def iff} & : & \<open>attribute\<close> \\
wenzelm@61493
  1316
    @{attribute_def swapped} & : & \<open>attribute\<close> \\
wenzelm@42928
  1317
  \end{matharray}
wenzelm@42928
  1318
wenzelm@55112
  1319
  @{rail \<open>
wenzelm@42928
  1320
    (@@{attribute intro} | @@{attribute elim} | @@{attribute dest}) ('!' | () | '?') @{syntax nat}?
wenzelm@42928
  1321
    ;
wenzelm@42928
  1322
    @@{attribute rule} 'del'
wenzelm@42928
  1323
    ;
wenzelm@42928
  1324
    @@{attribute iff} (((() | 'add') '?'?) | 'del')
wenzelm@55112
  1325
  \<close>}
wenzelm@42928
  1326
wenzelm@61439
  1327
  \<^descr> @{command "print_claset"} prints the collection of rules
wenzelm@42928
  1328
  declared to the Classical Reasoner, i.e.\ the @{ML_type claset}
wenzelm@42928
  1329
  within the context.
wenzelm@42928
  1330
wenzelm@61439
  1331
  \<^descr> @{attribute intro}, @{attribute elim}, and @{attribute dest}
wenzelm@42928
  1332
  declare introduction, elimination, and destruction rules,
wenzelm@61477
  1333
  respectively.  By default, rules are considered as \<^emph>\<open>unsafe\<close>
wenzelm@61493
  1334
  (i.e.\ not applied blindly without backtracking), while ``\<open>!\<close>'' classifies as \<^emph>\<open>safe\<close>.  Rule declarations marked by
wenzelm@61493
  1335
  ``\<open>?\<close>'' coincide with those of Isabelle/Pure, cf.\
wenzelm@42928
  1336
  \secref{sec:pure-meth-att} (i.e.\ are only applied in single steps
wenzelm@42928
  1337
  of the @{method rule} method).  The optional natural number
wenzelm@42928
  1338
  specifies an explicit weight argument, which is ignored by the
wenzelm@42928
  1339
  automated reasoning tools, but determines the search order of single
wenzelm@42928
  1340
  rule steps.
wenzelm@42928
  1341
wenzelm@42928
  1342
  Introduction rules are those that can be applied using ordinary
wenzelm@42928
  1343
  resolution.  Their swapped forms are generated internally, which
wenzelm@42928
  1344
  will be applied using elim-resolution.  Elimination rules are
wenzelm@42928
  1345
  applied using elim-resolution.  Rules are sorted by the number of
wenzelm@42928
  1346
  new subgoals they will yield; rules that generate the fewest
wenzelm@42928
  1347
  subgoals will be tried first.  Otherwise, later declarations take
wenzelm@42928
  1348
  precedence over earlier ones.
wenzelm@42928
  1349
wenzelm@42928
  1350
  Rules already present in the context with the same classification
wenzelm@42928
  1351
  are ignored.  A warning is printed if the rule has already been
wenzelm@42928
  1352
  added with some other classification, but the rule is added anyway
wenzelm@42928
  1353
  as requested.
wenzelm@42928
  1354
wenzelm@61493
  1355
  \<^descr> @{attribute rule}~\<open>del\<close> deletes all occurrences of a
wenzelm@42928
  1356
  rule from the classical context, regardless of its classification as
wenzelm@42928
  1357
  introduction~/ elimination~/ destruction and safe~/ unsafe.
wenzelm@42928
  1358
wenzelm@61439
  1359
  \<^descr> @{attribute iff} declares logical equivalences to the
wenzelm@42928
  1360
  Simplifier and the Classical reasoner at the same time.
wenzelm@42928
  1361
  Non-conditional rules result in a safe introduction and elimination
wenzelm@42928
  1362
  pair; conditional ones are considered unsafe.  Rules with negative
wenzelm@61493
  1363
  conclusion are automatically inverted (using \<open>\<not>\<close>-elimination
wenzelm@42928
  1364
  internally).
wenzelm@42928
  1365
wenzelm@61493
  1366
  The ``\<open>?\<close>'' version of @{attribute iff} declares rules to
wenzelm@42928
  1367
  the Isabelle/Pure context only, and omits the Simplifier
wenzelm@42928
  1368
  declaration.
wenzelm@42928
  1369
wenzelm@61439
  1370
  \<^descr> @{attribute swapped} turns an introduction rule into an
wenzelm@61493
  1371
  elimination, by resolving with the classical swap principle \<open>\<not> P \<Longrightarrow> (\<not> R \<Longrightarrow> P) \<Longrightarrow> R\<close> in the second position.  This is mainly for
wenzelm@42928
  1372
  illustrative purposes: the Classical Reasoner already swaps rules
wenzelm@42928
  1373
  internally as explained above.
wenzelm@58618
  1374
\<close>
wenzelm@26782
  1375
wenzelm@26782
  1376
wenzelm@58618
  1377
subsection \<open>Structured methods\<close>
wenzelm@43365
  1378
wenzelm@58618
  1379
text \<open>
wenzelm@43365
  1380
  \begin{matharray}{rcl}
wenzelm@61493
  1381
    @{method_def rule} & : & \<open>method\<close> \\
wenzelm@61493
  1382
    @{method_def contradiction} & : & \<open>method\<close> \\
wenzelm@43365
  1383
  \end{matharray}
wenzelm@43365
  1384
wenzelm@55112
  1385
  @{rail \<open>
wenzelm@62969
  1386
    @@{method rule} @{syntax thms}?
wenzelm@55112
  1387
  \<close>}
wenzelm@43365
  1388
wenzelm@61439
  1389
  \<^descr> @{method rule} as offered by the Classical Reasoner is a
wenzelm@43365
  1390
  refinement over the Pure one (see \secref{sec:pure-meth-att}).  Both
wenzelm@43365
  1391
  versions work the same, but the classical version observes the
wenzelm@43365
  1392
  classical rule context in addition to that of Isabelle/Pure.
wenzelm@43365
  1393
wenzelm@43365
  1394
  Common object logics (HOL, ZF, etc.) declare a rich collection of
wenzelm@43365
  1395
  classical rules (even if these would qualify as intuitionistic
wenzelm@43365
  1396
  ones), but only few declarations to the rule context of
wenzelm@43365
  1397
  Isabelle/Pure (\secref{sec:pure-meth-att}).
wenzelm@43365
  1398
wenzelm@61439
  1399
  \<^descr> @{method contradiction} solves some goal by contradiction,
wenzelm@61493
  1400
  deriving any result from both \<open>\<not> A\<close> and \<open>A\<close>.  Chained
wenzelm@43365
  1401
  facts, which are guaranteed to participate, may appear in either
wenzelm@43365
  1402
  order.
wenzelm@58618
  1403
\<close>
wenzelm@43365
  1404
wenzelm@43365
  1405
wenzelm@58618
  1406
subsection \<open>Fully automated methods\<close>
wenzelm@26782
  1407
wenzelm@58618
  1408
text \<open>
wenzelm@26782
  1409
  \begin{matharray}{rcl}
wenzelm@61493
  1410
    @{method_def blast} & : & \<open>method\<close> \\
wenzelm@61493
  1411
    @{method_def auto} & : & \<open>method\<close> \\
wenzelm@61493
  1412
    @{method_def force} & : & \<open>method\<close> \\
wenzelm@61493
  1413
    @{method_def fast} & : & \<open>method\<close> \\
wenzelm@61493
  1414
    @{method_def slow} & : & \<open>method\<close> \\
wenzelm@61493
  1415
    @{method_def best} & : & \<open>method\<close> \\
wenzelm@61493
  1416
    @{method_def fastforce} & : & \<open>method\<close> \\
wenzelm@61493
  1417
    @{method_def slowsimp} & : & \<open>method\<close> \\
wenzelm@61493
  1418
    @{method_def bestsimp} & : & \<open>method\<close> \\
wenzelm@61493
  1419
    @{method_def deepen} & : & \<open>method\<close> \\
wenzelm@26782
  1420
  \end{matharray}
wenzelm@26782
  1421
wenzelm@55112
  1422
  @{rail \<open>
wenzelm@42930
  1423
    @@{method blast} @{syntax nat}? (@{syntax clamod} * )
wenzelm@42930
  1424
    ;
wenzelm@42596
  1425
    @@{method auto} (@{syntax nat} @{syntax nat})? (@{syntax clasimpmod} * )
wenzelm@26782
  1426
    ;
wenzelm@42930
  1427
    @@{method force} (@{syntax clasimpmod} * )
wenzelm@42930
  1428
    ;
wenzelm@42930
  1429
    (@@{method fast} | @@{method slow} | @@{method best}) (@{syntax clamod} * )
wenzelm@26782
  1430
    ;
nipkow@44911
  1431
    (@@{method fastforce} | @@{method slowsimp} | @@{method bestsimp})
wenzelm@42930
  1432
      (@{syntax clasimpmod} * )
wenzelm@42930
  1433
    ;
wenzelm@43367
  1434
    @@{method deepen} (@{syntax nat} ?) (@{syntax clamod} * )
wenzelm@43367
  1435
    ;
wenzelm@42930
  1436
    @{syntax_def clamod}:
wenzelm@62969
  1437
      (('intro' | 'elim' | 'dest') ('!' | () | '?') | 'del') ':' @{syntax thms}
wenzelm@42930
  1438
    ;
wenzelm@42596
  1439
    @{syntax_def clasimpmod}: ('simp' (() | 'add' | 'del' | 'only') |
wenzelm@26782
  1440
      ('cong' | 'split') (() | 'add' | 'del') |
wenzelm@26782
  1441
      'iff' (((() | 'add') '?'?) | 'del') |
wenzelm@62969
  1442
      (('intro' | 'elim' | 'dest') ('!' | () | '?') | 'del')) ':' @{syntax thms}
wenzelm@55112
  1443
  \<close>}
wenzelm@26782
  1444
wenzelm@61439
  1445
  \<^descr> @{method blast} is a separate classical tableau prover that
wenzelm@42930
  1446
  uses the same classical rule declarations as explained before.
wenzelm@42930
  1447
wenzelm@42930
  1448
  Proof search is coded directly in ML using special data structures.
wenzelm@42930
  1449
  A successful proof is then reconstructed using regular Isabelle
wenzelm@42930
  1450
  inferences.  It is faster and more powerful than the other classical
wenzelm@42930
  1451
  reasoning tools, but has major limitations too.
wenzelm@42930
  1452
wenzelm@61459
  1453
    \<^item> It does not use the classical wrapper tacticals, such as the
wenzelm@61458
  1454
    integration with the Simplifier of @{method fastforce}.
wenzelm@42930
  1455
wenzelm@61459
  1456
    \<^item> It does not perform higher-order unification, as needed by the
wenzelm@61458
  1457
    rule @{thm [source=false] rangeI} in HOL.  There are often
wenzelm@61458
  1458
    alternatives to such rules, for example @{thm [source=false]
wenzelm@61458
  1459
    range_eqI}.
wenzelm@42930
  1460
wenzelm@61459
  1461
    \<^item> Function variables may only be applied to parameters of the
wenzelm@61458
  1462
    subgoal.  (This restriction arises because the prover does not use
wenzelm@61458
  1463
    higher-order unification.)  If other function variables are present
wenzelm@61458
  1464
    then the prover will fail with the message
wenzelm@61458
  1465
    @{verbatim [display] \<open>Function unknown's argument not a bound variable\<close>}
wenzelm@42930
  1466
wenzelm@61459
  1467
    \<^item> Its proof strategy is more general than @{method fast} but can
wenzelm@61458
  1468
    be slower.  If @{method blast} fails or seems to be running forever,
wenzelm@61458
  1469
    try @{method fast} and the other proof tools described below.
wenzelm@42930
  1470
wenzelm@42930
  1471
  The optional integer argument specifies a bound for the number of
wenzelm@42930
  1472
  unsafe steps used in a proof.  By default, @{method blast} starts
wenzelm@42930
  1473
  with a bound of 0 and increases it successively to 20.  In contrast,
wenzelm@61493
  1474
  \<open>(blast lim)\<close> tries to prove the goal using a search bound
wenzelm@61493
  1475
  of \<open>lim\<close>.  Sometimes a slow proof using @{method blast} can
wenzelm@42930
  1476
  be made much faster by supplying the successful search bound to this
wenzelm@42930
  1477
  proof method instead.
wenzelm@42930
  1478
wenzelm@61439
  1479
  \<^descr> @{method auto} combines classical reasoning with
wenzelm@42930
  1480
  simplification.  It is intended for situations where there are a lot
wenzelm@42930
  1481
  of mostly trivial subgoals; it proves all the easy ones, leaving the
wenzelm@42930
  1482
  ones it cannot prove.  Occasionally, attempting to prove the hard
wenzelm@42930
  1483
  ones may take a long time.
wenzelm@42930
  1484
wenzelm@61493
  1485
  The optional depth arguments in \<open>(auto m n)\<close> refer to its
wenzelm@61493
  1486
  builtin classical reasoning procedures: \<open>m\<close> (default 4) is for
wenzelm@61493
  1487
  @{method blast}, which is tried first, and \<open>n\<close> (default 2) is
wenzelm@43332
  1488
  for a slower but more general alternative that also takes wrappers
wenzelm@43332
  1489
  into account.
wenzelm@42930
  1490
wenzelm@61439
  1491
  \<^descr> @{method force} is intended to prove the first subgoal
wenzelm@42930
  1492
  completely, using many fancy proof tools and performing a rather
wenzelm@42930
  1493
  exhaustive search.  As a result, proof attempts may take rather long
wenzelm@42930
  1494
  or diverge easily.
wenzelm@42930
  1495
wenzelm@61439
  1496
  \<^descr> @{method fast}, @{method best}, @{method slow} attempt to
wenzelm@42930
  1497
  prove the first subgoal using sequent-style reasoning as explained
wenzelm@42930
  1498
  before.  Unlike @{method blast}, they construct proofs directly in
wenzelm@42930
  1499
  Isabelle.
wenzelm@26782
  1500
wenzelm@42930
  1501
  There is a difference in search strategy and back-tracking: @{method
wenzelm@42930
  1502
  fast} uses depth-first search and @{method best} uses best-first
wenzelm@42930
  1503
  search (guided by a heuristic function: normally the total size of
wenzelm@42930
  1504
  the proof state).
wenzelm@42930
  1505
wenzelm@42930
  1506
  Method @{method slow} is like @{method fast}, but conducts a broader
wenzelm@42930
  1507
  search: it may, when backtracking from a failed proof attempt, undo
wenzelm@42930
  1508
  even the step of proving a subgoal by assumption.
wenzelm@42930
  1509
wenzelm@61439
  1510
  \<^descr> @{method fastforce}, @{method slowsimp}, @{method bestsimp}
wenzelm@47967
  1511
  are like @{method fast}, @{method slow}, @{method best},
wenzelm@47967
  1512
  respectively, but use the Simplifier as additional wrapper. The name
wenzelm@47967
  1513
  @{method fastforce}, reflects the behaviour of this popular method
wenzelm@47967
  1514
  better without requiring an understanding of its implementation.
wenzelm@42930
  1515
wenzelm@61439
  1516
  \<^descr> @{method deepen} works by exhaustive search up to a certain
wenzelm@43367
  1517
  depth.  The start depth is 4 (unless specified explicitly), and the
wenzelm@43367
  1518
  depth is increased iteratively up to 10.  Unsafe rules are modified
wenzelm@43367
  1519
  to preserve the formula they act on, so that it be used repeatedly.
wenzelm@43367
  1520
  This method can prove more goals than @{method fast}, but is much
wenzelm@43367
  1521
  slower, for example if the assumptions have many universal
wenzelm@43367
  1522
  quantifiers.
wenzelm@43367
  1523
wenzelm@42930
  1524
wenzelm@42930
  1525
  Any of the above methods support additional modifiers of the context
wenzelm@42930
  1526
  of classical (and simplifier) rules, but the ones related to the
wenzelm@61493
  1527
  Simplifier are explicitly prefixed by \<open>simp\<close> here.  The
wenzelm@42930
  1528
  semantics of these ad-hoc rule declarations is analogous to the
wenzelm@42930
  1529
  attributes given before.  Facts provided by forward chaining are
wenzelm@42930
  1530
  inserted into the goal before commencing proof search.
wenzelm@58618
  1531
\<close>
wenzelm@42930
  1532
wenzelm@42930
  1533
wenzelm@58618
  1534
subsection \<open>Partially automated methods\<close>
wenzelm@42930
  1535
wenzelm@58618
  1536
text \<open>These proof methods may help in situations when the
wenzelm@42930
  1537
  fully-automated tools fail.  The result is a simpler subgoal that
wenzelm@42930
  1538
  can be tackled by other means, such as by manual instantiation of
wenzelm@42930
  1539
  quantifiers.
wenzelm@42930
  1540
wenzelm@42930
  1541
  \begin{matharray}{rcl}
wenzelm@61493
  1542
    @{method_def safe} & : & \<open>method\<close> \\
wenzelm@61493
  1543
    @{method_def clarify} & : & \<open>method\<close> \\
wenzelm@61493
  1544
    @{method_def clarsimp} & : & \<open>method\<close> \\
wenzelm@42930
  1545
  \end{matharray}
wenzelm@42930
  1546
wenzelm@55112
  1547
  @{rail \<open>
wenzelm@42930
  1548
    (@@{method safe} | @@{method clarify}) (@{syntax clamod} * )
wenzelm@42930
  1549
    ;
wenzelm@42930
  1550
    @@{method clarsimp} (@{syntax clasimpmod} * )
wenzelm@55112
  1551
  \<close>}
wenzelm@42930
  1552
wenzelm@61439
  1553
  \<^descr> @{method safe} repeatedly performs safe steps on all subgoals.
wenzelm@42930
  1554
  It is deterministic, with at most one outcome.
wenzelm@42930
  1555
wenzelm@61439
  1556
  \<^descr> @{method clarify} performs a series of safe steps without
wenzelm@50108
  1557
  splitting subgoals; see also @{method clarify_step}.
wenzelm@42930
  1558
wenzelm@61439
  1559
  \<^descr> @{method clarsimp} acts like @{method clarify}, but also does
wenzelm@42930
  1560
  simplification.  Note that if the Simplifier context includes a
wenzelm@42930
  1561
  splitter for the premises, the subgoal may still be split.
wenzelm@58618
  1562
\<close>
wenzelm@26782
  1563
wenzelm@26782
  1564
wenzelm@58618
  1565
subsection \<open>Single-step tactics\<close>
wenzelm@43366
  1566
wenzelm@58618
  1567
text \<open>
wenzelm@50108
  1568
  \begin{matharray}{rcl}
wenzelm@61493
  1569
    @{method_def safe_step} & : & \<open>method\<close> \\
wenzelm@61493
  1570
    @{method_def inst_step} & : & \<open>method\<close> \\
wenzelm@61493
  1571
    @{method_def step} & : & \<open>method\<close> \\
wenzelm@61493
  1572
    @{method_def slow_step} & : & \<open>method\<close> \\
wenzelm@61493
  1573
    @{method_def clarify_step} & : &  \<open>method\<close> \\
wenzelm@50108
  1574
  \end{matharray}
wenzelm@43366
  1575
wenzelm@50070
  1576
  These are the primitive tactics behind the automated proof methods
wenzelm@50070
  1577
  of the Classical Reasoner.  By calling them yourself, you can
wenzelm@50070
  1578
  execute these procedures one step at a time.
wenzelm@43366
  1579
wenzelm@61439
  1580
  \<^descr> @{method safe_step} performs a safe step on the first subgoal.
wenzelm@50108
  1581
  The safe wrapper tacticals are applied to a tactic that may include
wenzelm@50108
  1582
  proof by assumption or Modus Ponens (taking care not to instantiate
wenzelm@50108
  1583
  unknowns), or substitution.
wenzelm@43366
  1584
wenzelm@61439
  1585
  \<^descr> @{method inst_step} is like @{method safe_step}, but allows
wenzelm@43366
  1586
  unknowns to be instantiated.
wenzelm@43366
  1587
wenzelm@61439
  1588
  \<^descr> @{method step} is the basic step of the proof procedure, it
wenzelm@50108
  1589
  operates on the first subgoal.  The unsafe wrapper tacticals are
wenzelm@50108
  1590
  applied to a tactic that tries @{method safe}, @{method inst_step},
wenzelm@50108
  1591
  or applies an unsafe rule from the context.
wenzelm@43366
  1592
wenzelm@61439
  1593
  \<^descr> @{method slow_step} resembles @{method step}, but allows
wenzelm@50108
  1594
  backtracking between using safe rules with instantiation (@{method
wenzelm@50108
  1595
  inst_step}) and using unsafe rules.  The resulting search space is
wenzelm@50108
  1596
  larger.
wenzelm@43366
  1597
wenzelm@61439
  1598
  \<^descr> @{method clarify_step} performs a safe step on the first
wenzelm@50108
  1599
  subgoal; no splitting step is applied.  For example, the subgoal
wenzelm@61493
  1600
  \<open>A \<and> B\<close> is left as a conjunction.  Proof by assumption,
wenzelm@50108
  1601
  Modus Ponens, etc., may be performed provided they do not
wenzelm@61493
  1602
  instantiate unknowns.  Assumptions of the form \<open>x = t\<close> may
wenzelm@50108
  1603
  be eliminated.  The safe wrapper tactical is applied.
wenzelm@58618
  1604
\<close>
wenzelm@43366
  1605
wenzelm@43366
  1606
wenzelm@58618
  1607
subsection \<open>Modifying the search step\<close>
wenzelm@50071
  1608
wenzelm@58618
  1609
text \<open>
wenzelm@50071
  1610
  \begin{mldecls}
wenzelm@50071
  1611
    @{index_ML_type wrapper: "(int -> tactic) -> (int -> tactic)"} \\[0.5ex]
wenzelm@51703
  1612
    @{index_ML_op addSWrapper: "Proof.context *
wenzelm@51703
  1613
  (string * (Proof.context -> wrapper)) -> Proof.context"} \\
wenzelm@51703
  1614
    @{index_ML_op addSbefore: "Proof.context *
wenzelm@51717
  1615
  (string * (Proof.context -> int -> tactic)) -> Proof.context"} \\
wenzelm@51703
  1616
    @{index_ML_op addSafter: "Proof.context *
wenzelm@51717
  1617
  (string * (Proof.context -> int -> tactic)) -> Proof.context"} \\
wenzelm@51703
  1618
    @{index_ML_op delSWrapper: "Proof.context * string -> Proof.context"} \\[0.5ex]
wenzelm@51703
  1619
    @{index_ML_op addWrapper: "Proof.context *
wenzelm@51703
  1620
  (string * (Proof.context -> wrapper)) -> Proof.context"} \\
wenzelm@51703
  1621
    @{index_ML_op addbefore: "Proof.context *
wenzelm@51717
  1622
  (string * (Proof.context -> int -> tactic)) -> Proof.context"} \\
wenzelm@51703
  1623
    @{index_ML_op addafter: "Proof.context *
wenzelm@51717
  1624
  (string * (Proof.context -> int -> tactic)) -> Proof.context"} \\
wenzelm@51703
  1625
    @{index_ML_op delWrapper: "Proof.context * string -> Proof.context"} \\[0.5ex]
wenzelm@50071
  1626
    @{index_ML addSss: "Proof.context -> Proof.context"} \\
wenzelm@50071
  1627
    @{index_ML addss: "Proof.context -> Proof.context"} \\
wenzelm@50071
  1628
  \end{mldecls}
wenzelm@50071
  1629
wenzelm@50071
  1630
  The proof strategy of the Classical Reasoner is simple.  Perform as
wenzelm@50071
  1631
  many safe inferences as possible; or else, apply certain safe rules,
wenzelm@50071
  1632
  allowing instantiation of unknowns; or else, apply an unsafe rule.
wenzelm@61493
  1633
  The tactics also eliminate assumptions of the form \<open>x = t\<close>
wenzelm@50071
  1634
  by substitution if they have been set up to do so.  They may perform
wenzelm@61493
  1635
  a form of Modus Ponens: if there are assumptions \<open>P \<longrightarrow> Q\<close> and
wenzelm@61493
  1636
  \<open>P\<close>, then replace \<open>P \<longrightarrow> Q\<close> by \<open>Q\<close>.
wenzelm@50071
  1637
wenzelm@50071
  1638
  The classical reasoning tools --- except @{method blast} --- allow
wenzelm@50071
  1639
  to modify this basic proof strategy by applying two lists of
wenzelm@61477
  1640
  arbitrary \<^emph>\<open>wrapper tacticals\<close> to it.  The first wrapper list,
wenzelm@50108
  1641
  which is considered to contain safe wrappers only, affects @{method
wenzelm@50108
  1642
  safe_step} and all the tactics that call it.  The second one, which
wenzelm@50108
  1643
  may contain unsafe wrappers, affects the unsafe parts of @{method
wenzelm@50108
  1644
  step}, @{method slow_step}, and the tactics that call them.  A
wenzelm@50071
  1645
  wrapper transforms each step of the search, for example by
wenzelm@50071
  1646
  attempting other tactics before or after the original step tactic.
wenzelm@50071
  1647
  All members of a wrapper list are applied in turn to the respective
wenzelm@50071
  1648
  step tactic.
wenzelm@50071
  1649
wenzelm@50071
  1650
  Initially the two wrapper lists are empty, which means no
wenzelm@50071
  1651
  modification of the step tactics. Safe and unsafe wrappers are added
wenzelm@59905
  1652
  to the context with the functions given below, supplying them with
wenzelm@50071
  1653
  wrapper names.  These names may be used to selectively delete
wenzelm@50071
  1654
  wrappers.
wenzelm@50071
  1655
wenzelm@61493
  1656
  \<^descr> \<open>ctxt addSWrapper (name, wrapper)\<close> adds a new wrapper,
wenzelm@50071
  1657
  which should yield a safe tactic, to modify the existing safe step
wenzelm@50071
  1658
  tactic.
wenzelm@50071
  1659
wenzelm@61493
  1660
  \<^descr> \<open>ctxt addSbefore (name, tac)\<close> adds the given tactic as a
wenzelm@61477
  1661
  safe wrapper, such that it is tried \<^emph>\<open>before\<close> each safe step of
wenzelm@50071
  1662
  the search.
wenzelm@50071
  1663
wenzelm@61493
  1664
  \<^descr> \<open>ctxt addSafter (name, tac)\<close> adds the given tactic as a
wenzelm@50071
  1665
  safe wrapper, such that it is tried when a safe step of the search
wenzelm@50071
  1666
  would fail.
wenzelm@50071
  1667
wenzelm@61493
  1668
  \<^descr> \<open>ctxt delSWrapper name\<close> deletes the safe wrapper with
wenzelm@50071
  1669
  the given name.
wenzelm@50071
  1670
wenzelm@61493
  1671
  \<^descr> \<open>ctxt addWrapper (name, wrapper)\<close> adds a new wrapper to
wenzelm@50071
  1672
  modify the existing (unsafe) step tactic.
wenzelm@50071
  1673
wenzelm@61493
  1674
  \<^descr> \<open>ctxt addbefore (name, tac)\<close> adds the given tactic as an
wenzelm@50071
  1675
  unsafe wrapper, such that it its result is concatenated
wenzelm@61477
  1676
  \<^emph>\<open>before\<close> the result of each unsafe step.
wenzelm@50071
  1677
wenzelm@61493
  1678
  \<^descr> \<open>ctxt addafter (name, tac)\<close> adds the given tactic as an
wenzelm@61477
  1679
  unsafe wrapper, such that it its result is concatenated \<^emph>\<open>after\<close>
wenzelm@50071
  1680
  the result of each unsafe step.
wenzelm@50071
  1681
wenzelm@61493
  1682
  \<^descr> \<open>ctxt delWrapper name\<close> deletes the unsafe wrapper with
wenzelm@50071
  1683
  the given name.
wenzelm@50071
  1684
wenzelm@61493
  1685
  \<^descr> \<open>addSss\<close> adds the simpset of the context to its
wenzelm@50071
  1686
  classical set. The assumptions and goal will be simplified, in a
wenzelm@50071
  1687
  rather safe way, after each safe step of the search.
wenzelm@50071
  1688
wenzelm@61493
  1689
  \<^descr> \<open>addss\<close> adds the simpset of the context to its
wenzelm@50071
  1690
  classical set. The assumptions and goal will be simplified, before
wenzelm@50071
  1691
  the each unsafe step of the search.
wenzelm@58618
  1692
\<close>
wenzelm@50071
  1693
wenzelm@50071
  1694
wenzelm@58618
  1695
section \<open>Object-logic setup \label{sec:object-logic}\<close>
wenzelm@26790
  1696
wenzelm@58618
  1697
text \<open>
wenzelm@26790
  1698
  \begin{matharray}{rcl}
wenzelm@61493
  1699
    @{command_def "judgment"} & : & \<open>theory \<rightarrow> theory\<close> \\
wenzelm@61493
  1700
    @{method_def atomize} & : & \<open>method\<close> \\
wenzelm@61493
  1701
    @{attribute_def atomize} & : & \<open>attribute\<close> \\
wenzelm@61493
  1702
    @{attribute_def rule_format} & : & \<open>attribute\<close> \\
wenzelm@61493
  1703
    @{attribute_def rulify} & : & \<open>attribute\<close> \\
wenzelm@26790
  1704
  \end{matharray}
wenzelm@26790
  1705
wenzelm@26790
  1706
  The very starting point for any Isabelle object-logic is a ``truth
wenzelm@26790
  1707
  judgment'' that links object-level statements to the meta-logic
wenzelm@61493
  1708
  (with its minimal language of \<open>prop\<close> that covers universal
wenzelm@61493
  1709
  quantification \<open>\<And>\<close> and implication \<open>\<Longrightarrow>\<close>).
wenzelm@26790
  1710
wenzelm@26790
  1711
  Common object-logics are sufficiently expressive to internalize rule
wenzelm@61493
  1712
  statements over \<open>\<And>\<close> and \<open>\<Longrightarrow>\<close> within their own
wenzelm@26790
  1713
  language.  This is useful in certain situations where a rule needs
wenzelm@26790
  1714
  to be viewed as an atomic statement from the meta-level perspective,
wenzelm@61493
  1715
  e.g.\ \<open>\<And>x. x \<in> A \<Longrightarrow> P x\<close> versus \<open>\<forall>x \<in> A. P x\<close>.
wenzelm@26790
  1716
wenzelm@26790
  1717
  From the following language elements, only the @{method atomize}
wenzelm@26790
  1718
  method and @{attribute rule_format} attribute are occasionally
wenzelm@26790
  1719
  required by end-users, the rest is for those who need to setup their
wenzelm@26790
  1720
  own object-logic.  In the latter case existing formulations of
wenzelm@26790
  1721
  Isabelle/FOL or Isabelle/HOL may be taken as realistic examples.
wenzelm@26790
  1722
wenzelm@26790
  1723
  Generic tools may refer to the information provided by object-logic
wenzelm@26790
  1724
  declarations internally.
wenzelm@26790
  1725
wenzelm@55112
  1726
  @{rail \<open>
wenzelm@46494
  1727
    @@{command judgment} @{syntax name} '::' @{syntax type} @{syntax mixfix}?
wenzelm@26790
  1728
    ;
wenzelm@42596
  1729
    @@{attribute atomize} ('(' 'full' ')')?
wenzelm@26790
  1730
    ;
wenzelm@42596
  1731
    @@{attribute rule_format} ('(' 'noasm' ')')?
wenzelm@55112
  1732
  \<close>}
wenzelm@26790
  1733
wenzelm@61493
  1734
  \<^descr> @{command "judgment"}~\<open>c :: \<sigma> (mx)\<close> declares constant
wenzelm@61493
  1735
  \<open>c\<close> as the truth judgment of the current object-logic.  Its
wenzelm@61493
  1736
  type \<open>\<sigma>\<close> should specify a coercion of the category of
wenzelm@61493
  1737
  object-level propositions to \<open>prop\<close> of the Pure meta-logic;
wenzelm@61493
  1738
  the mixfix annotation \<open>(mx)\<close> would typically just link the
wenzelm@61493
  1739
  object language (internally of syntactic category \<open>logic\<close>)
wenzelm@61493
  1740
  with that of \<open>prop\<close>.  Only one @{command "judgment"}
wenzelm@28760
  1741
  declaration may be given in any theory development.
wenzelm@26790
  1742
  
wenzelm@61439
  1743
  \<^descr> @{method atomize} (as a method) rewrites any non-atomic
wenzelm@26790
  1744
  premises of a sub-goal, using the meta-level equations declared via
wenzelm@26790
  1745
  @{attribute atomize} (as an attribute) beforehand.  As a result,
wenzelm@26790
  1746
  heavily nested goals become amenable to fundamental operations such
wenzelm@61493
  1747
  as resolution (cf.\ the @{method (Pure) rule} method).  Giving the ``\<open>(full)\<close>'' option here means to turn the whole subgoal into an
wenzelm@26790
  1748
  object-statement (if possible), including the outermost parameters
wenzelm@26790
  1749
  and assumptions as well.
wenzelm@26790
  1750
wenzelm@26790
  1751
  A typical collection of @{attribute atomize} rules for a particular
wenzelm@26790
  1752
  object-logic would provide an internalization for each of the
wenzelm@61493
  1753
  connectives of \<open>\<And>\<close>, \<open>\<Longrightarrow>\<close>, and \<open>\<equiv>\<close>.
wenzelm@26790
  1754
  Meta-level conjunction should be covered as well (this is
wenzelm@26790
  1755
  particularly important for locales, see \secref{sec:locale}).
wenzelm@26790
  1756
wenzelm@61439
  1757
  \<^descr> @{attribute rule_format} rewrites a theorem by the equalities
wenzelm@28760
  1758
  declared as @{attribute rulify} rules in the current object-logic.
wenzelm@28760
  1759
  By default, the result is fully normalized, including assumptions
wenzelm@61493
  1760
  and conclusions at any depth.  The \<open>(no_asm)\<close> option
wenzelm@28760
  1761
  restricts the transformation to the conclusion of a rule.
wenzelm@26790
  1762
wenzelm@26790
  1763
  In common object-logics (HOL, FOL, ZF), the effect of @{attribute
wenzelm@26790
  1764
  rule_format} is to replace (bounded) universal quantification
wenzelm@61493
  1765
  (\<open>\<forall>\<close>) and implication (\<open>\<longrightarrow>\<close>) by the corresponding
wenzelm@61493
  1766
  rule statements over \<open>\<And>\<close> and \<open>\<Longrightarrow>\<close>.
wenzelm@58618
  1767
\<close>
wenzelm@26790
  1768
wenzelm@50083
  1769
wenzelm@58618
  1770
section \<open>Tracing higher-order unification\<close>
wenzelm@50083
  1771
wenzelm@58618
  1772
text \<open>
wenzelm@50083
  1773
  \begin{tabular}{rcll}
wenzelm@61493
  1774
    @{attribute_def unify_trace_simp} & : & \<open>attribute\<close> & default \<open>false\<close> \\
wenzelm@61493
  1775
    @{attribute_def unify_trace_types} & : & \<open>attribute\<close> & default \<open>false\<close> \\
wenzelm@61493
  1776
    @{attribute_def unify_trace_bound} & : & \<open>attribute\<close> & default \<open>50\<close> \\
wenzelm@61493
  1777
    @{attribute_def unify_search_bound} & : & \<open>attribute\<close> & default \<open>60\<close> \\
wenzelm@50083
  1778
  \end{tabular}
wenzelm@61421
  1779
  \<^medskip>
wenzelm@50083
  1780
wenzelm@50083
  1781
  Higher-order unification works well in most practical situations,
wenzelm@50083
  1782
  but sometimes needs extra care to identify problems.  These tracing
wenzelm@50083
  1783
  options may help.
wenzelm@50083
  1784
wenzelm@61439
  1785
  \<^descr> @{attribute unify_trace_simp} controls tracing of the
wenzelm@50083
  1786
  simplification phase of higher-order unification.
wenzelm@50083
  1787
wenzelm@61439
  1788
  \<^descr> @{attribute unify_trace_types} controls warnings of
wenzelm@50083
  1789
  incompleteness, when unification is not considering all possible
wenzelm@50083
  1790
  instantiations of schematic type variables.
wenzelm@50083
  1791
wenzelm@61439
  1792
  \<^descr> @{attribute unify_trace_bound} determines the depth where
wenzelm@50083
  1793
  unification starts to print tracing information once it reaches
wenzelm@50083
  1794
  depth; 0 for full tracing.  At the default value, tracing
wenzelm@50083
  1795
  information is almost never printed in practice.
wenzelm@50083
  1796
wenzelm@61439
  1797
  \<^descr> @{attribute unify_search_bound} prevents unification from
wenzelm@50083
  1798
  searching past the given depth.  Because of this bound, higher-order
wenzelm@50083
  1799
  unification cannot return an infinite sequence, though it can return
wenzelm@50083
  1800
  an exponentially long one.  The search rarely approaches the default
wenzelm@50083
  1801
  value in practice.  If the search is cut off, unification prints a
wenzelm@50083
  1802
  warning ``Unification bound exceeded''.
wenzelm@50083
  1803
wenzelm@50083
  1804
wenzelm@50083
  1805
  \begin{warn}
wenzelm@50083
  1806
  Options for unification cannot be modified in a local context.  Only
wenzelm@50083
  1807
  the global theory content is taken into account.
wenzelm@50083
  1808
  \end{warn}
wenzelm@58618
  1809
\<close>
wenzelm@50083
  1810
wenzelm@26782
  1811
end