doc-src/Ref/simplifier.tex
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 15:03:34 +0200
branchisac-from-Isabelle2009-2
changeset 37913 20e3616b2d9c
parent 30184 37969710e61f
child 43760 e87888b4152f
permissions -rw-r--r--
prepare reactivation of isac-update-Isa09-2
wenzelm@30184
     1
nipkow@3950
     2
\chapter{Simplification}
nipkow@3950
     3
\label{chap:simplification}
lcp@104
     4
\index{simplification|(}
lcp@104
     5
wenzelm@9695
     6
This chapter describes Isabelle's generic simplification package.  It performs
wenzelm@9695
     7
conditional and unconditional rewriting and uses contextual information
wenzelm@9695
     8
(`local assumptions').  It provides several general hooks, which can provide
wenzelm@9695
     9
automatic case splits during rewriting, for example.  The simplifier is
wenzelm@9695
    10
already set up for many of Isabelle's logics: FOL, ZF, HOL, HOLCF.
nipkow@1860
    11
wenzelm@4395
    12
The first section is a quick introduction to the simplifier that
wenzelm@4395
    13
should be sufficient to get started.  The later sections explain more
wenzelm@4395
    14
advanced features.
wenzelm@4395
    15
nipkow@1860
    16
nipkow@1860
    17
\section{Simplification for dummies}
nipkow@1860
    18
\label{sec:simp-for-dummies}
nipkow@1860
    19
wenzelm@4395
    20
Basic use of the simplifier is particularly easy because each theory
wenzelm@4557
    21
is equipped with sensible default information controlling the rewrite
wenzelm@4557
    22
process --- namely the implicit {\em current
wenzelm@4557
    23
  simpset}\index{simpset!current}.  A suite of simple commands is
wenzelm@4557
    24
provided that refer to the implicit simpset of the current theory
wenzelm@4557
    25
context.
wenzelm@4395
    26
wenzelm@4395
    27
\begin{warn}
wenzelm@4395
    28
  Make sure that you are working within the correct theory context.
wenzelm@4395
    29
  Executing proofs interactively, or loading them from ML files
wenzelm@4395
    30
  without associated theories may require setting the current theory
wenzelm@4395
    31
  manually via the \ttindex{context} command.
wenzelm@4395
    32
\end{warn}
wenzelm@4395
    33
wenzelm@4395
    34
\subsection{Simplification tactics} \label{sec:simp-for-dummies-tacs}
nipkow@1860
    35
\begin{ttbox}
wenzelm@4395
    36
Simp_tac          : int -> tactic
wenzelm@4395
    37
Asm_simp_tac      : int -> tactic
wenzelm@4395
    38
Full_simp_tac     : int -> tactic
wenzelm@4395
    39
Asm_full_simp_tac : int -> tactic
wenzelm@4395
    40
trace_simp        : bool ref \hfill{\bf initially false}
wenzelm@7920
    41
debug_simp        : bool ref \hfill{\bf initially false}
wenzelm@4395
    42
\end{ttbox}
wenzelm@4395
    43
wenzelm@4395
    44
\begin{ttdescription}
wenzelm@4395
    45
\item[\ttindexbold{Simp_tac} $i$] simplifies subgoal~$i$ using the
wenzelm@4395
    46
  current simpset.  It may solve the subgoal completely if it has
wenzelm@4395
    47
  become trivial, using the simpset's solver tactic.
wenzelm@4395
    48
  
wenzelm@4395
    49
\item[\ttindexbold{Asm_simp_tac}]\index{assumptions!in simplification}
wenzelm@4395
    50
  is like \verb$Simp_tac$, but extracts additional rewrite rules from
wenzelm@4395
    51
  the local assumptions.
wenzelm@4395
    52
  
wenzelm@4395
    53
\item[\ttindexbold{Full_simp_tac}] is like \verb$Simp_tac$, but also
wenzelm@4395
    54
  simplifies the assumptions (without using the assumptions to
wenzelm@4395
    55
  simplify each other or the actual goal).
wenzelm@4395
    56
  
wenzelm@4395
    57
\item[\ttindexbold{Asm_full_simp_tac}] is like \verb$Asm_simp_tac$,
nipkow@4889
    58
  but also simplifies the assumptions. In particular, assumptions can
nipkow@4889
    59
  simplify each other.
nipkow@4889
    60
\footnote{\texttt{Asm_full_simp_tac} used to process the assumptions from
nipkow@4889
    61
  left to right. For backwards compatibilty reasons only there is now
nipkow@4889
    62
  \texttt{Asm_lr_simp_tac} that behaves like the old \texttt{Asm_full_simp_tac}.}
wenzelm@7920
    63
\item[set \ttindexbold{trace_simp};] makes the simplifier output internal
wenzelm@7920
    64
  operations.  This includes rewrite steps, but also bookkeeping like
wenzelm@7920
    65
  modifications of the simpset.
wenzelm@7920
    66
\item[set \ttindexbold{debug_simp};] makes the simplifier output some extra
wenzelm@7920
    67
  information about internal operations.  This includes any attempted
wenzelm@7920
    68
  invocation of simplification procedures.
wenzelm@4395
    69
\end{ttdescription}
wenzelm@4395
    70
wenzelm@4395
    71
\medskip
wenzelm@4395
    72
wenzelm@9695
    73
As an example, consider the theory of arithmetic in HOL.  The (rather trivial)
wenzelm@9695
    74
goal $0 + (x + 0) = x + 0 + 0$ can be solved by a single call of
wenzelm@9695
    75
\texttt{Simp_tac} as follows:
wenzelm@4395
    76
\begin{ttbox}
wenzelm@4395
    77
context Arith.thy;
paulson@5205
    78
Goal "0 + (x + 0) = x + 0 + 0";
nipkow@1860
    79
{\out  1. 0 + (x + 0) = x + 0 + 0}
wenzelm@4395
    80
by (Simp_tac 1);
wenzelm@4395
    81
{\out Level 1}
wenzelm@4395
    82
{\out 0 + (x + 0) = x + 0 + 0}
wenzelm@4395
    83
{\out No subgoals!}
nipkow@1860
    84
\end{ttbox}
wenzelm@4395
    85
wenzelm@4395
    86
The simplifier uses the current simpset of \texttt{Arith.thy}, which
wenzelm@4395
    87
contains suitable theorems like $\Var{n}+0 = \Var{n}$ and $0+\Var{n} =
nipkow@1860
    88
\Var{n}$.
nipkow@1860
    89
wenzelm@4395
    90
\medskip In many cases, assumptions of a subgoal are also needed in
wenzelm@4395
    91
the simplification process.  For example, \texttt{x = 0 ==> x + x = 0}
wenzelm@4395
    92
is solved by \texttt{Asm_simp_tac} as follows:
nipkow@1860
    93
\begin{ttbox}
nipkow@1860
    94
{\out  1. x = 0 ==> x + x = 0}
paulson@2479
    95
by (Asm_simp_tac 1);
nipkow@1860
    96
\end{ttbox}
nipkow@1860
    97
paulson@4597
    98
\medskip \texttt{Asm_full_simp_tac} is the most powerful of this quartet
wenzelm@4395
    99
of tactics but may also loop where some of the others terminate.  For
wenzelm@4395
   100
example,
nipkow@1860
   101
\begin{ttbox}
wenzelm@4395
   102
{\out  1. ALL x. f x = g (f (g x)) ==> f 0 = f 0 + 0}
nipkow@1860
   103
\end{ttbox}
paulson@4597
   104
is solved by \texttt{Simp_tac}, but \texttt{Asm_simp_tac} and {\tt
berghofe@13616
   105
  Asm_full_simp_tac} loop because the rewrite rule $f\,\Var{x} =
wenzelm@4395
   106
g\,(f\,(g\,\Var{x}))$ extracted from the assumption does not
wenzelm@4395
   107
terminate.  Isabelle notices certain simple forms of nontermination,
nipkow@4889
   108
but not this one. Because assumptions may simplify each other, there can be
berghofe@13616
   109
very subtle cases of nontermination. For example, invoking
berghofe@13616
   110
{\tt Asm_full_simp_tac} on
nipkow@1860
   111
\begin{ttbox}
berghofe@13616
   112
{\out  1. [| P (f x); y = x; f x = f y |] ==> Q}
nipkow@1860
   113
\end{ttbox}
berghofe@13616
   114
gives rise to the infinite reduction sequence
berghofe@13616
   115
\[
berghofe@13693
   116
P\,(f\,x) \stackrel{f\,x = f\,y}{\longmapsto} P\,(f\,y) \stackrel{y = x}{\longmapsto}
berghofe@13693
   117
P\,(f\,x) \stackrel{f\,x = f\,y}{\longmapsto} \cdots
berghofe@13616
   118
\]
berghofe@13616
   119
whereas applying the same tactic to
berghofe@13616
   120
\begin{ttbox}
berghofe@13616
   121
{\out  1. [| y = x; f x = f y; P (f x) |] ==> Q}
berghofe@13616
   122
\end{ttbox}
berghofe@13616
   123
terminates.
nipkow@1860
   124
wenzelm@4395
   125
\medskip
wenzelm@4395
   126
wenzelm@3108
   127
Using the simplifier effectively may take a bit of experimentation.
wenzelm@4395
   128
Set the \verb$trace_simp$\index{tracing!of simplification} flag to get
wenzelm@4395
   129
a better idea of what is going on.  The resulting output can be
wenzelm@4395
   130
enormous, especially since invocations of the simplifier are often
wenzelm@4395
   131
nested (e.g.\ when solving conditions of rewrite rules).
nipkow@1860
   132
wenzelm@4395
   133
wenzelm@4395
   134
\subsection{Modifying the current simpset}
nipkow@1860
   135
\begin{ttbox}
wenzelm@4395
   136
Addsimps    : thm list -> unit
wenzelm@4395
   137
Delsimps    : thm list -> unit
wenzelm@4395
   138
Addsimprocs : simproc list -> unit
wenzelm@4395
   139
Delsimprocs : simproc list -> unit
wenzelm@4395
   140
Addcongs    : thm list -> unit
wenzelm@4395
   141
Delcongs    : thm list -> unit
oheimb@5549
   142
Addsplits   : thm list -> unit
oheimb@5549
   143
Delsplits   : thm list -> unit
wenzelm@4395
   144
\end{ttbox}
wenzelm@4395
   145
wenzelm@4395
   146
Depending on the theory context, the \texttt{Add} and \texttt{Del}
wenzelm@4395
   147
functions manipulate basic components of the associated current
wenzelm@4395
   148
simpset.  Internally, all rewrite rules have to be expressed as
wenzelm@4395
   149
(conditional) meta-equalities.  This form is derived automatically
wenzelm@4395
   150
from object-level equations that are supplied by the user.  Another
wenzelm@4395
   151
source of rewrite rules are \emph{simplification procedures}, that is
wenzelm@4395
   152
\ML\ functions that produce suitable theorems on demand, depending on
wenzelm@4395
   153
the current redex.  Congruences are a more advanced feature; see
oheimb@11181
   154
{\S}\ref{sec:simp-congs}.
wenzelm@4395
   155
wenzelm@4395
   156
\begin{ttdescription}
wenzelm@4395
   157
wenzelm@4395
   158
\item[\ttindexbold{Addsimps} $thms$;] adds rewrite rules derived from
wenzelm@4395
   159
  $thms$ to the current simpset.
wenzelm@4395
   160
  
wenzelm@4395
   161
\item[\ttindexbold{Delsimps} $thms$;] deletes rewrite rules derived
wenzelm@4395
   162
  from $thms$ from the current simpset.
wenzelm@4395
   163
  
wenzelm@4395
   164
\item[\ttindexbold{Addsimprocs} $procs$;] adds simplification
wenzelm@4395
   165
  procedures $procs$ to the current simpset.
wenzelm@4395
   166
  
wenzelm@4395
   167
\item[\ttindexbold{Delsimprocs} $procs$;] deletes simplification
wenzelm@4395
   168
  procedures $procs$ from the current simpset.
wenzelm@4395
   169
  
wenzelm@4395
   170
\item[\ttindexbold{Addcongs} $thms$;] adds congruence rules to the
wenzelm@4395
   171
  current simpset.
wenzelm@4395
   172
  
oheimb@5549
   173
\item[\ttindexbold{Delcongs} $thms$;] deletes congruence rules from the
oheimb@5549
   174
  current simpset.
oheimb@5549
   175
oheimb@5549
   176
\item[\ttindexbold{Addsplits} $thms$;] adds splitting rules to the
oheimb@5549
   177
  current simpset.
oheimb@5549
   178
  
oheimb@5549
   179
\item[\ttindexbold{Delsplits} $thms$;] deletes splitting rules from the
wenzelm@4395
   180
  current simpset.
wenzelm@4395
   181
wenzelm@4395
   182
\end{ttdescription}
wenzelm@4395
   183
wenzelm@9695
   184
When a new theory is built, its implicit simpset is initialized by the union
wenzelm@9695
   185
of the respective simpsets of its parent theories.  In addition, certain
wenzelm@9695
   186
theory definition constructs (e.g.\ \ttindex{datatype} and \ttindex{primrec}
wenzelm@9695
   187
in HOL) implicitly augment the current simpset.  Ordinary definitions are not
wenzelm@9695
   188
added automatically!
wenzelm@4395
   189
wenzelm@4395
   190
It is up the user to manipulate the current simpset further by
wenzelm@4395
   191
explicitly adding or deleting theorems and simplification procedures.
wenzelm@4395
   192
wenzelm@4395
   193
\medskip
wenzelm@4395
   194
paulson@5205
   195
Good simpsets are hard to design.  Rules that obviously simplify,
paulson@5205
   196
like $\Var{n}+0 = \Var{n}$, should be added to the current simpset right after
paulson@5205
   197
they have been proved.  More specific ones (such as distributive laws, which
paulson@5205
   198
duplicate subterms) should be added only for specific proofs and deleted
paulson@5205
   199
afterwards.  Conversely, sometimes a rule needs
paulson@5205
   200
to be removed for a certain proof and restored afterwards.  The need of
paulson@5205
   201
frequent additions or deletions may indicate a badly designed
paulson@5205
   202
simpset.
wenzelm@4395
   203
wenzelm@4395
   204
\begin{warn}
wenzelm@4395
   205
  The union of the parent simpsets (as described above) is not always
wenzelm@4395
   206
  a good starting point for the new theory.  If some ancestors have
wenzelm@4395
   207
  deleted simplification rules because they are no longer wanted,
wenzelm@4395
   208
  while others have left those rules in, then the union will contain
paulson@5205
   209
  the unwanted rules.  After this union is formed, changes to 
paulson@5205
   210
  a parent simpset have no effect on the child simpset.
wenzelm@4395
   211
\end{warn}
wenzelm@4395
   212
wenzelm@4395
   213
wenzelm@4395
   214
\section{Simplification sets}\index{simplification sets} 
wenzelm@4395
   215
wenzelm@4395
   216
The simplifier is controlled by information contained in {\bf
wenzelm@4395
   217
  simpsets}.  These consist of several components, including rewrite
wenzelm@4395
   218
rules, simplification procedures, congruence rules, and the subgoaler,
wenzelm@4395
   219
solver and looper tactics.  The simplifier should be set up with
wenzelm@4395
   220
sensible defaults so that most simplifier calls specify only rewrite
wenzelm@4395
   221
rules or simplification procedures.  Experienced users can exploit the
wenzelm@4395
   222
other components to streamline proofs in more sophisticated manners.
wenzelm@4395
   223
wenzelm@4395
   224
\subsection{Inspecting simpsets}
wenzelm@4395
   225
\begin{ttbox}
wenzelm@4395
   226
print_ss : simpset -> unit
nipkow@4889
   227
rep_ss   : simpset -> \{mss        : meta_simpset, 
oheimb@4664
   228
                       subgoal_tac: simpset  -> int -> tactic,
nipkow@7620
   229
                       loop_tacs  : (string * (int -> tactic))list,
nipkow@7620
   230
                       finish_tac : solver list,
nipkow@7620
   231
                unsafe_finish_tac : solver list\}
nipkow@1860
   232
\end{ttbox}
nipkow@1860
   233
\begin{ttdescription}
wenzelm@4395
   234
  
wenzelm@4395
   235
\item[\ttindexbold{print_ss} $ss$;] displays the printable contents of
wenzelm@4395
   236
  simpset $ss$.  This includes the rewrite rules and congruences in
wenzelm@4395
   237
  their internal form expressed as meta-equalities.  The names of the
wenzelm@4395
   238
  simplification procedures and the patterns they are invoked on are
wenzelm@4395
   239
  also shown.  The other parts, functions and tactics, are
wenzelm@4395
   240
  non-printable.
wenzelm@4395
   241
oheimb@4664
   242
\item[\ttindexbold{rep_ss} $ss$;] decomposes $ss$ as a record of its internal 
oheimb@4664
   243
  components, namely the meta_simpset, the subgoaler, the loop, and the safe
oheimb@4664
   244
  and unsafe solvers.
oheimb@4664
   245
nipkow@1860
   246
\end{ttdescription}
nipkow@1860
   247
nipkow@1860
   248
wenzelm@4395
   249
\subsection{Building simpsets}
wenzelm@4395
   250
\begin{ttbox}
wenzelm@4395
   251
empty_ss : simpset
wenzelm@4395
   252
merge_ss : simpset * simpset -> simpset
wenzelm@4395
   253
\end{ttbox}
wenzelm@4395
   254
\begin{ttdescription}
wenzelm@4395
   255
  
wenzelm@9695
   256
\item[\ttindexbold{empty_ss}] is the empty simpset.  This is not very useful
wenzelm@9695
   257
  under normal circumstances because it doesn't contain suitable tactics
wenzelm@9695
   258
  (subgoaler etc.).  When setting up the simplifier for a particular
wenzelm@9695
   259
  object-logic, one will typically define a more appropriate ``almost empty''
wenzelm@9695
   260
  simpset.  For example, in HOL this is called \ttindexbold{HOL_basic_ss}.
wenzelm@4395
   261
  
wenzelm@4395
   262
\item[\ttindexbold{merge_ss} ($ss@1$, $ss@2$)] merges simpsets $ss@1$
wenzelm@4395
   263
  and $ss@2$ by building the union of their respective rewrite rules,
wenzelm@4395
   264
  simplification procedures and congruences.  The other components
wenzelm@4557
   265
  (tactics etc.) cannot be merged, though; they are taken from either
wenzelm@4557
   266
  simpset\footnote{Actually from $ss@1$, but it would unwise to count
wenzelm@4557
   267
    on that.}.
paulson@2479
   268
wenzelm@4395
   269
\end{ttdescription}
lcp@104
   270
lcp@104
   271
wenzelm@4395
   272
\subsection{Accessing the current simpset}
oheimb@5575
   273
\label{sec:access-current-simpset}
lcp@323
   274
wenzelm@4395
   275
\begin{ttbox}
oheimb@5575
   276
simpset        : unit   -> simpset
oheimb@5575
   277
simpset_ref    : unit   -> simpset ref
wenzelm@4395
   278
simpset_of     : theory -> simpset
wenzelm@4395
   279
simpset_ref_of : theory -> simpset ref
wenzelm@4395
   280
print_simpset  : theory -> unit
oheimb@5575
   281
SIMPSET        :(simpset ->       tactic) ->       tactic
oheimb@5575
   282
SIMPSET'       :(simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4395
   283
\end{ttbox}
wenzelm@4395
   284
wenzelm@4395
   285
Each theory contains a current simpset\index{simpset!current} stored
wenzelm@4395
   286
within a private ML reference variable.  This can be retrieved and
wenzelm@4395
   287
modified as follows.
wenzelm@4395
   288
wenzelm@4395
   289
\begin{ttdescription}
wenzelm@4395
   290
  
wenzelm@4395
   291
\item[\ttindexbold{simpset}();] retrieves the simpset value from the
wenzelm@4395
   292
  current theory context.
wenzelm@4395
   293
  
wenzelm@4395
   294
\item[\ttindexbold{simpset_ref}();] retrieves the simpset reference
wenzelm@4395
   295
  variable from the current theory context.  This can be assigned to
wenzelm@4395
   296
  by using \texttt{:=} in ML.
wenzelm@4395
   297
  
wenzelm@4395
   298
\item[\ttindexbold{simpset_of} $thy$;] retrieves the simpset value
wenzelm@4395
   299
  from theory $thy$.
wenzelm@4395
   300
  
wenzelm@4395
   301
\item[\ttindexbold{simpset_ref_of} $thy$;] retrieves the simpset
wenzelm@4395
   302
  reference variable from theory $thy$.
wenzelm@4395
   303
oheimb@5575
   304
\item[\ttindexbold{print_simpset} $thy$;] prints the current simpset
oheimb@5575
   305
  of theory $thy$ in the same way as \texttt{print_ss}.
oheimb@5575
   306
wenzelm@5574
   307
\item[\ttindexbold{SIMPSET} $tacf$, \ttindexbold{SIMPSET'} $tacf'$]
wenzelm@5574
   308
  are tacticals that make a tactic depend on the implicit current
wenzelm@5574
   309
  simpset of the theory associated with the proof state they are
wenzelm@5574
   310
  applied on.
wenzelm@5574
   311
wenzelm@4395
   312
\end{ttdescription}
wenzelm@4395
   313
wenzelm@5574
   314
\begin{warn}
paulson@8136
   315
  There is a small difference between \texttt{(SIMPSET'~$tacf$)} and
paulson@8136
   316
  \texttt{($tacf\,$(simpset()))}.  For example \texttt{(SIMPSET'
wenzelm@5574
   317
    simp_tac)} would depend on the theory of the proof state it is
wenzelm@5574
   318
  applied to, while \texttt{(simp_tac (simpset()))} implicitly refers
wenzelm@5574
   319
  to the current theory context.  Both are usually the same in proof
wenzelm@5574
   320
  scripts, provided that goals are only stated within the current
wenzelm@5574
   321
  theory.  Robust programs would not count on that, of course.
wenzelm@5574
   322
\end{warn}
wenzelm@5574
   323
lcp@104
   324
lcp@332
   325
\subsection{Rewrite rules}
wenzelm@4395
   326
\begin{ttbox}
wenzelm@4395
   327
addsimps : simpset * thm list -> simpset \hfill{\bf infix 4}
wenzelm@4395
   328
delsimps : simpset * thm list -> simpset \hfill{\bf infix 4}
wenzelm@4395
   329
\end{ttbox}
wenzelm@4395
   330
wenzelm@4395
   331
\index{rewrite rules|(} Rewrite rules are theorems expressing some
wenzelm@4395
   332
form of equality, for example:
lcp@323
   333
\begin{eqnarray*}
lcp@323
   334
  Suc(\Var{m}) + \Var{n} &=&      \Var{m} + Suc(\Var{n}) \\
lcp@323
   335
  \Var{P}\conj\Var{P}    &\bimp&  \Var{P} \\
nipkow@714
   336
  \Var{A} \un \Var{B} &\equiv& \{x.x\in \Var{A} \disj x\in \Var{B}\}
lcp@323
   337
\end{eqnarray*}
nipkow@1860
   338
Conditional rewrites such as $\Var{m}<\Var{n} \Imp \Var{m}/\Var{n} =
wenzelm@4395
   339
0$ are also permitted; the conditions can be arbitrary formulas.
lcp@104
   340
wenzelm@4395
   341
Internally, all rewrite rules are translated into meta-equalities,
wenzelm@4395
   342
theorems with conclusion $lhs \equiv rhs$.  Each simpset contains a
wenzelm@4395
   343
function for extracting equalities from arbitrary theorems.  For
oheimb@11181
   344
example, $\neg(\Var{x}\in \{\})$ could be turned into $\Var{x}\in \{\}
wenzelm@4395
   345
\equiv False$.  This function can be installed using
wenzelm@4395
   346
\ttindex{setmksimps} but only the definer of a logic should need to do
oheimb@11181
   347
this; see {\S}\ref{sec:setmksimps}.  The function processes theorems
wenzelm@4395
   348
added by \texttt{addsimps} as well as local assumptions.
lcp@104
   349
wenzelm@4395
   350
\begin{ttdescription}
wenzelm@4395
   351
  
wenzelm@4395
   352
\item[$ss$ \ttindexbold{addsimps} $thms$] adds rewrite rules derived
wenzelm@4395
   353
  from $thms$ to the simpset $ss$.
wenzelm@4395
   354
  
wenzelm@4395
   355
\item[$ss$ \ttindexbold{delsimps} $thms$] deletes rewrite rules
wenzelm@4395
   356
  derived from $thms$ from the simpset $ss$.
wenzelm@4395
   357
wenzelm@4395
   358
\end{ttdescription}
lcp@104
   359
lcp@332
   360
\begin{warn}
wenzelm@4395
   361
  The simplifier will accept all standard rewrite rules: those where
wenzelm@4395
   362
  all unknowns are of base type.  Hence ${\Var{i}+(\Var{j}+\Var{k})} =
wenzelm@4395
   363
  {(\Var{i}+\Var{j})+\Var{k}}$ is OK.
wenzelm@4395
   364
  
wenzelm@4395
   365
  It will also deal gracefully with all rules whose left-hand sides
wenzelm@4395
   366
  are so-called {\em higher-order patterns}~\cite{nipkow-patterns}.
wenzelm@4395
   367
  \indexbold{higher-order pattern}\indexbold{pattern, higher-order}
wenzelm@4395
   368
  These are terms in $\beta$-normal form (this will always be the case
wenzelm@4395
   369
  unless you have done something strange) where each occurrence of an
wenzelm@4395
   370
  unknown is of the form $\Var{F}(x@1,\dots,x@n)$, where the $x@i$ are
wenzelm@4395
   371
  distinct bound variables. Hence $(\forall x.\Var{P}(x) \land
wenzelm@4395
   372
  \Var{Q}(x)) \bimp (\forall x.\Var{P}(x)) \land (\forall
wenzelm@4395
   373
  x.\Var{Q}(x))$ is also OK, in both directions.
wenzelm@4395
   374
  
wenzelm@4395
   375
  In some rare cases the rewriter will even deal with quite general
wenzelm@4395
   376
  rules: for example ${\Var{f}(\Var{x})\in range(\Var{f})} = True$
wenzelm@4395
   377
  rewrites $g(a) \in range(g)$ to $True$, but will fail to match
wenzelm@4395
   378
  $g(h(b)) \in range(\lambda x.g(h(x)))$.  However, you can replace
wenzelm@4395
   379
  the offending subterms (in our case $\Var{f}(\Var{x})$, which is not
wenzelm@4395
   380
  a pattern) by adding new variables and conditions: $\Var{y} =
wenzelm@4395
   381
  \Var{f}(\Var{x}) \Imp \Var{y}\in range(\Var{f}) = True$ is
wenzelm@4395
   382
  acceptable as a conditional rewrite rule since conditions can be
wenzelm@4395
   383
  arbitrary terms.
wenzelm@4395
   384
  
wenzelm@4395
   385
  There is basically no restriction on the form of the right-hand
wenzelm@4395
   386
  sides.  They may not contain extraneous term or type variables,
wenzelm@4395
   387
  though.
lcp@104
   388
\end{warn}
lcp@332
   389
\index{rewrite rules|)}
lcp@332
   390
wenzelm@4395
   391
nipkow@4947
   392
\subsection{*Simplification procedures}
wenzelm@4395
   393
\begin{ttbox}
wenzelm@4395
   394
addsimprocs : simpset * simproc list -> simpset
wenzelm@4395
   395
delsimprocs : simpset * simproc list -> simpset
wenzelm@4395
   396
\end{ttbox}
wenzelm@4395
   397
wenzelm@4557
   398
Simplification procedures are {\ML} objects of abstract type
wenzelm@4557
   399
\texttt{simproc}.  Basically they are just functions that may produce
wenzelm@4395
   400
\emph{proven} rewrite rules on demand.  They are associated with
wenzelm@4395
   401
certain patterns that conceptually represent left-hand sides of
wenzelm@4395
   402
equations; these are shown by \texttt{print_ss}.  During its
wenzelm@4395
   403
operation, the simplifier may offer a simplification procedure the
wenzelm@4395
   404
current redex and ask for a suitable rewrite rule.  Thus rules may be
wenzelm@4395
   405
specifically fashioned for particular situations, resulting in a more
wenzelm@4395
   406
powerful mechanism than term rewriting by a fixed set of rules.
wenzelm@4395
   407
wenzelm@4395
   408
wenzelm@4395
   409
\begin{ttdescription}
wenzelm@4395
   410
  
paulson@4597
   411
\item[$ss$ \ttindexbold{addsimprocs} $procs$] adds the simplification
wenzelm@4395
   412
  procedures $procs$ to the current simpset.
wenzelm@4395
   413
  
paulson@4597
   414
\item[$ss$ \ttindexbold{delsimprocs} $procs$] deletes the simplification
wenzelm@4395
   415
  procedures $procs$ from the current simpset.
wenzelm@4395
   416
wenzelm@4395
   417
\end{ttdescription}
wenzelm@4395
   418
wenzelm@4557
   419
For example, simplification procedures \ttindexbold{nat_cancel} of
wenzelm@4557
   420
\texttt{HOL/Arith} cancel common summands and constant factors out of
wenzelm@4557
   421
several relations of sums over natural numbers.
wenzelm@4557
   422
wenzelm@4557
   423
Consider the following goal, which after cancelling $a$ on both sides
wenzelm@4557
   424
contains a factor of $2$.  Simplifying with the simpset of
wenzelm@4557
   425
\texttt{Arith.thy} will do the cancellation automatically:
wenzelm@4557
   426
\begin{ttbox}
wenzelm@4557
   427
{\out 1. x + a + x < y + y + 2 + a + a + a + a + a}
wenzelm@4557
   428
by (Simp_tac 1);
wenzelm@4557
   429
{\out 1. x < Suc (a + (a + y))}
wenzelm@4557
   430
\end{ttbox}
wenzelm@4557
   431
wenzelm@4395
   432
wenzelm@4395
   433
\subsection{*Congruence rules}\index{congruence rules}\label{sec:simp-congs}
wenzelm@4395
   434
\begin{ttbox}
wenzelm@4395
   435
addcongs   : simpset * thm list -> simpset \hfill{\bf infix 4}
wenzelm@4395
   436
delcongs   : simpset * thm list -> simpset \hfill{\bf infix 4}
wenzelm@4395
   437
addeqcongs : simpset * thm list -> simpset \hfill{\bf infix 4}
wenzelm@4395
   438
deleqcongs : simpset * thm list -> simpset \hfill{\bf infix 4}
wenzelm@4395
   439
\end{ttbox}
wenzelm@4395
   440
lcp@104
   441
Congruence rules are meta-equalities of the form
wenzelm@3108
   442
\[ \dots \Imp
lcp@104
   443
   f(\Var{x@1},\ldots,\Var{x@n}) \equiv f(\Var{y@1},\ldots,\Var{y@n}).
lcp@104
   444
\]
lcp@323
   445
This governs the simplification of the arguments of~$f$.  For
lcp@104
   446
example, some arguments can be simplified under additional assumptions:
lcp@104
   447
\[ \List{\Var{P@1} \bimp \Var{Q@1};\; \Var{Q@1} \Imp \Var{P@2} \bimp \Var{Q@2}}
lcp@104
   448
   \Imp (\Var{P@1} \imp \Var{P@2}) \equiv (\Var{Q@1} \imp \Var{Q@2})
lcp@104
   449
\]
wenzelm@4395
   450
Given this rule, the simplifier assumes $Q@1$ and extracts rewrite
wenzelm@4395
   451
rules from it when simplifying~$P@2$.  Such local assumptions are
wenzelm@4395
   452
effective for rewriting formulae such as $x=0\imp y+x=y$.  The local
wenzelm@4395
   453
assumptions are also provided as theorems to the solver; see
oheimb@11181
   454
{\S}~\ref{sec:simp-solver} below.
lcp@698
   455
wenzelm@4395
   456
\begin{ttdescription}
wenzelm@4395
   457
  
wenzelm@4395
   458
\item[$ss$ \ttindexbold{addcongs} $thms$] adds congruence rules to the
wenzelm@4395
   459
  simpset $ss$.  These are derived from $thms$ in an appropriate way,
wenzelm@4395
   460
  depending on the underlying object-logic.
wenzelm@4395
   461
  
wenzelm@4395
   462
\item[$ss$ \ttindexbold{delcongs} $thms$] deletes congruence rules
wenzelm@4395
   463
  derived from $thms$.
wenzelm@4395
   464
  
wenzelm@4395
   465
\item[$ss$ \ttindexbold{addeqcongs} $thms$] adds congruence rules in
wenzelm@4395
   466
  their internal form (conclusions using meta-equality) to simpset
wenzelm@4395
   467
  $ss$.  This is the basic mechanism that \texttt{addcongs} is built
wenzelm@4395
   468
  on.  It should be rarely used directly.
wenzelm@4395
   469
  
wenzelm@4395
   470
\item[$ss$ \ttindexbold{deleqcongs} $thms$] deletes congruence rules
wenzelm@4395
   471
  in internal form from simpset $ss$.
wenzelm@4395
   472
  
wenzelm@4395
   473
\end{ttdescription}
wenzelm@4395
   474
wenzelm@4395
   475
\medskip
wenzelm@4395
   476
wenzelm@4395
   477
Here are some more examples.  The congruence rule for bounded
wenzelm@4395
   478
quantifiers also supplies contextual information, this time about the
wenzelm@4395
   479
bound variable:
lcp@286
   480
\begin{eqnarray*}
lcp@286
   481
  &&\List{\Var{A}=\Var{B};\; 
lcp@286
   482
          \Forall x. x\in \Var{B} \Imp \Var{P}(x) = \Var{Q}(x)} \Imp{} \\
lcp@286
   483
 &&\qquad\qquad
lcp@286
   484
    (\forall x\in \Var{A}.\Var{P}(x)) = (\forall x\in \Var{B}.\Var{Q}(x))
lcp@286
   485
\end{eqnarray*}
lcp@323
   486
The congruence rule for conditional expressions can supply contextual
lcp@323
   487
information for simplifying the arms:
lcp@104
   488
\[ \List{\Var{p}=\Var{q};~ \Var{q} \Imp \Var{a}=\Var{c};~
oheimb@11181
   489
         \neg\Var{q} \Imp \Var{b}=\Var{d}} \Imp
lcp@104
   490
   if(\Var{p},\Var{a},\Var{b}) \equiv if(\Var{q},\Var{c},\Var{d})
lcp@104
   491
\]
lcp@698
   492
A congruence rule can also {\em prevent\/} simplification of some arguments.
lcp@104
   493
Here is an alternative congruence rule for conditional expressions:
lcp@104
   494
\[ \Var{p}=\Var{q} \Imp
lcp@104
   495
   if(\Var{p},\Var{a},\Var{b}) \equiv if(\Var{q},\Var{a},\Var{b})
lcp@104
   496
\]
lcp@104
   497
Only the first argument is simplified; the others remain unchanged.
lcp@104
   498
This can make simplification much faster, but may require an extra case split
lcp@104
   499
to prove the goal.  
lcp@104
   500
lcp@104
   501
wenzelm@4395
   502
\subsection{*The subgoaler}\label{sec:simp-subgoaler}
wenzelm@4395
   503
\begin{ttbox}
wenzelm@7990
   504
setsubgoaler :
wenzelm@7990
   505
  simpset *  (simpset -> int -> tactic) -> simpset \hfill{\bf infix 4}
wenzelm@4395
   506
prems_of_ss  : simpset -> thm list
wenzelm@4395
   507
\end{ttbox}
wenzelm@4395
   508
lcp@104
   509
The subgoaler is the tactic used to solve subgoals arising out of
lcp@104
   510
conditional rewrite rules or congruence rules.  The default should be
wenzelm@4395
   511
simplification itself.  Occasionally this strategy needs to be
wenzelm@4395
   512
changed.  For example, if the premise of a conditional rule is an
wenzelm@4395
   513
instance of its conclusion, as in $Suc(\Var{m}) < \Var{n} \Imp \Var{m}
wenzelm@4395
   514
< \Var{n}$, the default strategy could loop.
lcp@104
   515
wenzelm@4395
   516
\begin{ttdescription}
wenzelm@4395
   517
  
wenzelm@4395
   518
\item[$ss$ \ttindexbold{setsubgoaler} $tacf$] sets the subgoaler of
wenzelm@4395
   519
  $ss$ to $tacf$.  The function $tacf$ will be applied to the current
wenzelm@4395
   520
  simplifier context expressed as a simpset.
wenzelm@4395
   521
  
wenzelm@4395
   522
\item[\ttindexbold{prems_of_ss} $ss$] retrieves the current set of
wenzelm@4395
   523
  premises from simplifier context $ss$.  This may be non-empty only
wenzelm@4395
   524
  if the simplifier has been told to utilize local assumptions in the
wenzelm@4395
   525
  first place, e.g.\ if invoked via \texttt{asm_simp_tac}.
wenzelm@4395
   526
wenzelm@4395
   527
\end{ttdescription}
wenzelm@4395
   528
wenzelm@4395
   529
As an example, consider the following subgoaler:
lcp@104
   530
\begin{ttbox}
wenzelm@4395
   531
fun subgoaler ss =
wenzelm@4395
   532
    assume_tac ORELSE'
wenzelm@4395
   533
    resolve_tac (prems_of_ss ss) ORELSE'
wenzelm@4395
   534
    asm_simp_tac ss;
lcp@104
   535
\end{ttbox}
wenzelm@4395
   536
This tactic first tries to solve the subgoal by assumption or by
wenzelm@4395
   537
resolving with with one of the premises, calling simplification only
wenzelm@4395
   538
if that fails.
wenzelm@4395
   539
lcp@104
   540
lcp@698
   541
\subsection{*The solver}\label{sec:simp-solver}
wenzelm@4395
   542
\begin{ttbox}
nipkow@7620
   543
mk_solver  : string -> (thm list -> int -> tactic) -> solver
nipkow@7620
   544
setSolver  : simpset * solver -> simpset \hfill{\bf infix 4}
nipkow@7620
   545
addSolver  : simpset * solver -> simpset \hfill{\bf infix 4}
nipkow@7620
   546
setSSolver : simpset * solver -> simpset \hfill{\bf infix 4}
nipkow@7620
   547
addSSolver : simpset * solver -> simpset \hfill{\bf infix 4}
wenzelm@4395
   548
\end{ttbox}
wenzelm@4395
   549
nipkow@7620
   550
A solver is a tactic that attempts to solve a subgoal after
wenzelm@4395
   551
simplification.  Typically it just proves trivial subgoals such as
paulson@4597
   552
\texttt{True} and $t=t$.  It could use sophisticated means such as {\tt
wenzelm@4395
   553
  blast_tac}, though that could make simplification expensive.
nipkow@7620
   554
To keep things more abstract, solvers are packaged up in type
nipkow@7620
   555
\texttt{solver}. The only way to create a solver is via \texttt{mk_solver}.
lcp@286
   556
wenzelm@3108
   557
Rewriting does not instantiate unknowns.  For example, rewriting
wenzelm@3108
   558
cannot prove $a\in \Var{A}$ since this requires
wenzelm@3108
   559
instantiating~$\Var{A}$.  The solver, however, is an arbitrary tactic
wenzelm@3108
   560
and may instantiate unknowns as it pleases.  This is the only way the
wenzelm@3108
   561
simplifier can handle a conditional rewrite rule whose condition
paulson@3485
   562
contains extra variables.  When a simplification tactic is to be
wenzelm@3108
   563
combined with other provers, especially with the classical reasoner,
wenzelm@4395
   564
it is important whether it can be considered safe or not.  For this
nipkow@7620
   565
reason a simpset contains two solvers, a safe and an unsafe one.
oheimb@2628
   566
wenzelm@3108
   567
The standard simplification strategy solely uses the unsafe solver,
wenzelm@4395
   568
which is appropriate in most cases.  For special applications where
wenzelm@3108
   569
the simplification process is not allowed to instantiate unknowns
wenzelm@4395
   570
within the goal, simplification starts with the safe solver, but may
wenzelm@4395
   571
still apply the ordinary unsafe one in nested simplifications for
oheimb@9398
   572
conditional rules or congruences. Note that in this way the overall
oheimb@9398
   573
tactic is not totally safe:  it may instantiate unknowns that appear also 
oheimb@9398
   574
in other subgoals.
lcp@323
   575
wenzelm@4395
   576
\begin{ttdescription}
nipkow@7620
   577
\item[\ttindexbold{mk_solver} $s$ $tacf$] converts $tacf$ into a new solver;
nipkow@7620
   578
  the string $s$ is only attached as a comment and has no other significance.
nipkow@7620
   579
wenzelm@4395
   580
\item[$ss$ \ttindexbold{setSSolver} $tacf$] installs $tacf$ as the
wenzelm@4395
   581
  \emph{safe} solver of $ss$.
wenzelm@4395
   582
  
wenzelm@4395
   583
\item[$ss$ \ttindexbold{addSSolver} $tacf$] adds $tacf$ as an
wenzelm@4395
   584
  additional \emph{safe} solver; it will be tried after the solvers
wenzelm@4395
   585
  which had already been present in $ss$.
wenzelm@4395
   586
  
wenzelm@4395
   587
\item[$ss$ \ttindexbold{setSolver} $tacf$] installs $tacf$ as the
wenzelm@4395
   588
  unsafe solver of $ss$.
wenzelm@4395
   589
  
wenzelm@4395
   590
\item[$ss$ \ttindexbold{addSolver} $tacf$] adds $tacf$ as an
wenzelm@4395
   591
  additional unsafe solver; it will be tried after the solvers which
wenzelm@4395
   592
  had already been present in $ss$.
lcp@104
   593
wenzelm@4395
   594
\end{ttdescription}
wenzelm@4395
   595
wenzelm@4395
   596
\medskip
wenzelm@4395
   597
wenzelm@4395
   598
\index{assumptions!in simplification} The solver tactic is invoked
wenzelm@4395
   599
with a list of theorems, namely assumptions that hold in the local
wenzelm@4395
   600
context.  This may be non-empty only if the simplifier has been told
wenzelm@4395
   601
to utilize local assumptions in the first place, e.g.\ if invoked via
wenzelm@4395
   602
\texttt{asm_simp_tac}.  The solver is also presented the full goal
wenzelm@4395
   603
including its assumptions in any case.  Thus it can use these (e.g.\ 
wenzelm@4395
   604
by calling \texttt{assume_tac}), even if the list of premises is not
wenzelm@4395
   605
passed.
wenzelm@4395
   606
wenzelm@4395
   607
\medskip
wenzelm@4395
   608
oheimb@11181
   609
As explained in {\S}\ref{sec:simp-subgoaler}, the subgoaler is also used
wenzelm@4395
   610
to solve the premises of congruence rules.  These are usually of the
wenzelm@4395
   611
form $s = \Var{x}$, where $s$ needs to be simplified and $\Var{x}$
wenzelm@4395
   612
needs to be instantiated with the result.  Typically, the subgoaler
wenzelm@4395
   613
will invoke the simplifier at some point, which will eventually call
wenzelm@4395
   614
the solver.  For this reason, solver tactics must be prepared to solve
wenzelm@4395
   615
goals of the form $t = \Var{x}$, usually by reflexivity.  In
wenzelm@4395
   616
particular, reflexivity should be tried before any of the fancy
paulson@4597
   617
tactics like \texttt{blast_tac}.
lcp@323
   618
wenzelm@3108
   619
It may even happen that due to simplification the subgoal is no longer
wenzelm@3108
   620
an equality.  For example $False \bimp \Var{Q}$ could be rewritten to
oheimb@11181
   621
$\neg\Var{Q}$.  To cover this case, the solver could try resolving
oheimb@11181
   622
with the theorem $\neg False$.
lcp@323
   623
wenzelm@4395
   624
\medskip
wenzelm@4395
   625
lcp@104
   626
\begin{warn}
ballarin@13938
   627
  If a premise of a congruence rule cannot be proved, then the
ballarin@13938
   628
  congruence is ignored.  This should only happen if the rule is
ballarin@13938
   629
  \emph{conditional} --- that is, contains premises not of the form $t
ballarin@13938
   630
  = \Var{x}$; otherwise it indicates that some congruence rule, or
ballarin@13938
   631
  possibly the subgoaler or solver, is faulty.
lcp@104
   632
\end{warn}
lcp@104
   633
lcp@104
   634
wenzelm@4395
   635
\subsection{*The looper}\label{sec:simp-looper}
wenzelm@4395
   636
\begin{ttbox}
oheimb@5549
   637
setloop   : simpset *           (int -> tactic)  -> simpset \hfill{\bf infix 4}
oheimb@5549
   638
addloop   : simpset * (string * (int -> tactic)) -> simpset \hfill{\bf infix 4}
oheimb@5549
   639
delloop   : simpset *  string                    -> simpset \hfill{\bf infix 4}
wenzelm@4395
   640
addsplits : simpset * thm list -> simpset \hfill{\bf infix 4}
oheimb@5549
   641
delsplits : simpset * thm list -> simpset \hfill{\bf infix 4}
wenzelm@4395
   642
\end{ttbox}
lcp@104
   643
oheimb@5549
   644
The looper is a list of tactics that are applied after simplification, in case
wenzelm@4395
   645
the solver failed to solve the simplified goal.  If the looper
wenzelm@4395
   646
succeeds, the simplification process is started all over again.  Each
wenzelm@4395
   647
of the subgoals generated by the looper is attacked in turn, in
wenzelm@4395
   648
reverse order.
lcp@104
   649
oheimb@9398
   650
A typical looper is \index{case splitting}: the expansion of a conditional.
wenzelm@4395
   651
Another possibility is to apply an elimination rule on the
wenzelm@4395
   652
assumptions.  More adventurous loopers could start an induction.
oheimb@2567
   653
wenzelm@4395
   654
\begin{ttdescription}
wenzelm@4395
   655
  
oheimb@5549
   656
\item[$ss$ \ttindexbold{setloop} $tacf$] installs $tacf$ as the only looper
oheimb@5549
   657
  tactic of $ss$.
wenzelm@4395
   658
  
oheimb@5549
   659
\item[$ss$ \ttindexbold{addloop} $(name,tacf)$] adds $tacf$ as an additional
oheimb@5549
   660
  looper tactic with name $name$; it will be tried after the looper tactics
oheimb@5549
   661
  that had already been present in $ss$.
oheimb@5549
   662
  
oheimb@5549
   663
\item[$ss$ \ttindexbold{delloop} $name$] deletes the looper tactic $name$
oheimb@5549
   664
  from $ss$.
wenzelm@4395
   665
  
wenzelm@4395
   666
\item[$ss$ \ttindexbold{addsplits} $thms$] adds
oheimb@5549
   667
  split tactics for $thms$ as additional looper tactics of $ss$.
oheimb@5549
   668
oheimb@5549
   669
\item[$ss$ \ttindexbold{addsplits} $thms$] deletes the
oheimb@5549
   670
  split tactics for $thms$ from the looper tactics of $ss$.
wenzelm@4395
   671
wenzelm@4395
   672
\end{ttdescription}
wenzelm@4395
   673
oheimb@5549
   674
The splitter replaces applications of a given function; the right-hand side
oheimb@5549
   675
of the replacement can be anything.  For example, here is a splitting rule
oheimb@5549
   676
for conditional expressions:
oheimb@5549
   677
\[ \Var{P}(if(\Var{Q},\Var{x},\Var{y})) \bimp (\Var{Q} \imp \Var{P}(\Var{x}))
oheimb@11181
   678
\conj (\neg\Var{Q} \imp \Var{P}(\Var{y})) 
oheimb@5549
   679
\] 
paulson@8136
   680
Another example is the elimination operator for Cartesian products (which
paulson@8136
   681
happens to be called~$split$):  
oheimb@5549
   682
\[ \Var{P}(split(\Var{f},\Var{p})) \bimp (\forall a~b. \Var{p} =
oheimb@5549
   683
\langle a,b\rangle \imp \Var{P}(\Var{f}(a,b))) 
oheimb@5549
   684
\] 
oheimb@5549
   685
oheimb@5549
   686
For technical reasons, there is a distinction between case splitting in the 
oheimb@5549
   687
conclusion and in the premises of a subgoal. The former is done by
oheimb@9398
   688
\texttt{split_tac} with rules like \texttt{split_if} or \texttt{option.split}, 
oheimb@9398
   689
which do not split the subgoal, while the latter is done by 
oheimb@9398
   690
\texttt{split_asm_tac} with rules like \texttt{split_if_asm} or 
oheimb@9398
   691
\texttt{option.split_asm}, which split the subgoal.
oheimb@5549
   692
The operator \texttt{addsplits} automatically takes care of which tactic to
oheimb@5549
   693
call, analyzing the form of the rules given as argument.
oheimb@5549
   694
\begin{warn}
oheimb@5549
   695
Due to \texttt{split_asm_tac}, the simplifier may split subgoals!
oheimb@5549
   696
\end{warn}
oheimb@5549
   697
oheimb@5549
   698
Case splits should be allowed only when necessary; they are expensive
oheimb@5549
   699
and hard to control.  Here is an example of use, where \texttt{split_if}
oheimb@5549
   700
is the first rule above:
oheimb@5549
   701
\begin{ttbox}
paulson@8136
   702
by (simp_tac (simpset() 
paulson@8136
   703
                 addloop ("split if", split_tac [split_if])) 1);
oheimb@5549
   704
\end{ttbox}
wenzelm@5776
   705
Users would usually prefer the following shortcut using \texttt{addsplits}:
oheimb@5549
   706
\begin{ttbox}
oheimb@5549
   707
by (simp_tac (simpset() addsplits [split_if]) 1);
oheimb@5549
   708
\end{ttbox}
paulson@8136
   709
Case-splitting on conditional expressions is usually beneficial, so it is
paulson@8136
   710
enabled by default in the object-logics \texttt{HOL} and \texttt{FOL}.
wenzelm@4395
   711
wenzelm@4395
   712
wenzelm@4395
   713
\section{The simplification tactics}\label{simp-tactics}
wenzelm@4395
   714
\index{simplification!tactics}\index{tactics!simplification}
lcp@104
   715
\begin{ttbox}
oheimb@9398
   716
generic_simp_tac       : bool -> bool * bool * bool -> 
oheimb@9398
   717
                         simpset -> int -> tactic
wenzelm@4395
   718
simp_tac               : simpset -> int -> tactic
wenzelm@4395
   719
asm_simp_tac           : simpset -> int -> tactic
wenzelm@4395
   720
full_simp_tac          : simpset -> int -> tactic
wenzelm@4395
   721
asm_full_simp_tac      : simpset -> int -> tactic
wenzelm@4395
   722
safe_asm_full_simp_tac : simpset -> int -> tactic
wenzelm@4395
   723
\end{ttbox}
lcp@104
   724
oheimb@9398
   725
\texttt{generic_simp_tac} is the basic tactic that is underlying any actual
oheimb@9398
   726
simplification work. The others are just instantiations of it. The rewriting 
oheimb@9398
   727
strategy is always strictly bottom up, except for congruence rules, 
oheimb@9398
   728
which are applied while descending into a term.  Conditions in conditional 
oheimb@9398
   729
rewrite rules are solved recursively before the rewrite rule is applied.
lcp@104
   730
wenzelm@4395
   731
\begin{ttdescription}
wenzelm@4395
   732
  
oheimb@9398
   733
\item[\ttindexbold{generic_simp_tac} $safe$ ($simp\_asm$, $use\_asm$, $mutual$)] 
oheimb@9398
   734
  gives direct access to the various simplification modes: 
oheimb@9398
   735
  \begin{itemize}
oheimb@9398
   736
  \item if $safe$ is {\tt true}, the safe solver is used as explained in
oheimb@11181
   737
  {\S}\ref{sec:simp-solver},  
oheimb@9398
   738
  \item $simp\_asm$ determines whether the local assumptions are simplified,
oheimb@9398
   739
  \item $use\_asm$ determines whether the assumptions are used as local rewrite 
oheimb@9398
   740
   rules, and
oheimb@9398
   741
  \item $mutual$ determines whether assumptions can simplify each other rather
oheimb@9398
   742
  than being processed from left to right. 
oheimb@9398
   743
  \end{itemize}
oheimb@9398
   744
  This generic interface is intended 
oheimb@9398
   745
  for building special tools, e.g.\ for combining the simplifier with the 
oheimb@9398
   746
  classical reasoner. It is rarely used directly.
oheimb@9398
   747
  
wenzelm@4395
   748
\item[\ttindexbold{simp_tac}, \ttindexbold{asm_simp_tac},
wenzelm@4395
   749
  \ttindexbold{full_simp_tac}, \ttindexbold{asm_full_simp_tac}] are
wenzelm@4395
   750
  the basic simplification tactics that work exactly like their
oheimb@11181
   751
  namesakes in {\S}\ref{sec:simp-for-dummies}, except that they are
wenzelm@4395
   752
  explicitly supplied with a simpset.
wenzelm@4395
   753
  
wenzelm@4395
   754
\end{ttdescription}
lcp@104
   755
wenzelm@4395
   756
\medskip
lcp@104
   757
wenzelm@4395
   758
Local modifications of simpsets within a proof are often much cleaner
wenzelm@4395
   759
by using above tactics in conjunction with explicit simpsets, rather
wenzelm@4395
   760
than their capitalized counterparts.  For example
nipkow@1860
   761
\begin{ttbox}
nipkow@1860
   762
Addsimps \(thms\);
paulson@2479
   763
by (Simp_tac \(i\));
nipkow@1860
   764
Delsimps \(thms\);
nipkow@1860
   765
\end{ttbox}
wenzelm@4395
   766
can be expressed more appropriately as
nipkow@1860
   767
\begin{ttbox}
wenzelm@4395
   768
by (simp_tac (simpset() addsimps \(thms\)) \(i\));
nipkow@1860
   769
\end{ttbox}
nipkow@1860
   770
wenzelm@4395
   771
\medskip
nipkow@1860
   772
wenzelm@4395
   773
Also note that functions depending implicitly on the current theory
wenzelm@4395
   774
context (like capital \texttt{Simp_tac} and the other commands of
oheimb@11181
   775
{\S}\ref{sec:simp-for-dummies}) should be considered harmful outside of
wenzelm@4395
   776
actual proof scripts.  In particular, ML programs like theory
wenzelm@4395
   777
definition packages or special tactics should refer to simpsets only
wenzelm@4395
   778
explicitly, via the above tactics used in conjunction with
wenzelm@4395
   779
\texttt{simpset_of} or the \texttt{SIMPSET} tacticals.
lcp@104
   780
wenzelm@4395
   781
wenzelm@5370
   782
\section{Forward rules and conversions}
wenzelm@5370
   783
\index{simplification!forward rules}\index{simplification!conversions}
wenzelm@5370
   784
\begin{ttbox}\index{*simplify}\index{*asm_simplify}\index{*full_simplify}\index{*asm_full_simplify}\index{*Simplifier.rewrite}\index{*Simplifier.asm_rewrite}\index{*Simplifier.full_rewrite}\index{*Simplifier.asm_full_rewrite}
wenzelm@4395
   785
simplify          : simpset -> thm -> thm
wenzelm@4395
   786
asm_simplify      : simpset -> thm -> thm
wenzelm@4395
   787
full_simplify     : simpset -> thm -> thm
wenzelm@5370
   788
asm_full_simplify : simpset -> thm -> thm\medskip
wenzelm@5370
   789
Simplifier.rewrite           : simpset -> cterm -> thm
wenzelm@5370
   790
Simplifier.asm_rewrite       : simpset -> cterm -> thm
wenzelm@5370
   791
Simplifier.full_rewrite      : simpset -> cterm -> thm
wenzelm@5370
   792
Simplifier.asm_full_rewrite  : simpset -> cterm -> thm
wenzelm@4395
   793
\end{ttbox}
wenzelm@4395
   794
wenzelm@5370
   795
The first four of these functions provide \emph{forward} rules for
wenzelm@5370
   796
simplification.  Their effect is analogous to the corresponding
oheimb@11181
   797
tactics described in {\S}\ref{simp-tactics}, but affect the whole
wenzelm@5370
   798
theorem instead of just a certain subgoal.  Also note that the
oheimb@11181
   799
looper~/ solver process as described in {\S}\ref{sec:simp-looper} and
oheimb@11181
   800
{\S}\ref{sec:simp-solver} is omitted in forward simplification.
wenzelm@5370
   801
wenzelm@5370
   802
The latter four are \emph{conversions}, establishing proven equations
wenzelm@5370
   803
of the form $t \equiv u$ where the l.h.s.\ $t$ has been given as
wenzelm@5370
   804
argument.
wenzelm@4395
   805
wenzelm@4395
   806
\begin{warn}
wenzelm@5370
   807
  Forward simplification rules and conversions should be used rarely
wenzelm@5370
   808
  in ordinary proof scripts.  The main intention is to provide an
wenzelm@5370
   809
  internal interface to the simplifier for special utilities.
wenzelm@4395
   810
\end{warn}
wenzelm@4395
   811
wenzelm@4395
   812
lcp@332
   813
\section{Permutative rewrite rules}
lcp@323
   814
\index{rewrite rules!permutative|(}
lcp@323
   815
lcp@323
   816
A rewrite rule is {\bf permutative} if the left-hand side and right-hand
lcp@323
   817
side are the same up to renaming of variables.  The most common permutative
lcp@323
   818
rule is commutativity: $x+y = y+x$.  Other examples include $(x-y)-z =
lcp@323
   819
(x-z)-y$ in arithmetic and $insert(x,insert(y,A)) = insert(y,insert(x,A))$
lcp@323
   820
for sets.  Such rules are common enough to merit special attention.
lcp@323
   821
wenzelm@4395
   822
Because ordinary rewriting loops given such rules, the simplifier
wenzelm@4395
   823
employs a special strategy, called {\bf ordered
wenzelm@4395
   824
  rewriting}\index{rewriting!ordered}.  There is a standard
wenzelm@4395
   825
lexicographic ordering on terms.  This should be perfectly OK in most
wenzelm@4395
   826
cases, but can be changed for special applications.
lcp@323
   827
nipkow@4947
   828
\begin{ttbox}
nipkow@4947
   829
settermless : simpset * (term * term -> bool) -> simpset \hfill{\bf infix 4}
nipkow@4947
   830
\end{ttbox}
wenzelm@4395
   831
\begin{ttdescription}
wenzelm@4395
   832
  
wenzelm@4395
   833
\item[$ss$ \ttindexbold{settermless} $rel$] installs relation $rel$ as
wenzelm@4395
   834
  term order in simpset $ss$.
wenzelm@4395
   835
wenzelm@4395
   836
\end{ttdescription}
wenzelm@4395
   837
wenzelm@4395
   838
\medskip
wenzelm@4395
   839
wenzelm@4395
   840
A permutative rewrite rule is applied only if it decreases the given
wenzelm@4395
   841
term with respect to this ordering.  For example, commutativity
wenzelm@4395
   842
rewrites~$b+a$ to $a+b$, but then stops because $a+b$ is strictly less
wenzelm@4395
   843
than $b+a$.  The Boyer-Moore theorem prover~\cite{bm88book} also
wenzelm@4395
   844
employs ordered rewriting.
wenzelm@4395
   845
wenzelm@4395
   846
Permutative rewrite rules are added to simpsets just like other
wenzelm@4395
   847
rewrite rules; the simplifier recognizes their special status
wenzelm@4395
   848
automatically.  They are most effective in the case of
wenzelm@4395
   849
associative-commutative operators.  (Associativity by itself is not
wenzelm@4395
   850
permutative.)  When dealing with an AC-operator~$f$, keep the
wenzelm@4395
   851
following points in mind:
lcp@323
   852
\begin{itemize}\index{associative-commutative operators}
wenzelm@4395
   853
  
wenzelm@4395
   854
\item The associative law must always be oriented from left to right,
wenzelm@4395
   855
  namely $f(f(x,y),z) = f(x,f(y,z))$.  The opposite orientation, if
wenzelm@4395
   856
  used with commutativity, leads to looping in conjunction with the
wenzelm@4395
   857
  standard term order.
lcp@323
   858
lcp@323
   859
\item To complete your set of rewrite rules, you must add not just
lcp@323
   860
  associativity~(A) and commutativity~(C) but also a derived rule, {\bf
paulson@4597
   861
    left-com\-mut\-ativ\-ity} (LC): $f(x,f(y,z)) = f(y,f(x,z))$.
lcp@323
   862
\end{itemize}
lcp@323
   863
Ordered rewriting with the combination of A, C, and~LC sorts a term
lcp@323
   864
lexicographically:
lcp@323
   865
\[\def\maps#1{\stackrel{#1}{\longmapsto}}
lcp@323
   866
 (b+c)+a \maps{A} b+(c+a) \maps{C} b+(a+c) \maps{LC} a+(b+c) \]
lcp@323
   867
Martin and Nipkow~\cite{martin-nipkow} discuss the theory and give many
lcp@323
   868
examples; other algebraic structures are amenable to ordered rewriting,
lcp@323
   869
such as boolean rings.
lcp@323
   870
wenzelm@3108
   871
\subsection{Example: sums of natural numbers}
wenzelm@4395
   872
wenzelm@9695
   873
This example is again set in HOL (see \texttt{HOL/ex/NatSum}).  Theory
wenzelm@9695
   874
\thydx{Arith} contains natural numbers arithmetic.  Its associated simpset
wenzelm@9695
   875
contains many arithmetic laws including distributivity of~$\times$ over~$+$,
wenzelm@9695
   876
while \texttt{add_ac} is a list consisting of the A, C and LC laws for~$+$ on
wenzelm@9695
   877
type \texttt{nat}.  Let us prove the theorem
lcp@323
   878
\[ \sum@{i=1}^n i = n\times(n+1)/2. \]
lcp@323
   879
%
paulson@4597
   880
A functional~\texttt{sum} represents the summation operator under the
paulson@4597
   881
interpretation $\texttt{sum} \, f \, (n + 1) = \sum@{i=0}^n f\,i$.  We
paulson@4597
   882
extend \texttt{Arith} as follows:
lcp@323
   883
\begin{ttbox}
lcp@323
   884
NatSum = Arith +
clasohm@1387
   885
consts sum     :: [nat=>nat, nat] => nat
berghofe@9445
   886
primrec 
paulson@4245
   887
  "sum f 0 = 0"
paulson@4245
   888
  "sum f (Suc n) = f(n) + sum f n"
lcp@323
   889
end
lcp@323
   890
\end{ttbox}
paulson@4245
   891
The \texttt{primrec} declaration automatically adds rewrite rules for
wenzelm@4557
   892
\texttt{sum} to the default simpset.  We now remove the
wenzelm@4557
   893
\texttt{nat_cancel} simplification procedures (in order not to spoil
wenzelm@4557
   894
the example) and insert the AC-rules for~$+$:
lcp@323
   895
\begin{ttbox}
wenzelm@4557
   896
Delsimprocs nat_cancel;
paulson@4245
   897
Addsimps add_ac;
lcp@323
   898
\end{ttbox}
paulson@4597
   899
Our desired theorem now reads $\texttt{sum} \, (\lambda i.i) \, (n+1) =
lcp@323
   900
n\times(n+1)/2$.  The Isabelle goal has both sides multiplied by~$2$:
lcp@323
   901
\begin{ttbox}
paulson@5205
   902
Goal "2 * sum (\%i.i) (Suc n) = n * Suc n";
lcp@323
   903
{\out Level 0}
wenzelm@3108
   904
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n}
wenzelm@3108
   905
{\out  1. 2 * sum (\%i. i) (Suc n) = n * Suc n}
lcp@323
   906
\end{ttbox}
wenzelm@3108
   907
Induction should not be applied until the goal is in the simplest
wenzelm@3108
   908
form:
lcp@323
   909
\begin{ttbox}
paulson@4245
   910
by (Simp_tac 1);
lcp@323
   911
{\out Level 1}
wenzelm@3108
   912
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n}
wenzelm@3108
   913
{\out  1. n + (sum (\%i. i) n + sum (\%i. i) n) = n * n}
lcp@323
   914
\end{ttbox}
wenzelm@3108
   915
Ordered rewriting has sorted the terms in the left-hand side.  The
wenzelm@3108
   916
subgoal is now ready for induction:
lcp@323
   917
\begin{ttbox}
paulson@4245
   918
by (induct_tac "n" 1);
lcp@323
   919
{\out Level 2}
wenzelm@3108
   920
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n}
wenzelm@3108
   921
{\out  1. 0 + (sum (\%i. i) 0 + sum (\%i. i) 0) = 0 * 0}
lcp@323
   922
\ttbreak
paulson@4245
   923
{\out  2. !!n. n + (sum (\%i. i) n + sum (\%i. i) n) = n * n}
paulson@8136
   924
{\out           ==> Suc n + (sum (\%i. i) (Suc n) + sum (\%i.\,i) (Suc n)) =}
paulson@4245
   925
{\out               Suc n * Suc n}
lcp@323
   926
\end{ttbox}
lcp@323
   927
Simplification proves both subgoals immediately:\index{*ALLGOALS}
lcp@323
   928
\begin{ttbox}
paulson@4245
   929
by (ALLGOALS Asm_simp_tac);
lcp@323
   930
{\out Level 3}
wenzelm@3108
   931
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n}
lcp@323
   932
{\out No subgoals!}
lcp@323
   933
\end{ttbox}
paulson@4597
   934
Simplification cannot prove the induction step if we omit \texttt{add_ac} from
paulson@4245
   935
the simpset.  Observe that like terms have not been collected:
lcp@323
   936
\begin{ttbox}
paulson@4245
   937
{\out Level 3}
paulson@4245
   938
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n}
paulson@4245
   939
{\out  1. !!n. n + sum (\%i. i) n + (n + sum (\%i. i) n) = n + n * n}
paulson@8136
   940
{\out           ==> n + (n + sum (\%i. i) n) + (n + (n + sum (\%i.\,i) n)) =}
paulson@4245
   941
{\out               n + (n + (n + n * n))}
lcp@323
   942
\end{ttbox}
lcp@323
   943
Ordered rewriting proves this by sorting the left-hand side.  Proving
lcp@323
   944
arithmetic theorems without ordered rewriting requires explicit use of
lcp@323
   945
commutativity.  This is tedious; try it and see!
lcp@323
   946
lcp@323
   947
Ordered rewriting is equally successful in proving
lcp@323
   948
$\sum@{i=1}^n i^3 = n^2\times(n+1)^2/4$.
lcp@323
   949
lcp@323
   950
lcp@323
   951
\subsection{Re-orienting equalities}
paulson@4597
   952
Ordered rewriting with the derived rule \texttt{symmetry} can reverse
wenzelm@4557
   953
equations:
lcp@323
   954
\begin{ttbox}
lcp@323
   955
val symmetry = prove_goal HOL.thy "(x=y) = (y=x)"
paulson@3128
   956
                 (fn _ => [Blast_tac 1]);
lcp@323
   957
\end{ttbox}
lcp@323
   958
This is frequently useful.  Assumptions of the form $s=t$, where $t$ occurs
lcp@323
   959
in the conclusion but not~$s$, can often be brought into the right form.
paulson@4597
   960
For example, ordered rewriting with \texttt{symmetry} can prove the goal
lcp@323
   961
\[ f(a)=b \conj f(a)=c \imp b=c. \]
paulson@4597
   962
Here \texttt{symmetry} reverses both $f(a)=b$ and $f(a)=c$
lcp@323
   963
because $f(a)$ is lexicographically greater than $b$ and~$c$.  These
lcp@323
   964
re-oriented equations, as rewrite rules, replace $b$ and~$c$ in the
lcp@323
   965
conclusion by~$f(a)$. 
lcp@323
   966
oheimb@11181
   967
Another example is the goal $\neg(t=u) \imp \neg(u=t)$.
lcp@323
   968
The differing orientations make this appear difficult to prove.  Ordered
paulson@4597
   969
rewriting with \texttt{symmetry} makes the equalities agree.  (Without
lcp@323
   970
knowing more about~$t$ and~$u$ we cannot say whether they both go to $t=u$
lcp@323
   971
or~$u=t$.)  Then the simplifier can prove the goal outright.
lcp@323
   972
lcp@323
   973
\index{rewrite rules!permutative|)}
lcp@323
   974
lcp@323
   975
wenzelm@4395
   976
\section{*Coding simplification procedures}
wenzelm@4395
   977
\begin{ttbox}
wenzelm@13474
   978
  val Simplifier.simproc: Sign.sg -> string -> string list
wenzelm@15027
   979
    -> (Sign.sg -> simpset -> term -> thm option) -> simproc
wenzelm@13474
   980
  val Simplifier.simproc_i: Sign.sg -> string -> term list
wenzelm@15027
   981
    -> (Sign.sg -> simpset -> term -> thm option) -> simproc
wenzelm@4395
   982
\end{ttbox}
wenzelm@4395
   983
wenzelm@4395
   984
\begin{ttdescription}
wenzelm@13477
   985
\item[\ttindexbold{Simplifier.simproc}~$sign$~$name$~$lhss$~$proc$] makes
wenzelm@13477
   986
  $proc$ a simplification procedure for left-hand side patterns $lhss$.  The
wenzelm@13477
   987
  name just serves as a comment.  The function $proc$ may be invoked by the
wenzelm@13477
   988
  simplifier for redex positions matched by one of $lhss$ as described below
wenzelm@13477
   989
  (which are be specified as strings to be read as terms).
wenzelm@13477
   990
  
wenzelm@13477
   991
\item[\ttindexbold{Simplifier.simproc_i}] is similar to
wenzelm@13477
   992
  \verb,Simplifier.simproc,, but takes well-typed terms as pattern argument.
wenzelm@4395
   993
\end{ttdescription}
wenzelm@4395
   994
wenzelm@4395
   995
Simplification procedures are applied in a two-stage process as
wenzelm@4395
   996
follows: The simplifier tries to match the current redex position
wenzelm@4395
   997
against any one of the $lhs$ patterns of any simplification procedure.
wenzelm@4395
   998
If this succeeds, it invokes the corresponding {\ML} function, passing
wenzelm@4395
   999
with the current signature, local assumptions and the (potential)
wenzelm@4395
  1000
redex.  The result may be either \texttt{None} (indicating failure) or
wenzelm@4395
  1001
\texttt{Some~$thm$}.
wenzelm@4395
  1002
wenzelm@4395
  1003
Any successful result is supposed to be a (possibly conditional)
wenzelm@4395
  1004
rewrite rule $t \equiv u$ that is applicable to the current redex.
wenzelm@4395
  1005
The rule will be applied just as any ordinary rewrite rule.  It is
wenzelm@4395
  1006
expected to be already in \emph{internal form}, though, bypassing the
wenzelm@4395
  1007
automatic preprocessing of object-level equivalences.
wenzelm@4395
  1008
wenzelm@4395
  1009
\medskip
wenzelm@4395
  1010
wenzelm@4395
  1011
As an example of how to write your own simplification procedures,
wenzelm@4395
  1012
consider eta-expansion of pair abstraction (see also
wenzelm@4395
  1013
\texttt{HOL/Modelcheck/MCSyn} where this is used to provide external
wenzelm@4395
  1014
model checker syntax).
wenzelm@4395
  1015
  
wenzelm@9695
  1016
The HOL theory of tuples (see \texttt{HOL/Prod}) provides an operator
wenzelm@9695
  1017
\texttt{split} together with some concrete syntax supporting
wenzelm@9695
  1018
$\lambda\,(x,y).b$ abstractions.  Assume that we would like to offer a tactic
wenzelm@9695
  1019
that rewrites any function $\lambda\,p.f\,p$ (where $p$ is of some pair type)
wenzelm@9695
  1020
to $\lambda\,(x,y).f\,(x,y)$.  The corresponding rule is:
wenzelm@4395
  1021
\begin{ttbox}
wenzelm@4395
  1022
pair_eta_expand:  (f::'a*'b=>'c) = (\%(x, y). f (x, y))
wenzelm@4395
  1023
\end{ttbox}
wenzelm@4395
  1024
Unfortunately, term rewriting using this rule directly would not
wenzelm@4395
  1025
terminate!  We now use the simplification procedure mechanism in order
wenzelm@4395
  1026
to stop the simplifier from applying this rule over and over again,
wenzelm@4395
  1027
making it rewrite only actual abstractions.  The simplification
wenzelm@4395
  1028
procedure \texttt{pair_eta_expand_proc} is defined as follows:
wenzelm@4395
  1029
\begin{ttbox}
wenzelm@13474
  1030
val pair_eta_expand_proc =
wenzelm@13477
  1031
  Simplifier.simproc (Theory.sign_of (the_context ()))
wenzelm@13477
  1032
    "pair_eta_expand" ["f::'a*'b=>'c"]
wenzelm@13477
  1033
    (fn _ => fn _ => fn t =>
wenzelm@13477
  1034
      case t of Abs _ => Some (mk_meta_eq pair_eta_expand)
wenzelm@13477
  1035
      | _ => None);
wenzelm@4395
  1036
\end{ttbox}
wenzelm@4395
  1037
This is an example of using \texttt{pair_eta_expand_proc}:
wenzelm@4395
  1038
\begin{ttbox}
wenzelm@4395
  1039
{\out 1. P (\%p::'a * 'a. fst p + snd p + z)}
wenzelm@4395
  1040
by (simp_tac (simpset() addsimprocs [pair_eta_expand_proc]) 1);
wenzelm@4395
  1041
{\out 1. P (\%(x::'a,y::'a). x + y + z)}
wenzelm@4395
  1042
\end{ttbox}
wenzelm@4395
  1043
wenzelm@4395
  1044
\medskip
wenzelm@4395
  1045
wenzelm@4395
  1046
In the above example the simplification procedure just did fine
wenzelm@4395
  1047
grained control over rule application, beyond higher-order pattern
wenzelm@4395
  1048
matching.  Usually, procedures would do some more work, in particular
wenzelm@4395
  1049
prove particular theorems depending on the current redex.
wenzelm@4395
  1050
wenzelm@4395
  1051
wenzelm@7990
  1052
\section{*Setting up the Simplifier}\label{sec:setting-up-simp}
lcp@323
  1053
\index{simplification!setting up}
lcp@286
  1054
wenzelm@9712
  1055
Setting up the simplifier for new logics is complicated in the general case.
wenzelm@9712
  1056
This section describes how the simplifier is installed for intuitionistic
wenzelm@9712
  1057
first-order logic; the code is largely taken from {\tt FOL/simpdata.ML} of the
wenzelm@9712
  1058
Isabelle sources.
lcp@286
  1059
wenzelm@16019
  1060
The case splitting tactic, which resides on a separate files, is not part of
wenzelm@16019
  1061
Pure Isabelle.  It needs to be loaded explicitly by the object-logic as
wenzelm@16019
  1062
follows (below \texttt{\~\relax\~\relax} refers to \texttt{\$ISABELLE_HOME}):
lcp@286
  1063
\begin{ttbox}
wenzelm@6569
  1064
use "\~\relax\~\relax/src/Provers/splitter.ML";
lcp@286
  1065
\end{ttbox}
lcp@286
  1066
paulson@4597
  1067
Simplification requires converting object-equalities to meta-level rewrite
paulson@4597
  1068
rules.  This demands rules stating that equal terms and equivalent formulae
paulson@4597
  1069
are also equal at the meta-level.  The rule declaration part of the file
paulson@4597
  1070
\texttt{FOL/IFOL.thy} contains the two lines
lcp@323
  1071
\begin{ttbox}\index{*eq_reflection theorem}\index{*iff_reflection theorem}
lcp@286
  1072
eq_reflection   "(x=y)   ==> (x==y)"
lcp@286
  1073
iff_reflection  "(P<->Q) ==> (P==Q)"
lcp@286
  1074
\end{ttbox}
lcp@323
  1075
Of course, you should only assert such rules if they are true for your
lcp@286
  1076
particular logic.  In Constructive Type Theory, equality is a ternary
wenzelm@4395
  1077
relation of the form $a=b\in A$; the type~$A$ determines the meaning
wenzelm@4395
  1078
of the equality essentially as a partial equivalence relation.  The
paulson@4597
  1079
present simplifier cannot be used.  Rewriting in \texttt{CTT} uses
wenzelm@4395
  1080
another simplifier, which resides in the file {\tt
wenzelm@4395
  1081
  Provers/typedsimp.ML} and is not documented.  Even this does not
wenzelm@4395
  1082
work for later variants of Constructive Type Theory that use
lcp@323
  1083
intensional equality~\cite{nordstrom90}.
lcp@286
  1084
lcp@286
  1085
lcp@286
  1086
\subsection{A collection of standard rewrite rules}
wenzelm@4557
  1087
wenzelm@4557
  1088
We first prove lots of standard rewrite rules about the logical
wenzelm@4557
  1089
connectives.  These include cancellation and associative laws.  We
wenzelm@4557
  1090
define a function that echoes the desired law and then supplies it the
wenzelm@9695
  1091
prover for intuitionistic FOL:
lcp@286
  1092
\begin{ttbox}
lcp@286
  1093
fun int_prove_fun s = 
lcp@286
  1094
 (writeln s;  
lcp@286
  1095
  prove_goal IFOL.thy s
lcp@286
  1096
   (fn prems => [ (cut_facts_tac prems 1), 
wenzelm@4395
  1097
                  (IntPr.fast_tac 1) ]));
lcp@286
  1098
\end{ttbox}
lcp@286
  1099
The following rewrite rules about conjunction are a selection of those
paulson@4597
  1100
proved on \texttt{FOL/simpdata.ML}.  Later, these will be supplied to the
lcp@286
  1101
standard simpset.
lcp@286
  1102
\begin{ttbox}
wenzelm@4395
  1103
val conj_simps = map int_prove_fun
lcp@286
  1104
 ["P & True <-> P",      "True & P <-> P",
lcp@286
  1105
  "P & False <-> False", "False & P <-> False",
lcp@286
  1106
  "P & P <-> P",
lcp@286
  1107
  "P & ~P <-> False",    "~P & P <-> False",
lcp@286
  1108
  "(P & Q) & R <-> P & (Q & R)"];
lcp@286
  1109
\end{ttbox}
lcp@286
  1110
The file also proves some distributive laws.  As they can cause exponential
lcp@286
  1111
blowup, they will not be included in the standard simpset.  Instead they
lcp@323
  1112
are merely bound to an \ML{} identifier, for user reference.
lcp@286
  1113
\begin{ttbox}
wenzelm@4395
  1114
val distrib_simps  = map int_prove_fun
lcp@286
  1115
 ["P & (Q | R) <-> P&Q | P&R", 
lcp@286
  1116
  "(Q | R) & P <-> Q&P | R&P",
lcp@286
  1117
  "(P | Q --> R) <-> (P --> R) & (Q --> R)"];
lcp@286
  1118
\end{ttbox}
lcp@286
  1119
lcp@286
  1120
lcp@286
  1121
\subsection{Functions for preprocessing the rewrite rules}
lcp@323
  1122
\label{sec:setmksimps}
wenzelm@4395
  1123
\begin{ttbox}\indexbold{*setmksimps}
wenzelm@4395
  1124
setmksimps : simpset * (thm -> thm list) -> simpset \hfill{\bf infix 4}
wenzelm@4395
  1125
\end{ttbox}
lcp@286
  1126
The next step is to define the function for preprocessing rewrite rules.
paulson@4597
  1127
This will be installed by calling \texttt{setmksimps} below.  Preprocessing
lcp@286
  1128
occurs whenever rewrite rules are added, whether by user command or
lcp@286
  1129
automatically.  Preprocessing involves extracting atomic rewrites at the
lcp@286
  1130
object-level, then reflecting them to the meta-level.
lcp@286
  1131
wenzelm@12725
  1132
To start, the function \texttt{gen_all} strips any meta-level
wenzelm@12717
  1133
quantifiers from the front of the given theorem.
oheimb@5549
  1134
paulson@4597
  1135
The function \texttt{atomize} analyses a theorem in order to extract
lcp@286
  1136
atomic rewrite rules.  The head of all the patterns, matched by the
paulson@4597
  1137
wildcard~\texttt{_}, is the coercion function \texttt{Trueprop}.
lcp@286
  1138
\begin{ttbox}
lcp@286
  1139
fun atomize th = case concl_of th of 
lcp@286
  1140
    _ $ (Const("op &",_) $ _ $ _)   => atomize(th RS conjunct1) \at
lcp@286
  1141
                                       atomize(th RS conjunct2)
lcp@286
  1142
  | _ $ (Const("op -->",_) $ _ $ _) => atomize(th RS mp)
lcp@286
  1143
  | _ $ (Const("All",_) $ _)        => atomize(th RS spec)
lcp@286
  1144
  | _ $ (Const("True",_))           => []
lcp@286
  1145
  | _ $ (Const("False",_))          => []
lcp@286
  1146
  | _                               => [th];
lcp@286
  1147
\end{ttbox}
lcp@286
  1148
There are several cases, depending upon the form of the conclusion:
lcp@286
  1149
\begin{itemize}
lcp@286
  1150
\item Conjunction: extract rewrites from both conjuncts.
lcp@286
  1151
\item Implication: convert $P\imp Q$ to the meta-implication $P\Imp Q$ and
lcp@286
  1152
  extract rewrites from~$Q$; these will be conditional rewrites with the
lcp@286
  1153
  condition~$P$.
lcp@286
  1154
\item Universal quantification: remove the quantifier, replacing the bound
lcp@286
  1155
  variable by a schematic variable, and extract rewrites from the body.
paulson@4597
  1156
\item \texttt{True} and \texttt{False} contain no useful rewrites.
lcp@286
  1157
\item Anything else: return the theorem in a singleton list.
lcp@286
  1158
\end{itemize}
lcp@286
  1159
The resulting theorems are not literally atomic --- they could be
oheimb@5549
  1160
disjunctive, for example --- but are broken down as much as possible. 
oheimb@5549
  1161
See the file \texttt{ZF/simpdata.ML} for a sophisticated translation of
oheimb@5549
  1162
set-theoretic formulae into rewrite rules. 
oheimb@5549
  1163
oheimb@5549
  1164
For standard situations like the above,
oheimb@5549
  1165
there is a generic auxiliary function \ttindexbold{mk_atomize} that takes a 
oheimb@5549
  1166
list of pairs $(name, thms)$, where $name$ is an operator name and
oheimb@5549
  1167
$thms$ is a list of theorems to resolve with in case the pattern matches, 
oheimb@5549
  1168
and returns a suitable \texttt{atomize} function.
oheimb@5549
  1169
lcp@286
  1170
lcp@286
  1171
The simplified rewrites must now be converted into meta-equalities.  The
paulson@4597
  1172
rule \texttt{eq_reflection} converts equality rewrites, while {\tt
lcp@286
  1173
  iff_reflection} converts if-and-only-if rewrites.  The latter possibility
oheimb@11181
  1174
can arise in two other ways: the negative theorem~$\neg P$ is converted to
paulson@4597
  1175
$P\equiv\texttt{False}$, and any other theorem~$P$ is converted to
paulson@4597
  1176
$P\equiv\texttt{True}$.  The rules \texttt{iff_reflection_F} and {\tt
lcp@286
  1177
  iff_reflection_T} accomplish this conversion.
lcp@286
  1178
\begin{ttbox}
lcp@286
  1179
val P_iff_F = int_prove_fun "~P ==> (P <-> False)";
lcp@286
  1180
val iff_reflection_F = P_iff_F RS iff_reflection;
lcp@286
  1181
\ttbreak
lcp@286
  1182
val P_iff_T = int_prove_fun "P ==> (P <-> True)";
lcp@286
  1183
val iff_reflection_T = P_iff_T RS iff_reflection;
lcp@286
  1184
\end{ttbox}
oheimb@5549
  1185
The function \texttt{mk_eq} converts a theorem to a meta-equality
lcp@286
  1186
using the case analysis described above.
lcp@286
  1187
\begin{ttbox}
oheimb@5549
  1188
fun mk_eq th = case concl_of th of
lcp@286
  1189
    _ $ (Const("op =",_)$_$_)   => th RS eq_reflection
lcp@286
  1190
  | _ $ (Const("op <->",_)$_$_) => th RS iff_reflection
lcp@286
  1191
  | _ $ (Const("Not",_)$_)      => th RS iff_reflection_F
lcp@286
  1192
  | _                           => th RS iff_reflection_T;
lcp@286
  1193
\end{ttbox}
oheimb@11162
  1194
The 
wenzelm@12725
  1195
three functions \texttt{gen_all}, \texttt{atomize} and \texttt{mk_eq} 
oheimb@5549
  1196
will be composed together and supplied below to \texttt{setmksimps}.
lcp@286
  1197
lcp@286
  1198
lcp@286
  1199
\subsection{Making the initial simpset}
wenzelm@4395
  1200
wenzelm@9712
  1201
It is time to assemble these items.  The list \texttt{IFOL_simps} contains the
wenzelm@9712
  1202
default rewrite rules for intuitionistic first-order logic.  The first of
wenzelm@9712
  1203
these is the reflexive law expressed as the equivalence
wenzelm@9712
  1204
$(a=a)\bimp\texttt{True}$; the rewrite rule $a=a$ is clearly useless.
wenzelm@4395
  1205
\begin{ttbox}
wenzelm@4395
  1206
val IFOL_simps =
wenzelm@4395
  1207
   [refl RS P_iff_T] \at conj_simps \at disj_simps \at not_simps \at 
wenzelm@4395
  1208
    imp_simps \at iff_simps \at quant_simps;
lcp@286
  1209
\end{ttbox}
paulson@4597
  1210
The list \texttt{triv_rls} contains trivial theorems for the solver.  Any
lcp@286
  1211
subgoal that is simplified to one of these will be removed.
lcp@286
  1212
\begin{ttbox}
lcp@286
  1213
val notFalseI = int_prove_fun "~False";
lcp@286
  1214
val triv_rls = [TrueI,refl,iff_refl,notFalseI];
lcp@286
  1215
\end{ttbox}
wenzelm@9712
  1216
We also define the function \ttindex{mk_meta_cong} to convert the conclusion
wenzelm@9712
  1217
of congruence rules into meta-equalities.
wenzelm@9712
  1218
\begin{ttbox}
wenzelm@9712
  1219
fun mk_meta_cong rl = standard (mk_meta_eq (mk_meta_prems rl));
wenzelm@9712
  1220
\end{ttbox}
lcp@323
  1221
%
wenzelm@9695
  1222
The basic simpset for intuitionistic FOL is \ttindexbold{FOL_basic_ss}.  It
oheimb@11162
  1223
preprocess rewrites using 
wenzelm@12725
  1224
{\tt gen_all}, \texttt{atomize} and \texttt{mk_eq}.
wenzelm@9695
  1225
It solves simplified subgoals using \texttt{triv_rls} and assumptions, and by
wenzelm@9695
  1226
detecting contradictions.  It uses \ttindex{asm_simp_tac} to tackle subgoals
wenzelm@9695
  1227
of conditional rewrites.
wenzelm@4395
  1228
paulson@4597
  1229
Other simpsets built from \texttt{FOL_basic_ss} will inherit these items.
wenzelm@4395
  1230
In particular, \ttindexbold{IFOL_ss}, which introduces {\tt
wenzelm@4395
  1231
  IFOL_simps} as rewrite rules.  \ttindexbold{FOL_ss} will later
oheimb@11181
  1232
extend \texttt{IFOL_ss} with classical rewrite rules such as $\neg\neg
wenzelm@4395
  1233
P\bimp P$.
oheimb@2628
  1234
\index{*setmksimps}\index{*setSSolver}\index{*setSolver}\index{*setsubgoaler}
lcp@286
  1235
\index{*addsimps}\index{*addcongs}
lcp@286
  1236
\begin{ttbox}
wenzelm@4395
  1237
fun unsafe_solver prems = FIRST'[resolve_tac (triv_rls {\at} prems),
oheimb@2628
  1238
                                 atac, etac FalseE];
wenzelm@4395
  1239
paulson@8136
  1240
fun safe_solver prems = FIRST'[match_tac (triv_rls {\at} prems),
paulson@8136
  1241
                               eq_assume_tac, ematch_tac [FalseE]];
wenzelm@4395
  1242
wenzelm@9712
  1243
val FOL_basic_ss =
paulson@8136
  1244
      empty_ss setsubgoaler asm_simp_tac
paulson@8136
  1245
               addsimprocs [defALL_regroup, defEX_regroup]
paulson@8136
  1246
               setSSolver   safe_solver
paulson@8136
  1247
               setSolver  unsafe_solver
wenzelm@12725
  1248
               setmksimps (map mk_eq o atomize o gen_all)
wenzelm@9712
  1249
               setmkcong mk_meta_cong;
wenzelm@4395
  1250
paulson@8136
  1251
val IFOL_ss = 
paulson@8136
  1252
      FOL_basic_ss addsimps (IFOL_simps {\at} 
paulson@8136
  1253
                             int_ex_simps {\at} int_all_simps)
paulson@8136
  1254
                   addcongs [imp_cong];
lcp@286
  1255
\end{ttbox}
paulson@4597
  1256
This simpset takes \texttt{imp_cong} as a congruence rule in order to use
lcp@286
  1257
contextual information to simplify the conclusions of implications:
lcp@286
  1258
\[ \List{\Var{P}\bimp\Var{P'};\; \Var{P'} \Imp \Var{Q}\bimp\Var{Q'}} \Imp
lcp@286
  1259
   (\Var{P}\imp\Var{Q}) \bimp (\Var{P'}\imp\Var{Q'})
lcp@286
  1260
\]
paulson@4597
  1261
By adding the congruence rule \texttt{conj_cong}, we could obtain a similar
lcp@286
  1262
effect for conjunctions.
lcp@286
  1263
lcp@286
  1264
oheimb@5549
  1265
\subsection{Splitter setup}\index{simplification!setting up the splitter}
wenzelm@4557
  1266
oheimb@5549
  1267
To set up case splitting, we have to call the \ML{} functor \ttindex{
oheimb@5549
  1268
SplitterFun}, which takes the argument signature \texttt{SPLITTER_DATA}. 
oheimb@5549
  1269
So we prove the theorem \texttt{meta_eq_to_iff} below and store it, together
oheimb@5549
  1270
with the \texttt{mk_eq} function described above and several standard
oheimb@5549
  1271
theorems, in the structure \texttt{SplitterData}. Calling the functor with
oheimb@5549
  1272
this data yields a new instantiation of the splitter for our logic.
lcp@286
  1273
\begin{ttbox}
oheimb@5549
  1274
val meta_eq_to_iff = prove_goal IFOL.thy "x==y ==> x<->y"
oheimb@5549
  1275
  (fn [prem] => [rewtac prem, rtac iffI 1, atac 1, atac 1]);
lcp@286
  1276
\ttbreak
oheimb@5549
  1277
structure SplitterData =
oheimb@5549
  1278
  struct
oheimb@5549
  1279
  structure Simplifier = Simplifier
oheimb@5549
  1280
  val mk_eq          = mk_eq
oheimb@5549
  1281
  val meta_eq_to_iff = meta_eq_to_iff
oheimb@5549
  1282
  val iffD           = iffD2
oheimb@5549
  1283
  val disjE          = disjE
oheimb@5549
  1284
  val conjE          = conjE
oheimb@5549
  1285
  val exE            = exE
oheimb@5549
  1286
  val contrapos      = contrapos
oheimb@5549
  1287
  val contrapos2     = contrapos2
oheimb@5549
  1288
  val notnotD        = notnotD
oheimb@5549
  1289
  end;
oheimb@5549
  1290
\ttbreak
oheimb@5549
  1291
structure Splitter = SplitterFun(SplitterData);
lcp@286
  1292
\end{ttbox}
lcp@286
  1293
lcp@104
  1294
lcp@104
  1295
\index{simplification|)}
wenzelm@5370
  1296
wenzelm@5370
  1297
wenzelm@5370
  1298
%%% Local Variables: 
wenzelm@5370
  1299
%%% mode: latex
wenzelm@5370
  1300
%%% TeX-master: "ref"
wenzelm@5370
  1301
%%% End: