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