doc-src/Ref/simp.tex
author wenzelm
Mon Aug 28 13:52:38 2000 +0200 (2000-08-28)
changeset 9695 ec7d7f877712
parent 1100 74921c7613e7
child 11181 d04f57b91166
permissions -rw-r--r--
proper setup of iman.sty/extra.sty/ttbox.sty;
lcp@104
     1
%%%THIS DOCUMENTS THE OBSOLETE SIMPLIFIER!!!!
lcp@104
     2
\chapter{Simplification} \label{simp-chap}
lcp@104
     3
\index{simplification|(}
lcp@104
     4
Object-level rewriting is not primitive in Isabelle.  For efficiency,
lcp@104
     5
perhaps it ought to be.  On the other hand, it is difficult to conceive of
lcp@104
     6
a general mechanism that could accommodate the diversity of rewriting found
lcp@104
     7
in different logics.  Hence rewriting in Isabelle works via resolution,
lcp@104
     8
using unknowns as place-holders for simplified terms.  This chapter
lcp@104
     9
describes a generic simplification package, the functor~\ttindex{SimpFun},
lcp@104
    10
which expects the basic laws of equational logic and returns a suite of
lcp@104
    11
simplification tactics.  The code lives in
lcp@104
    12
\verb$Provers/simp.ML$.
lcp@104
    13
lcp@104
    14
This rewriting package is not as general as one might hope (using it for {\tt
lcp@104
    15
HOL} is not quite as convenient as it could be; rewriting modulo equations is
lcp@104
    16
not supported~\ldots) but works well for many logics.  It performs
lcp@104
    17
conditional and unconditional rewriting and handles multiple reduction
lcp@104
    18
relations and local assumptions.  It also has a facility for automatic case
lcp@104
    19
splits by expanding conditionals like {\it if-then-else\/} during rewriting.
lcp@104
    20
lcp@104
    21
For many of Isabelle's logics ({\tt FOL}, {\tt ZF}, {\tt LCF} and {\tt HOL})
lcp@104
    22
the simplifier has been set up already. Hence we start by describing the
lcp@104
    23
functions provided by the simplifier --- those functions exported by
lcp@104
    24
\ttindex{SimpFun} through its result signature \ttindex{SIMP} shown in
lcp@286
    25
Fig.\ts\ref{SIMP}.  
lcp@104
    26
lcp@104
    27
lcp@104
    28
\section{Simplification sets}
lcp@104
    29
\index{simplification sets}
lcp@104
    30
The simplification tactics are controlled by {\bf simpsets}, which consist of
lcp@104
    31
three things:
lcp@104
    32
\begin{enumerate}
lcp@104
    33
\item {\bf Rewrite rules}, which are theorems like 
lcp@104
    34
$\Var{m} + succ(\Var{n}) = succ(\Var{m} + \Var{n})$.  {\bf Conditional}
lcp@104
    35
rewrites such as $m<n \Imp m/n = 0$ are permitted.
lcp@104
    36
\index{rewrite rules}
lcp@104
    37
lcp@104
    38
\item {\bf Congruence rules}, which typically have the form
lcp@104
    39
\index{congruence rules}
lcp@104
    40
\[ \List{\Var{x@1} = \Var{y@1}; \ldots; \Var{x@n} = \Var{y@n}} \Imp
lcp@104
    41
   f(\Var{x@1},\ldots,\Var{x@n}) = f(\Var{y@1},\ldots,\Var{y@n}).
lcp@104
    42
\]
lcp@104
    43
lcp@104
    44
\item The {\bf auto-tactic}, which attempts to solve the simplified
lcp@104
    45
subgoal, say by recognizing it as a tautology.
lcp@104
    46
\end{enumerate}
lcp@104
    47
lcp@104
    48
\subsection{Congruence rules}
lcp@104
    49
Congruence rules enable the rewriter to simplify subterms.  Without a
lcp@104
    50
congruence rule for the function~$g$, no argument of~$g$ can be rewritten.
lcp@104
    51
Congruence rules can be generalized in the following ways:
lcp@104
    52
lcp@104
    53
{\bf Additional assumptions} are allowed:
lcp@104
    54
\[ \List{\Var{P@1} \bimp \Var{Q@1};\; \Var{Q@1} \Imp \Var{P@2} \bimp \Var{Q@2}}
lcp@104
    55
   \Imp (\Var{P@1} \imp \Var{P@2}) \bimp (\Var{Q@1} \imp \Var{Q@2})
lcp@104
    56
\]
lcp@104
    57
This rule assumes $Q@1$, and any rewrite rules it contains, while
lcp@1100
    58
simplifying~$P@2$.  Such `local' assumptions are effective for rewriting
lcp@104
    59
formulae such as $x=0\imp y+x=y$.
lcp@104
    60
lcp@104
    61
{\bf Additional quantifiers} are allowed, typically for binding operators:
lcp@104
    62
\[ \List{\Forall z. \Var{P}(z) \bimp \Var{Q}(z)} \Imp
lcp@104
    63
   \forall x.\Var{P}(x) \bimp \forall x.\Var{Q}(x)
lcp@104
    64
\]
lcp@104
    65
lcp@104
    66
{\bf Different equalities} can be mixed.  The following example
lcp@104
    67
enables the transition from formula rewriting to term rewriting:
lcp@104
    68
\[ \List{\Var{x@1}=\Var{y@1};\Var{x@2}=\Var{y@2}} \Imp
lcp@104
    69
   (\Var{x@1}=\Var{x@2}) \bimp (\Var{y@1}=\Var{y@2})
lcp@104
    70
\]
lcp@104
    71
\begin{warn}
lcp@104
    72
It is not necessary to assert a separate congruence rule for each constant,
lcp@104
    73
provided your logic contains suitable substitution rules. The function {\tt
lcp@104
    74
mk_congs} derives congruence rules from substitution
lcp@104
    75
rules~\S\ref{simp-tactics}.
lcp@104
    76
\end{warn}
lcp@104
    77
lcp@104
    78
lcp@104
    79
\begin{figure}
lcp@104
    80
\indexbold{*SIMP}
lcp@104
    81
\begin{ttbox}
lcp@104
    82
infix 4 addrews addcongs delrews delcongs setauto;
lcp@104
    83
signature SIMP =
lcp@104
    84
sig
lcp@104
    85
  type simpset
lcp@104
    86
  val empty_ss  : simpset
lcp@104
    87
  val addcongs  : simpset * thm list -> simpset
lcp@104
    88
  val addrews   : simpset * thm list -> simpset
lcp@104
    89
  val delcongs  : simpset * thm list -> simpset
lcp@104
    90
  val delrews   : simpset * thm list -> simpset
lcp@104
    91
  val print_ss  : simpset -> unit
lcp@104
    92
  val setauto   : simpset * (int -> tactic) -> simpset
lcp@104
    93
  val ASM_SIMP_CASE_TAC : simpset -> int -> tactic
lcp@104
    94
  val ASM_SIMP_TAC      : simpset -> int -> tactic
lcp@104
    95
  val CASE_TAC          : simpset -> int -> tactic
lcp@104
    96
  val SIMP_CASE2_TAC    : simpset -> int -> tactic
lcp@104
    97
  val SIMP_THM          : simpset -> thm -> thm
lcp@104
    98
  val SIMP_TAC          : simpset -> int -> tactic
lcp@104
    99
  val SIMP_CASE_TAC     : simpset -> int -> tactic
lcp@104
   100
  val mk_congs          : theory -> string list -> thm list
lcp@104
   101
  val mk_typed_congs    : theory -> (string*string) list -> thm list
lcp@104
   102
  val tracing   : bool ref
lcp@104
   103
end;
lcp@104
   104
\end{ttbox}
lcp@104
   105
\caption{The signature {\tt SIMP}} \label{SIMP}
lcp@104
   106
\end{figure}
lcp@104
   107
lcp@104
   108
lcp@104
   109
\subsection{The abstract type {\tt simpset}}\label{simp-simpsets}
lcp@104
   110
Simpsets are values of the abstract type \ttindexbold{simpset}.  They are
lcp@104
   111
manipulated by the following functions:
lcp@104
   112
\index{simplification sets|bold}
lcp@323
   113
\begin{ttdescription}
lcp@104
   114
\item[\ttindexbold{empty_ss}] 
lcp@104
   115
is the empty simpset.  It has no congruence or rewrite rules and its
lcp@104
   116
auto-tactic always fails.
lcp@104
   117
lcp@323
   118
\item[$ss$ \ttindexbold{addcongs} $thms$] 
lcp@104
   119
is the simpset~$ss$ plus the congruence rules~$thms$.
lcp@104
   120
lcp@323
   121
\item[$ss$ \ttindexbold{delcongs} $thms$] 
lcp@104
   122
is the simpset~$ss$ minus the congruence rules~$thms$.
lcp@104
   123
lcp@323
   124
\item[$ss$ \ttindexbold{addrews} $thms$] 
lcp@104
   125
is the simpset~$ss$ plus the rewrite rules~$thms$.
lcp@104
   126
lcp@323
   127
\item[$ss$ \ttindexbold{delrews} $thms$] 
lcp@104
   128
is the simpset~$ss$ minus the rewrite rules~$thms$.
lcp@104
   129
lcp@323
   130
\item[$ss$ \ttindexbold{setauto} $tacf$] 
lcp@104
   131
is the simpset~$ss$ with $tacf$ for its auto-tactic.
lcp@104
   132
lcp@104
   133
\item[\ttindexbold{print_ss} $ss$] 
lcp@104
   134
prints all the congruence and rewrite rules in the simpset~$ss$.
lcp@323
   135
\end{ttdescription}
lcp@104
   136
Adding a rule to a simpset already containing it, or deleting one
lcp@104
   137
from a simpset not containing it, generates a warning message.
lcp@104
   138
lcp@104
   139
In principle, any theorem can be used as a rewrite rule.  Before adding a
lcp@104
   140
theorem to a simpset, {\tt addrews} preprocesses the theorem to extract the
lcp@104
   141
maximum amount of rewriting from it.  Thus it need not have the form $s=t$.
lcp@104
   142
In {\tt FOL} for example, an atomic formula $P$ is transformed into the
lcp@104
   143
rewrite rule $P \bimp True$.  This preprocessing is not fixed but logic
lcp@104
   144
dependent.  The existing logics like {\tt FOL} are fairly clever in this
lcp@104
   145
respect.  For a more precise description see {\tt mk_rew_rules} in
lcp@104
   146
\S\ref{SimpFun-input}.  
lcp@104
   147
lcp@104
   148
The auto-tactic is applied after simplification to solve a goal.  This may
lcp@104
   149
be the overall goal or some subgoal that arose during conditional
lcp@104
   150
rewriting.  Calling ${\tt auto_tac}~i$ must either solve exactly
lcp@104
   151
subgoal~$i$ or fail.  If it succeeds without reducing the number of
lcp@104
   152
subgoals by one, havoc and strange exceptions may result.
lcp@104
   153
A typical auto-tactic is {\tt ares_tac [TrueI]}, which attempts proof by
lcp@104
   154
assumption and resolution with the theorem $True$.  In explicitly typed
lcp@104
   155
logics, the auto-tactic can be used to solve simple type checking
lcp@104
   156
obligations.  Some applications demand a sophisticated auto-tactic such as
lcp@104
   157
{\tt fast_tac}, but this could make simplification slow.
lcp@104
   158
  
lcp@104
   159
\begin{warn}
lcp@104
   160
Rewriting never instantiates unknowns in subgoals.  (It uses
lcp@104
   161
\ttindex{match_tac} rather than \ttindex{resolve_tac}.)  However, the
lcp@104
   162
auto-tactic is permitted to instantiate unknowns.
lcp@104
   163
\end{warn}
lcp@104
   164
lcp@104
   165
lcp@104
   166
\section{The simplification tactics} \label{simp-tactics}
lcp@104
   167
\index{simplification!tactics|bold}
lcp@104
   168
\index{tactics!simplification|bold}
lcp@104
   169
The actual simplification work is performed by the following tactics.  The
lcp@104
   170
rewriting strategy is strictly bottom up.  Conditions in conditional rewrite
lcp@104
   171
rules are solved recursively before the rewrite rule is applied.
lcp@104
   172
lcp@104
   173
There are two basic simplification tactics:
lcp@323
   174
\begin{ttdescription}
lcp@104
   175
\item[\ttindexbold{SIMP_TAC} $ss$ $i$] 
lcp@104
   176
simplifies subgoal~$i$ using the rules in~$ss$.  It may solve the
lcp@104
   177
subgoal completely if it has become trivial, using the auto-tactic
lcp@104
   178
(\S\ref{simp-simpsets}).
lcp@104
   179
  
lcp@104
   180
\item[\ttindexbold{ASM_SIMP_TAC}] 
lcp@104
   181
is like \verb$SIMP_TAC$, but also uses assumptions as additional
lcp@104
   182
rewrite rules.
lcp@323
   183
\end{ttdescription}
lcp@104
   184
Many logics have conditional operators like {\it if-then-else}.  If the
lcp@104
   185
simplifier has been set up with such case splits (see~\ttindex{case_splits}
lcp@104
   186
in \S\ref{SimpFun-input}), there are tactics which automatically alternate
lcp@104
   187
between simplification and case splitting:
lcp@323
   188
\begin{ttdescription}
lcp@104
   189
\item[\ttindexbold{SIMP_CASE_TAC}] 
lcp@104
   190
is like {\tt SIMP_TAC} but also performs automatic case splits.
lcp@104
   191
More precisely, after each simplification phase the tactic tries to apply a
lcp@104
   192
theorem in \ttindex{case_splits}.  If this succeeds, the tactic calls
lcp@104
   193
itself recursively on the result.
lcp@104
   194
lcp@104
   195
\item[\ttindexbold{ASM_SIMP_CASE_TAC}] 
lcp@104
   196
is like {\tt SIMP_CASE_TAC}, but also uses assumptions for
lcp@104
   197
rewriting.
lcp@104
   198
lcp@104
   199
\item[\ttindexbold{SIMP_CASE2_TAC}] 
lcp@104
   200
is like {\tt SIMP_CASE_TAC}, but also tries to solve the
lcp@104
   201
pre-conditions of conditional simplification rules by repeated case splits.
lcp@104
   202
lcp@104
   203
\item[\ttindexbold{CASE_TAC}] 
lcp@104
   204
tries to break up a goal using a rule in
lcp@104
   205
\ttindex{case_splits}.
lcp@104
   206
lcp@104
   207
\item[\ttindexbold{SIMP_THM}] 
lcp@104
   208
simplifies a theorem using assumptions and case splitting.
lcp@323
   209
\end{ttdescription}
lcp@104
   210
Finally there are two useful functions for generating congruence
lcp@104
   211
rules for constants and free variables:
lcp@323
   212
\begin{ttdescription}
lcp@104
   213
\item[\ttindexbold{mk_congs} $thy$ $cs$] 
lcp@104
   214
computes a list of congruence rules, one for each constant in $cs$.
lcp@104
   215
Remember that the name of an infix constant
lcp@104
   216
\verb$+$ is \verb$op +$.
lcp@104
   217
lcp@104
   218
\item[\ttindexbold{mk_typed_congs}] 
lcp@104
   219
computes congruence rules for explicitly typed free variables and
lcp@104
   220
constants.  Its second argument is a list of name and type pairs.  Names
lcp@104
   221
can be either free variables like {\tt P}, or constants like \verb$op =$.
lcp@104
   222
For example in {\tt FOL}, the pair
lcp@104
   223
\verb$("f","'a => 'a")$ generates the rule \verb$?x = ?y ==> f(?x) = f(?y)$.
lcp@104
   224
Such congruence rules are necessary for goals with free variables whose
lcp@104
   225
arguments need to be rewritten.
lcp@323
   226
\end{ttdescription}
lcp@104
   227
Both functions work correctly only if {\tt SimpFun} has been supplied with the
lcp@104
   228
necessary substitution rules.  The details are discussed in
lcp@104
   229
\S\ref{SimpFun-input} under {\tt subst_thms}.
lcp@104
   230
\begin{warn}
lcp@104
   231
Using the simplifier effectively may take a bit of experimentation. In
lcp@104
   232
particular it may often happen that simplification stops short of what you
lcp@104
   233
expected or runs forever. To diagnose these problems, the simplifier can be
lcp@104
   234
traced. The reference variable \ttindexbold{tracing} controls the output of
lcp@104
   235
tracing information.
lcp@104
   236
\index{tracing!of simplification}
lcp@104
   237
\end{warn}
lcp@104
   238
lcp@104
   239
lcp@104
   240
\section{Example: using the simplifier}
lcp@104
   241
\index{simplification!example}
lcp@104
   242
Assume we are working within {\tt FOL} and that
lcp@323
   243
\begin{ttdescription}
lcp@323
   244
\item[Nat.thy] is a theory including the constants $0$, $Suc$ and $+$,
lcp@323
   245
\item[add_0] is the rewrite rule $0+n = n$,
lcp@323
   246
\item[add_Suc] is the rewrite rule $Suc(m)+n = Suc(m+n)$,
lcp@323
   247
\item[induct] is the induction rule
lcp@104
   248
$\List{P(0); \Forall x. P(x)\Imp P(Suc(x))} \Imp P(n)$.
lcp@323
   249
\item[FOL_ss] is a basic simpset for {\tt FOL}.
lcp@323
   250
\end{ttdescription}
lcp@104
   251
We generate congruence rules for $Suc$ and for the infix operator~$+$:
lcp@104
   252
\begin{ttbox}
lcp@104
   253
val nat_congs = mk_congs Nat.thy ["Suc", "op +"];
lcp@104
   254
prths nat_congs;
lcp@104
   255
{\out ?Xa = ?Ya ==> Suc(?Xa) = Suc(?Ya)}
lcp@104
   256
{\out [| ?Xa = ?Ya; ?Xb = ?Yb |] ==> ?Xa + ?Xb = ?Ya + ?Yb}
lcp@104
   257
\end{ttbox}
lcp@104
   258
We create a simpset for natural numbers by extending~{\tt FOL_ss}:
lcp@104
   259
\begin{ttbox}
lcp@104
   260
val add_ss = FOL_ss  addcongs nat_congs  
lcp@104
   261
                     addrews  [add_0, add_Suc];
lcp@104
   262
\end{ttbox}
lcp@104
   263
Proofs by induction typically involve simplification:\footnote
lcp@104
   264
{These examples reside on the file {\tt FOL/ex/nat.ML}.} 
lcp@104
   265
\begin{ttbox}
lcp@104
   266
goal Nat.thy "m+0 = m";
lcp@104
   267
{\out Level 0}
lcp@104
   268
{\out m + 0 = m}
lcp@104
   269
{\out  1. m + 0 = m}
lcp@104
   270
\ttbreak
lcp@104
   271
by (res_inst_tac [("n","m")] induct 1);
lcp@104
   272
{\out Level 1}
lcp@104
   273
{\out m + 0 = m}
lcp@104
   274
{\out  1. 0 + 0 = 0}
lcp@104
   275
{\out  2. !!x. x + 0 = x ==> Suc(x) + 0 = Suc(x)}
lcp@104
   276
\end{ttbox}
lcp@104
   277
Simplification solves the first subgoal:
lcp@104
   278
\begin{ttbox}
lcp@104
   279
by (SIMP_TAC add_ss 1);
lcp@104
   280
{\out Level 2}
lcp@104
   281
{\out m + 0 = m}
lcp@104
   282
{\out  1. !!x. x + 0 = x ==> Suc(x) + 0 = Suc(x)}
lcp@104
   283
\end{ttbox}
lcp@104
   284
The remaining subgoal requires \ttindex{ASM_SIMP_TAC} in order to use the
lcp@104
   285
induction hypothesis as a rewrite rule:
lcp@104
   286
\begin{ttbox}
lcp@104
   287
by (ASM_SIMP_TAC add_ss 1);
lcp@104
   288
{\out Level 3}
lcp@104
   289
{\out m + 0 = m}
lcp@104
   290
{\out No subgoals!}
lcp@104
   291
\end{ttbox}
lcp@104
   292
The next proof is similar.
lcp@104
   293
\begin{ttbox}
lcp@104
   294
goal Nat.thy "m+Suc(n) = Suc(m+n)";
lcp@104
   295
{\out Level 0}
lcp@104
   296
{\out m + Suc(n) = Suc(m + n)}
lcp@104
   297
{\out  1. m + Suc(n) = Suc(m + n)}
lcp@104
   298
\ttbreak
lcp@104
   299
by (res_inst_tac [("n","m")] induct 1);
lcp@104
   300
{\out Level 1}
lcp@104
   301
{\out m + Suc(n) = Suc(m + n)}
lcp@104
   302
{\out  1. 0 + Suc(n) = Suc(0 + n)}
lcp@104
   303
{\out  2. !!x. x + Suc(n) = Suc(x + n) ==> Suc(x) + Suc(n) = Suc(Suc(x) + n)}
lcp@104
   304
\end{ttbox}
lcp@104
   305
Using the tactical \ttindex{ALLGOALS}, a single command simplifies all the
lcp@104
   306
subgoals:
lcp@104
   307
\begin{ttbox}
lcp@104
   308
by (ALLGOALS (ASM_SIMP_TAC add_ss));
lcp@104
   309
{\out Level 2}
lcp@104
   310
{\out m + Suc(n) = Suc(m + n)}
lcp@104
   311
{\out No subgoals!}
lcp@104
   312
\end{ttbox}
lcp@104
   313
Some goals contain free function variables.  The simplifier must have
lcp@104
   314
congruence rules for those function variables, or it will be unable to
lcp@104
   315
simplify their arguments:
lcp@104
   316
\begin{ttbox}
lcp@104
   317
val f_congs = mk_typed_congs Nat.thy [("f","nat => nat")];
lcp@104
   318
val f_ss = add_ss addcongs f_congs;
lcp@104
   319
prths f_congs;
lcp@104
   320
{\out ?Xa = ?Ya ==> f(?Xa) = f(?Ya)}
lcp@104
   321
\end{ttbox}
lcp@104
   322
Here is a conjecture to be proved for an arbitrary function~$f$ satisfying
lcp@104
   323
the law $f(Suc(n)) = Suc(f(n))$:
lcp@104
   324
\begin{ttbox}
lcp@104
   325
val [prem] = goal Nat.thy
lcp@104
   326
    "(!!n. f(Suc(n)) = Suc(f(n))) ==> f(i+j) = i+f(j)";
lcp@104
   327
{\out Level 0}
lcp@104
   328
{\out f(i + j) = i + f(j)}
lcp@104
   329
{\out  1. f(i + j) = i + f(j)}
lcp@104
   330
\ttbreak
lcp@104
   331
by (res_inst_tac [("n","i")] induct 1);
lcp@104
   332
{\out Level 1}
lcp@104
   333
{\out f(i + j) = i + f(j)}
lcp@104
   334
{\out  1. f(0 + j) = 0 + f(j)}
lcp@104
   335
{\out  2. !!x. f(x + j) = x + f(j) ==> f(Suc(x) + j) = Suc(x) + f(j)}
lcp@104
   336
\end{ttbox}
lcp@104
   337
We simplify each subgoal in turn.  The first one is trivial:
lcp@104
   338
\begin{ttbox}
lcp@104
   339
by (SIMP_TAC f_ss 1);
lcp@104
   340
{\out Level 2}
lcp@104
   341
{\out f(i + j) = i + f(j)}
lcp@104
   342
{\out  1. !!x. f(x + j) = x + f(j) ==> f(Suc(x) + j) = Suc(x) + f(j)}
lcp@104
   343
\end{ttbox}
lcp@104
   344
The remaining subgoal requires rewriting by the premise, shown
lcp@104
   345
below, so we add it to {\tt f_ss}:
lcp@104
   346
\begin{ttbox}
lcp@104
   347
prth prem;
lcp@104
   348
{\out f(Suc(?n)) = Suc(f(?n))  [!!n. f(Suc(n)) = Suc(f(n))]}
lcp@104
   349
by (ASM_SIMP_TAC (f_ss addrews [prem]) 1);
lcp@104
   350
{\out Level 3}
lcp@104
   351
{\out f(i + j) = i + f(j)}
lcp@104
   352
{\out No subgoals!}
lcp@104
   353
\end{ttbox}
lcp@104
   354
lcp@104
   355
lcp@104
   356
\section{Setting up the simplifier} \label{SimpFun-input}
lcp@104
   357
\index{simplification!setting up|bold}
lcp@104
   358
To set up a simplifier for a new logic, the \ML\ functor
lcp@104
   359
\ttindex{SimpFun} needs to be supplied with theorems to justify
lcp@104
   360
rewriting.  A rewrite relation must be reflexive and transitive; symmetry
lcp@104
   361
is not necessary.  Hence the package is also applicable to non-symmetric
lcp@104
   362
relations such as occur in operational semantics.  In the sequel, $\gg$
lcp@104
   363
denotes some {\bf reduction relation}: a binary relation to be used for
lcp@104
   364
rewriting.  Several reduction relations can be used at once.  In {\tt FOL},
lcp@104
   365
both $=$ (on terms) and $\bimp$ (on formulae) can be used for rewriting.
lcp@104
   366
lcp@104
   367
The argument to {\tt SimpFun} is a structure with signature
lcp@104
   368
\ttindexbold{SIMP_DATA}:
lcp@104
   369
\begin{ttbox}
lcp@104
   370
signature SIMP_DATA =
lcp@104
   371
sig
lcp@104
   372
  val case_splits  : (thm * string) list
lcp@104
   373
  val dest_red     : term -> term * term * term
lcp@104
   374
  val mk_rew_rules : thm -> thm list
lcp@104
   375
  val norm_thms    : (thm*thm) list
lcp@104
   376
  val red1         : thm
lcp@104
   377
  val red2         : thm 
lcp@104
   378
  val refl_thms    : thm list
lcp@104
   379
  val subst_thms   : thm list 
lcp@104
   380
  val trans_thms   : thm list
lcp@104
   381
end;
lcp@104
   382
\end{ttbox}
lcp@104
   383
The components of {\tt SIMP_DATA} need to be instantiated as follows.  Many
lcp@104
   384
of these components are lists, and can be empty.
lcp@323
   385
\begin{ttdescription}
lcp@104
   386
\item[\ttindexbold{refl_thms}] 
lcp@104
   387
supplies reflexivity theorems of the form $\Var{x} \gg
lcp@104
   388
\Var{x}$.  They must not have additional premises as, for example,
lcp@104
   389
$\Var{x}\in\Var{A} \Imp \Var{x} = \Var{x}\in\Var{A}$ in type theory.
lcp@104
   390
lcp@104
   391
\item[\ttindexbold{trans_thms}] 
lcp@104
   392
supplies transitivity theorems of the form
lcp@104
   393
$\List{\Var{x}\gg\Var{y}; \Var{y}\gg\Var{z}} \Imp {\Var{x}\gg\Var{z}}$.
lcp@104
   394
lcp@104
   395
\item[\ttindexbold{red1}] 
lcp@104
   396
is a theorem of the form $\List{\Var{P}\gg\Var{Q};
lcp@104
   397
\Var{P}} \Imp \Var{Q}$, where $\gg$ is a relation between formulae, such as
lcp@104
   398
$\bimp$ in {\tt FOL}.
lcp@104
   399
lcp@104
   400
\item[\ttindexbold{red2}] 
lcp@104
   401
is a theorem of the form $\List{\Var{P}\gg\Var{Q};
lcp@104
   402
\Var{Q}} \Imp \Var{P}$, where $\gg$ is a relation between formulae, such as
lcp@104
   403
$\bimp$ in {\tt FOL}.
lcp@104
   404
lcp@104
   405
\item[\ttindexbold{mk_rew_rules}] 
lcp@104
   406
is a function that extracts rewrite rules from theorems.  A rewrite rule is
lcp@104
   407
a theorem of the form $\List{\ldots}\Imp s \gg t$.  In its simplest form,
lcp@104
   408
{\tt mk_rew_rules} maps a theorem $t$ to the singleton list $[t]$.  More
lcp@104
   409
sophisticated versions may do things like
lcp@104
   410
\[
lcp@104
   411
\begin{array}{l@{}r@{\quad\mapsto\quad}l}
lcp@104
   412
\mbox{create formula rewrites:}& P & [P \bimp True] \\[.5ex]
lcp@104
   413
\mbox{remove negations:}& \lnot P & [P \bimp False] \\[.5ex]
lcp@104
   414
\mbox{create conditional rewrites:}& P\imp s\gg t & [P\Imp s\gg t] \\[.5ex]
lcp@104
   415
\mbox{break up conjunctions:}& 
lcp@104
   416
        (s@1 \gg@1 t@1) \conj (s@2 \gg@2 t@2) & [s@1 \gg@1 t@1, s@2 \gg@2 t@2]
lcp@104
   417
\end{array}
lcp@104
   418
\]
lcp@104
   419
The more theorems are turned into rewrite rules, the better.  The function
lcp@104
   420
is used in two places:
lcp@104
   421
\begin{itemize}
lcp@104
   422
\item 
lcp@104
   423
$ss$~\ttindex{addrews}~$thms$ applies {\tt mk_rew_rules} to each element of
lcp@104
   424
$thms$ before adding it to $ss$.
lcp@104
   425
\item 
lcp@104
   426
simplification with assumptions (as in \ttindex{ASM_SIMP_TAC}) uses
lcp@104
   427
{\tt mk_rew_rules} to turn assumptions into rewrite rules.
lcp@104
   428
\end{itemize}
lcp@104
   429
lcp@104
   430
\item[\ttindexbold{case_splits}] 
lcp@104
   431
supplies expansion rules for case splits.  The simplifier is designed
lcp@104
   432
for rules roughly of the kind
lcp@104
   433
\[ \Var{P}(if(\Var{Q},\Var{x},\Var{y})) \bimp (\Var{Q} \imp \Var{P}(\Var{x}))
lcp@104
   434
\conj (\lnot\Var{Q} \imp \Var{P}(\Var{y})) 
lcp@104
   435
\] 
lcp@104
   436
but is insensitive to the form of the right-hand side.  Other examples
lcp@104
   437
include product types, where $split ::
lcp@104
   438
(\alpha\To\beta\To\gamma)\To\alpha*\beta\To\gamma$:
lcp@104
   439
\[ \Var{P}(split(\Var{f},\Var{p})) \bimp (\forall a~b. \Var{p} =
lcp@104
   440
{<}a,b{>} \imp \Var{P}(\Var{f}(a,b))) 
lcp@104
   441
\] 
lcp@104
   442
Each theorem in the list is paired with the name of the constant being
lcp@104
   443
eliminated, {\tt"if"} and {\tt"split"} in the examples above.
lcp@104
   444
lcp@104
   445
\item[\ttindexbold{norm_thms}] 
lcp@104
   446
supports an optimization.  It should be a list of pairs of rules of the
lcp@104
   447
form $\Var{x} \gg norm(\Var{x})$ and $norm(\Var{x}) \gg \Var{x}$.  These
lcp@104
   448
introduce and eliminate {\tt norm}, an arbitrary function that should be
lcp@104
   449
used nowhere else.  This function serves to tag subterms that are in normal
lcp@104
   450
form.  Such rules can speed up rewriting significantly!
lcp@104
   451
lcp@104
   452
\item[\ttindexbold{subst_thms}] 
lcp@104
   453
supplies substitution rules of the form
lcp@104
   454
\[ \List{\Var{x} \gg \Var{y}; \Var{P}(\Var{x})} \Imp \Var{P}(\Var{y}) \]
lcp@104
   455
They are used to derive congruence rules via \ttindex{mk_congs} and
lcp@104
   456
\ttindex{mk_typed_congs}.  If $f :: [\tau@1,\cdots,\tau@n]\To\tau$ is a
lcp@104
   457
constant or free variable, the computation of a congruence rule
lcp@104
   458
\[\List{\Var{x@1} \gg@1 \Var{y@1}; \ldots; \Var{x@n} \gg@n \Var{y@n}}
lcp@104
   459
\Imp f(\Var{x@1},\ldots,\Var{x@n}) \gg f(\Var{y@1},\ldots,\Var{y@n}) \]
lcp@104
   460
requires a reflexivity theorem for some reduction ${\gg} ::
lcp@104
   461
\alpha\To\alpha\To\sigma$ such that $\tau$ is an instance of $\alpha$.  If a
lcp@104
   462
suitable reflexivity law is missing, no congruence rule for $f$ can be
lcp@104
   463
generated.   Otherwise an $n$-ary congruence rule of the form shown above is
lcp@104
   464
derived, subject to the availability of suitable substitution laws for each
lcp@104
   465
argument position.  
lcp@104
   466
lcp@104
   467
A substitution law is suitable for argument $i$ if it
lcp@104
   468
uses a reduction ${\gg@i} :: \alpha@i\To\alpha@i\To\sigma@i$ such that
lcp@104
   469
$\tau@i$ is an instance of $\alpha@i$.  If a suitable substitution law for
lcp@104
   470
argument $i$ is missing, the $i^{th}$ premise of the above congruence rule
lcp@104
   471
cannot be generated and hence argument $i$ cannot be rewritten.  In the
lcp@104
   472
worst case, if there are no suitable substitution laws at all, the derived
lcp@104
   473
congruence simply degenerates into a reflexivity law.
lcp@104
   474
lcp@104
   475
\item[\ttindexbold{dest_red}] 
lcp@104
   476
takes reductions apart.  Given a term $t$ representing the judgement
lcp@104
   477
\mbox{$a \gg b$}, \verb$dest_red$~$t$ should return a triple $(c,ta,tb)$
lcp@104
   478
where $ta$ and $tb$ represent $a$ and $b$, and $c$ is a term of the form
lcp@104
   479
\verb$Const(_,_)$, the reduction constant $\gg$.  
lcp@104
   480
lcp@104
   481
Suppose the logic has a coercion function like $Trueprop::o\To prop$, as do
lcp@104
   482
{\tt FOL} and~{\tt HOL}\@.  If $\gg$ is a binary operator (not necessarily
lcp@104
   483
infix), the following definition does the job:
lcp@104
   484
\begin{verbatim}
lcp@104
   485
   fun dest_red( _ $ (c $ ta $ tb) ) = (c,ta,tb);
lcp@104
   486
\end{verbatim}
lcp@104
   487
The wildcard pattern {\tt_} matches the coercion function.
lcp@323
   488
\end{ttdescription}
lcp@104
   489
lcp@104
   490
lcp@104
   491
\section{A sample instantiation}
wenzelm@9695
   492
Here is the instantiation of {\tt SIMP_DATA} for FOL.  The code for {\tt
wenzelm@9695
   493
  mk_rew_rules} is not shown; see the file {\tt FOL/simpdata.ML}.
lcp@104
   494
\begin{ttbox}
lcp@104
   495
structure FOL_SimpData : SIMP_DATA =
lcp@104
   496
  struct
lcp@104
   497
  val refl_thms      = [ \(\Var{x}=\Var{x}\), \(\Var{P}\bimp\Var{P}\) ]
lcp@104
   498
  val trans_thms     = [ \(\List{\Var{x}=\Var{y};\Var{y}=\Var{z}}\Imp\Var{x}=\Var{z}\),
lcp@104
   499
                         \(\List{\Var{P}\bimp\Var{Q};\Var{Q}\bimp\Var{R}}\Imp\Var{P}\bimp\Var{R}\) ]
lcp@104
   500
  val red1           = \(\List{\Var{P}\bimp\Var{Q}; \Var{P}} \Imp \Var{Q}\)
lcp@104
   501
  val red2           = \(\List{\Var{P}\bimp\Var{Q}; \Var{Q}} \Imp \Var{P}\)
lcp@104
   502
  val mk_rew_rules   = ...
lcp@104
   503
  val case_splits    = [ \(\Var{P}(if(\Var{Q},\Var{x},\Var{y})) \bimp\)
lcp@104
   504
                           \((\Var{Q} \imp \Var{P}(\Var{x})) \conj (\lnot\Var{Q} \imp \Var{P}(\Var{y}))\) ]
lcp@104
   505
  val norm_thms      = [ (\(\Var{x}=norm(\Var{x})\),\(norm(\Var{x})=\Var{x}\)),
lcp@104
   506
                        (\(\Var{P}\bimp{}NORM(\Var{P}\)), \(NORM(\Var{P})\bimp\Var{P}\)) ]
lcp@104
   507
  val subst_thms     = [ \(\List{\Var{x}=\Var{y}; \Var{P}(\Var{x})}\Imp\Var{P}(\Var{y})\) ]
lcp@104
   508
  val dest_red       = fn (_ $ (opn $ lhs $ rhs)) => (opn,lhs,rhs)
lcp@104
   509
  end;
lcp@104
   510
\end{ttbox}
lcp@104
   511
lcp@104
   512
\index{simplification|)}