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