author | wenzelm |
Thu, 17 Jul 1997 15:03:38 +0200 | |
changeset 3524 | c02cb15830de |
parent 3485 | f27a30a18a17 |
child 3950 | e9d5bcae8351 |
permissions | -rw-r--r-- |
104 | 1 |
%% $Id$ |
2 |
\chapter{Simplification} \label{simp-chap} |
|
3 |
\index{simplification|(} |
|
4 |
||
5 |
This chapter describes Isabelle's generic simplification package, which |
|
323 | 6 |
provides a suite of simplification tactics. It performs conditional and |
7 |
unconditional rewriting and uses contextual information (`local |
|
8 |
assumptions'). It provides a few general hooks, which can provide |
|
9 |
automatic case splits during rewriting, for example. The simplifier is set |
|
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
10 |
up for many of Isabelle's logics: \FOL, \ZF, \HOL\ and \HOLCF. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
11 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
12 |
The next section is a quick introduction to the simplifier, the later |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
13 |
sections explain advanced features. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
14 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
15 |
\section{Simplification for dummies} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
16 |
\label{sec:simp-for-dummies} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
17 |
|
2479 | 18 |
In some logics (\FOL, {\HOL} and \ZF), the simplifier is particularly easy to |
19 |
use because it supports the concept of a {\em current |
|
20 |
simpset}\index{simpset!current}. This is a default set of simplification |
|
21 |
rules. All commands are interpreted relative to the current simpset. For |
|
22 |
example, in the theory of arithmetic the goal |
|
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
23 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
24 |
{\out 1. 0 + (x + 0) = x + 0 + 0} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
25 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
26 |
can be solved by a single |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
27 |
\begin{ttbox} |
2479 | 28 |
by (Simp_tac 1); |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
29 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
30 |
The simplifier uses the current simpset, which in the case of arithmetic |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
31 |
contains the required theorems $\Var{n}+0 = \Var{n}$ and $0+\Var{n} = |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
32 |
\Var{n}$. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
33 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
34 |
If assumptions of the subgoal are also needed in the simplification |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
35 |
process, as in |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
36 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
37 |
{\out 1. x = 0 ==> x + x = 0} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
38 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
39 |
then there is the more powerful |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
40 |
\begin{ttbox} |
2479 | 41 |
by (Asm_simp_tac 1); |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
42 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
43 |
which solves the above goal. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
44 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
45 |
There are four basic simplification tactics: |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
46 |
\begin{ttdescription} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
47 |
\item[\ttindexbold{Simp_tac} $i$] simplifies subgoal~$i$ using the current |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
48 |
simpset. It may solve the subgoal completely if it has become trivial, |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
49 |
using the solver. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
50 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
51 |
\item[\ttindexbold{Asm_simp_tac}]\index{assumptions!in simplification} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
52 |
is like \verb$Simp_tac$, but extracts additional rewrite rules from the |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
53 |
assumptions. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
54 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
55 |
\item[\ttindexbold{Full_simp_tac}] is like \verb$Simp_tac$, but also |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
56 |
simplifies the assumptions (but without using the assumptions to simplify |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
57 |
each other or the actual goal.) |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
58 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
59 |
\item[\ttindexbold{Asm_full_simp_tac}] |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
60 |
is like \verb$Asm_simp_tac$, but also simplifies the assumptions one by |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
61 |
one. {\em Working from left to right, it uses each assumption in the |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
62 |
simplification of those following.} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
63 |
\end{ttdescription} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
64 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
65 |
{\tt Asm_full_simp_tac} is the most powerful of this quartet but may also |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
66 |
loop where some of the others terminate. For example, |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
67 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
68 |
{\out 1. ALL x. f(x) = g(f(g(x))) ==> f(0) = f(0)+0} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
69 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
70 |
is solved by {\tt Simp_tac}, but {\tt Asm_simp_tac} and {\tt Asm_simp_tac} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
71 |
loop because the rewrite rule $f(\Var{x}) = f(g(f(\Var{x})))$ extracted from |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
72 |
the assumption does not terminate. Isabelle notices certain simple forms of |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
73 |
nontermination, but not this one. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
74 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
75 |
\begin{warn} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
76 |
Since \verb$Asm_full_simp_tac$ works from left to right, it sometimes |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
77 |
misses opportunities for simplification: given the subgoal |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
78 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
79 |
{\out [| P(f(a)); f(a) = t |] ==> ...} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
80 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
81 |
\verb$Asm_full_simp_tac$ will not simplify the first assumption using the |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
82 |
second but will leave the assumptions unchanged. See |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
83 |
\S\ref{sec:reordering-asms} for ways around this problem. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
84 |
\end{warn} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
85 |
|
3108 | 86 |
Using the simplifier effectively may take a bit of experimentation. |
87 |
\index{tracing!of simplification}\index{*trace_simp} The tactics can |
|
88 |
be traced by setting \verb$trace_simp := true$. |
|
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
89 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
90 |
There is not just one global current simpset, but one associated with each |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
91 |
theory as well. How are these simpset built up? |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
92 |
\begin{enumerate} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
93 |
\item When loading {\tt T.thy}, the current simpset is initialized with the |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
94 |
union of the simpsets associated with all the ancestors of {\tt T}. In |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
95 |
addition, certain constructs in {\tt T} may add new rules to the simpset, |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
96 |
e.g.\ \ttindex{datatype} and \ttindex{primrec} in \HOL. Definitions are not |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
97 |
added automatically! |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
98 |
\item The script in {\tt T.ML} may manipulate the current simpset further by |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
99 |
explicitly adding/deleting theorems to/from it (see below). |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
100 |
\item After {\tt T.ML} has been read, the current simpset is associated with |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
101 |
theory {\tt T}. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
102 |
\end{enumerate} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
103 |
The current simpset is manipulated by |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
104 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
105 |
Addsimps, Delsimps: thm list -> unit |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
106 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
107 |
\begin{ttdescription} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
108 |
\item[\ttindexbold{Addsimps} $thms$] adds $thms$ to the current simpset |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
109 |
\item[\ttindexbold{Delsimps} $thms$] deletes $thms$ from the current simpset |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
110 |
\end{ttdescription} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
111 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
112 |
Generally useful simplification rules $\Var{n}+0 = \Var{n}$ should be added |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
113 |
to the current simpset right after they have been proved. Those of a more |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
114 |
specific nature (e.g.\ the laws of de~Morgan, which alter the structure of a |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
115 |
formula) should only be added for specific proofs and deleted again |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
116 |
afterwards. Conversely, it may also happen that a generally useful rule needs |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
117 |
to be removed for a certain proof and is added again afterwards. Well |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
118 |
designed simpsets need few temporary additions or deletions. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
119 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
120 |
\begin{warn} |
2479 | 121 |
The union of the ancestor simpsets (as described above) is not always a good |
122 |
simpset for the new theory. If some ancestors have deleted simplification |
|
123 |
rules because they are no longer wanted, while others have left those rules |
|
124 |
in, then the union will contain the unwanted rules. If the ancestor |
|
125 |
simpsets differ in other components (the subgoaler, solver, looper or rule |
|
126 |
preprocessor: see below), then you cannot be sure which version of that |
|
127 |
component will be inherited. You may have to set the component explicitly |
|
128 |
for the new theory's simpset by an assignment of the form |
|
129 |
{\tt simpset := \dots}. |
|
130 |
\end{warn} |
|
131 |
||
132 |
\begin{warn} |
|
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
133 |
If you execute proofs interactively, or load them from an ML file without |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
134 |
associated {\tt .thy} file, you must set the current simpset by calling |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
135 |
\ttindex{set_current_thy}~{\tt"}$T${\tt"}, where $T$ is the name of the |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
136 |
theory you want to work in. If you have just loaded $T$, this is not |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
137 |
necessary. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
138 |
\end{warn} |
104 | 139 |
|
140 |
||
286 | 141 |
\section{Simplification sets}\index{simplification sets} |
3134 | 142 |
The simplification tactics are controlled by {\bf simpsets}. These |
143 |
consist of several components, including rewrite rules, congruence |
|
144 |
rules, the subgoaler, the solver and the looper. The simplifier |
|
145 |
should be set up with sensible defaults so that most simplifier calls |
|
146 |
specify only rewrite rules. Experienced users can exploit the other |
|
147 |
components to streamline proofs. |
|
323 | 148 |
|
3108 | 149 |
Logics like \HOL, which support a current |
150 |
simpset\index{simpset!current}, store its value in an ML reference |
|
151 |
variable called {\tt simpset}\index{simpset@{\tt simpset} ML value}. |
|
104 | 152 |
|
332 | 153 |
\subsection{Rewrite rules} |
154 |
\index{rewrite rules|(} |
|
323 | 155 |
Rewrite rules are theorems expressing some form of equality: |
156 |
\begin{eqnarray*} |
|
157 |
Suc(\Var{m}) + \Var{n} &=& \Var{m} + Suc(\Var{n}) \\ |
|
158 |
\Var{P}\conj\Var{P} &\bimp& \Var{P} \\ |
|
714 | 159 |
\Var{A} \un \Var{B} &\equiv& \{x.x\in \Var{A} \disj x\in \Var{B}\} |
323 | 160 |
\end{eqnarray*} |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
161 |
Conditional rewrites such as $\Var{m}<\Var{n} \Imp \Var{m}/\Var{n} = |
3108 | 162 |
0$ are permitted; the conditions can be arbitrary formulas. |
104 | 163 |
|
323 | 164 |
Internally, all rewrite rules are translated into meta-equalities, theorems |
165 |
with conclusion $lhs \equiv rhs$. Each simpset contains a function for |
|
166 |
extracting equalities from arbitrary theorems. For example, |
|
167 |
$\neg(\Var{x}\in \{\})$ could be turned into $\Var{x}\in \{\} \equiv |
|
168 |
False$. This function can be installed using \ttindex{setmksimps} but only |
|
169 |
the definer of a logic should need to do this; see \S\ref{sec:setmksimps}. |
|
170 |
The function processes theorems added by \ttindex{addsimps} as well as |
|
171 |
local assumptions. |
|
104 | 172 |
|
173 |
||
332 | 174 |
\begin{warn} |
1101 | 175 |
The simplifier will accept all standard rewrite rules: those |
176 |
where all unknowns are of base type. Hence ${\Var{i}+(\Var{j}+\Var{k})} = |
|
714 | 177 |
{(\Var{i}+\Var{j})+\Var{k}}$ is ok. |
178 |
||
179 |
It will also deal gracefully with all rules whose left-hand sides are |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
180 |
so-called {\em higher-order patterns}~\cite{nipkow-patterns}. These are terms |
714 | 181 |
in $\beta$-normal form (this will always be the case unless you have done |
182 |
something strange) where each occurrence of an unknown is of the form |
|
183 |
$\Var{F}(x@1,\dots,x@n)$, where the $x@i$ are distinct bound variables. |
|
184 |
Hence $(\forall x.\Var{P}(x) \land \Var{Q}(x)) \bimp (\forall x.\Var{P}(x)) |
|
185 |
\land (\forall x.\Var{Q}(x))$ is also ok, in both directions. |
|
186 |
||
187 |
In some rare cases the rewriter will even deal with quite general rules: for |
|
188 |
example ${\Var{f}(\Var{x})\in range(\Var{f})} = True$ rewrites $g(a) \in |
|
189 |
range(g)$ to $True$, but will fail to match $g(h(b)) \in range(\lambda |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
190 |
x.g(h(x)))$. However, you can replace the offending subterms (in our case |
714 | 191 |
$\Var{f}(\Var{x})$, which is not a pattern) by adding new variables and |
192 |
conditions: $\Var{y} = \Var{f}(\Var{x}) \Imp \Var{y}\in range(\Var{f}) |
|
193 |
= True$ is acceptable as a conditional rewrite rule since conditions can |
|
194 |
be arbitrary terms. |
|
195 |
||
196 |
There is no restriction on the form of the right-hand sides. |
|
104 | 197 |
\end{warn} |
198 |
||
332 | 199 |
\index{rewrite rules|)} |
200 |
||
201 |
\subsection{*Congruence rules}\index{congruence rules} |
|
104 | 202 |
Congruence rules are meta-equalities of the form |
3108 | 203 |
\[ \dots \Imp |
104 | 204 |
f(\Var{x@1},\ldots,\Var{x@n}) \equiv f(\Var{y@1},\ldots,\Var{y@n}). |
205 |
\] |
|
323 | 206 |
This governs the simplification of the arguments of~$f$. For |
104 | 207 |
example, some arguments can be simplified under additional assumptions: |
208 |
\[ \List{\Var{P@1} \bimp \Var{Q@1};\; \Var{Q@1} \Imp \Var{P@2} \bimp \Var{Q@2}} |
|
209 |
\Imp (\Var{P@1} \imp \Var{P@2}) \equiv (\Var{Q@1} \imp \Var{Q@2}) |
|
210 |
\] |
|
323 | 211 |
Given this rule, the simplifier assumes $Q@1$ and extracts rewrite rules |
212 |
from it when simplifying~$P@2$. Such local assumptions are effective for |
|
698
23734672dc12
updated discussion of congruence rules in first section
lcp
parents:
332
diff
changeset
|
213 |
rewriting formulae such as $x=0\imp y+x=y$. The local assumptions are also |
23734672dc12
updated discussion of congruence rules in first section
lcp
parents:
332
diff
changeset
|
214 |
provided as theorems to the solver; see page~\pageref{sec:simp-solver} below. |
23734672dc12
updated discussion of congruence rules in first section
lcp
parents:
332
diff
changeset
|
215 |
|
23734672dc12
updated discussion of congruence rules in first section
lcp
parents:
332
diff
changeset
|
216 |
Here are some more examples. The congruence rule for bounded quantifiers |
23734672dc12
updated discussion of congruence rules in first section
lcp
parents:
332
diff
changeset
|
217 |
also supplies contextual information, this time about the bound variable: |
286 | 218 |
\begin{eqnarray*} |
219 |
&&\List{\Var{A}=\Var{B};\; |
|
220 |
\Forall x. x\in \Var{B} \Imp \Var{P}(x) = \Var{Q}(x)} \Imp{} \\ |
|
221 |
&&\qquad\qquad |
|
222 |
(\forall x\in \Var{A}.\Var{P}(x)) = (\forall x\in \Var{B}.\Var{Q}(x)) |
|
223 |
\end{eqnarray*} |
|
323 | 224 |
The congruence rule for conditional expressions can supply contextual |
225 |
information for simplifying the arms: |
|
104 | 226 |
\[ \List{\Var{p}=\Var{q};~ \Var{q} \Imp \Var{a}=\Var{c};~ |
227 |
\neg\Var{q} \Imp \Var{b}=\Var{d}} \Imp |
|
228 |
if(\Var{p},\Var{a},\Var{b}) \equiv if(\Var{q},\Var{c},\Var{d}) |
|
229 |
\] |
|
698
23734672dc12
updated discussion of congruence rules in first section
lcp
parents:
332
diff
changeset
|
230 |
A congruence rule can also {\em prevent\/} simplification of some arguments. |
104 | 231 |
Here is an alternative congruence rule for conditional expressions: |
232 |
\[ \Var{p}=\Var{q} \Imp |
|
233 |
if(\Var{p},\Var{a},\Var{b}) \equiv if(\Var{q},\Var{a},\Var{b}) |
|
234 |
\] |
|
235 |
Only the first argument is simplified; the others remain unchanged. |
|
236 |
This can make simplification much faster, but may require an extra case split |
|
237 |
to prove the goal. |
|
238 |
||
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
239 |
Congruence rules are added/deleted using |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
240 |
\ttindexbold{addeqcongs}/\ttindex{deleqcongs}. |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
241 |
Their conclusion must be a meta-equality, as in the examples above. It is more |
104 | 242 |
natural to derive the rules with object-logic equality, for example |
243 |
\[ \List{\Var{P@1} \bimp \Var{Q@1};\; \Var{Q@1} \Imp \Var{P@2} \bimp \Var{Q@2}} |
|
244 |
\Imp (\Var{P@1} \imp \Var{P@2}) \bimp (\Var{Q@1} \imp \Var{Q@2}), |
|
245 |
\] |
|
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
246 |
Each object-logic should define operators called \ttindex{addcongs} and |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
247 |
\ttindex{delcongs} that expects object-equalities and translates them into |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
248 |
meta-equalities. |
104 | 249 |
|
323 | 250 |
\subsection{*The subgoaler} |
104 | 251 |
The subgoaler is the tactic used to solve subgoals arising out of |
252 |
conditional rewrite rules or congruence rules. The default should be |
|
253 |
simplification itself. Occasionally this strategy needs to be changed. For |
|
254 |
example, if the premise of a conditional rule is an instance of its |
|
255 |
conclusion, as in $Suc(\Var{m}) < \Var{n} \Imp \Var{m} < \Var{n}$, the |
|
256 |
default strategy could loop. |
|
257 |
||
258 |
The subgoaler can be set explicitly with \ttindex{setsubgoaler}. For |
|
259 |
example, the subgoaler |
|
260 |
\begin{ttbox} |
|
332 | 261 |
fun subgoal_tac ss = assume_tac ORELSE' |
262 |
resolve_tac (prems_of_ss ss) ORELSE' |
|
104 | 263 |
asm_simp_tac ss; |
264 |
\end{ttbox} |
|
332 | 265 |
tries to solve the subgoal by assumption or with one of the premises, calling |
104 | 266 |
simplification only if that fails; here {\tt prems_of_ss} extracts the |
267 |
current premises from a simpset. |
|
268 |
||
698
23734672dc12
updated discussion of congruence rules in first section
lcp
parents:
332
diff
changeset
|
269 |
\subsection{*The solver}\label{sec:simp-solver} |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
270 |
The solver is a pair of tactics that attempt to solve a subgoal after |
286 | 271 |
simplification. Typically it just proves trivial subgoals such as {\tt |
323 | 272 |
True} and $t=t$. It could use sophisticated means such as {\tt |
3128
d01d4c0c4b44
New acknowledgements; fixed overfull lines and tables
paulson
parents:
3112
diff
changeset
|
273 |
blast_tac}, though that could make simplification expensive. |
286 | 274 |
|
3108 | 275 |
Rewriting does not instantiate unknowns. For example, rewriting |
276 |
cannot prove $a\in \Var{A}$ since this requires |
|
277 |
instantiating~$\Var{A}$. The solver, however, is an arbitrary tactic |
|
278 |
and may instantiate unknowns as it pleases. This is the only way the |
|
279 |
simplifier can handle a conditional rewrite rule whose condition |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
280 |
contains extra variables. When a simplification tactic is to be |
3108 | 281 |
combined with other provers, especially with the classical reasoner, |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
282 |
it is important whether it can be considered safe or not. Therefore, |
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
283 |
the solver is split into a safe and an unsafe part. Both parts can be |
3108 | 284 |
set independently, using either \ttindex{setSSolver} with a safe |
285 |
tactic as argument, or \ttindex{setSolver} with an unsafe tactic. |
|
286 |
Additional solvers, which are tried after the already set solvers, may |
|
287 |
be added using \ttindex{addSSolver} and \ttindex{addSolver}. |
|
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
288 |
|
3108 | 289 |
The standard simplification strategy solely uses the unsafe solver, |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
290 |
which is appropriate in most cases. But for special applications where |
3108 | 291 |
the simplification process is not allowed to instantiate unknowns |
292 |
within the goal, the tactic \ttindexbold{safe_asm_full_simp_tac} is |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
293 |
provided. It uses the \emph{safe} solver for the current subgoal, but |
3108 | 294 |
applies ordinary unsafe {\tt asm_full_simp_tac} for recursive internal |
295 |
simplifications (for conditional rules or congruences). |
|
323 | 296 |
|
297 |
\index{assumptions!in simplification} |
|
286 | 298 |
The tactic is presented with the full goal, including the asssumptions. |
299 |
Hence it can use those assumptions (say by calling {\tt assume_tac}) even |
|
300 |
inside {\tt simp_tac}, which otherwise does not use assumptions. The |
|
301 |
solver is also supplied a list of theorems, namely assumptions that hold in |
|
302 |
the local context. |
|
104 | 303 |
|
323 | 304 |
The subgoaler is also used to solve the premises of congruence rules, which |
305 |
are usually of the form $s = \Var{x}$, where $s$ needs to be simplified and |
|
306 |
$\Var{x}$ needs to be instantiated with the result. Hence the subgoaler |
|
307 |
should call the simplifier at some point. The simplifier will then call the |
|
308 |
solver, which must therefore be prepared to solve goals of the form $t = |
|
309 |
\Var{x}$, usually by reflexivity. In particular, reflexivity should be |
|
3128
d01d4c0c4b44
New acknowledgements; fixed overfull lines and tables
paulson
parents:
3112
diff
changeset
|
310 |
tried before any of the fancy tactics like {\tt blast_tac}. |
323 | 311 |
|
3108 | 312 |
It may even happen that due to simplification the subgoal is no longer |
313 |
an equality. For example $False \bimp \Var{Q}$ could be rewritten to |
|
314 |
$\neg\Var{Q}$. To cover this case, the solver could try resolving |
|
315 |
with the theorem $\neg False$. |
|
104 | 316 |
|
317 |
\begin{warn} |
|
3108 | 318 |
If the simplifier aborts with the message {\tt Failed congruence |
319 |
proof!}, then the subgoaler or solver has failed to prove a |
|
320 |
premise of a congruence rule. This should never occur under normal |
|
321 |
circumstances; it indicates that some congruence rule, or possibly |
|
322 |
the subgoaler or solver, is faulty. |
|
104 | 323 |
\end{warn} |
324 |
||
323 | 325 |
|
326 |
\subsection{*The looper} |
|
104 | 327 |
The looper is a tactic that is applied after simplification, in case the |
328 |
solver failed to solve the simplified goal. If the looper succeeds, the |
|
329 |
simplification process is started all over again. Each of the subgoals |
|
330 |
generated by the looper is attacked in turn, in reverse order. A |
|
331 |
typical looper is case splitting: the expansion of a conditional. Another |
|
332 |
possibility is to apply an elimination rule on the assumptions. More |
|
333 |
adventurous loopers could start an induction. The looper is set with |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
334 |
\ttindex{setloop}. Additional loopers, which are tried after the already set |
2567 | 335 |
looper, may be added with \ttindex{addloop}. |
104 | 336 |
|
337 |
||
338 |
\begin{figure} |
|
323 | 339 |
\index{*simpset ML type} |
2567 | 340 |
\index{*empty_ss} |
341 |
\index{*rep_ss} |
|
342 |
\index{*prems_of_ss} |
|
343 |
\index{*setloop} |
|
344 |
\index{*addloop} |
|
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
345 |
\index{*setSSolver} |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
346 |
\index{*addSSolver} |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
347 |
\index{*setSolver} |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
348 |
\index{*addSolver} |
2567 | 349 |
\index{*setsubgoaler} |
350 |
\index{*setmksimps} |
|
323 | 351 |
\index{*addsimps} |
352 |
\index{*delsimps} |
|
2567 | 353 |
\index{*addeqcongs} |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
354 |
\index{*deleqcongs} |
323 | 355 |
\index{*merge_ss} |
2567 | 356 |
\index{*simpset} |
357 |
\index{*Addsimps} |
|
358 |
\index{*Delsimps} |
|
359 |
\index{*simp_tac} |
|
360 |
\index{*asm_simp_tac} |
|
361 |
\index{*full_simp_tac} |
|
362 |
\index{*asm_full_simp_tac} |
|
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
363 |
\index{*safe_asm_full_simp_tac} |
2567 | 364 |
\index{*Simp_tac} |
365 |
\index{*Asm_simp_tac} |
|
366 |
\index{*Full_simp_tac} |
|
367 |
\index{*Asm_full_simp_tac} |
|
368 |
||
104 | 369 |
\begin{ttbox} |
3128
d01d4c0c4b44
New acknowledgements; fixed overfull lines and tables
paulson
parents:
3112
diff
changeset
|
370 |
infix 4 setsubgoaler setloop addloop |
d01d4c0c4b44
New acknowledgements; fixed overfull lines and tables
paulson
parents:
3112
diff
changeset
|
371 |
setSSolver addSSolver setSolver addSolver |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
372 |
setmksimps addsimps delsimps addeqcongs deleqcongs; |
104 | 373 |
|
374 |
signature SIMPLIFIER = |
|
375 |
sig |
|
376 |
type simpset |
|
2567 | 377 |
val empty_ss: simpset |
3134 | 378 |
val rep_ss: simpset -> {\ttlbrace}simps: thm list, procs: string list, |
3128
d01d4c0c4b44
New acknowledgements; fixed overfull lines and tables
paulson
parents:
3112
diff
changeset
|
379 |
congs: thm list, |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
380 |
subgoal_tac: simpset -> int -> tactic, |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
381 |
loop_tac: int -> tactic, |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
382 |
finish_tac: thm list -> int -> tactic, |
3134 | 383 |
unsafe_finish_tac: thm list -> int -> tactic{\ttrbrace} |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
384 |
val setsubgoaler: simpset * (simpset -> int -> tactic) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
385 |
val setloop: simpset * (int -> tactic) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
386 |
val addloop: simpset * (int -> tactic) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
387 |
val setSSolver: simpset * (thm list -> int -> tactic) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
388 |
val addSSolver: simpset * (thm list -> int -> tactic) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
389 |
val setSolver: simpset * (thm list -> int -> tactic) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
390 |
val addSolver: simpset * (thm list -> int -> tactic) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
391 |
val setmksimps: simpset * (thm -> thm list) -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
392 |
val addsimps: simpset * thm list -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
393 |
val delsimps: simpset * thm list -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
394 |
val addeqcongs: simpset * thm list -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
395 |
val deleqcongs: simpset * thm list -> simpset |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
396 |
val merge_ss: simpset * simpset -> simpset |
2567 | 397 |
val prems_of_ss: simpset -> thm list |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
398 |
val simpset: simpset ref |
2567 | 399 |
val Addsimps: thm list -> unit |
400 |
val Delsimps: thm list -> unit |
|
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
401 |
val simp_tac: simpset -> int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
402 |
val asm_simp_tac: simpset -> int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
403 |
val full_simp_tac: simpset -> int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
404 |
val asm_full_simp_tac: simpset -> int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
405 |
val safe_asm_full_simp_tac: simpset -> int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
406 |
val Simp_tac: int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
407 |
val Asm_simp_tac: int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
408 |
val Full_simp_tac: int -> tactic |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
409 |
val Asm_full_simp_tac: int -> tactic |
104 | 410 |
end; |
411 |
\end{ttbox} |
|
323 | 412 |
\caption{The simplifier primitives} \label{SIMPLIFIER} |
104 | 413 |
\end{figure} |
414 |
||
415 |
||
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
416 |
\section{The simplification tactics} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
417 |
\label{simp-tactics} |
323 | 418 |
\index{simplification!tactics} |
419 |
\index{tactics!simplification} |
|
104 | 420 |
|
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
421 |
The actual simplification work is performed by the following basic tactics: |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
422 |
\ttindexbold{simp_tac}, \ttindexbold{asm_simp_tac}, |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
423 |
\ttindexbold{full_simp_tac} and \ttindexbold{asm_full_simp_tac}. They work |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
424 |
exactly like their namesakes in \S\ref{sec:simp-for-dummies}, except that |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
425 |
they are explicitly supplied with a simpset. This is because the ones in |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
426 |
\S\ref{sec:simp-for-dummies} are defined in terms of the ones above, e.g. |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
427 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
428 |
fun Simp_tac i = simp_tac (!simpset) i; |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
429 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
430 |
where \ttindex{!simpset} is the current simpset\index{simpset!current}. |
104 | 431 |
|
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
432 |
The rewriting strategy of all four tactics is strictly bottom up, except for |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
433 |
congruence rules, which are applied while descending into a term. Conditions |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
434 |
in conditional rewrite rules are solved recursively before the rewrite rule |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
435 |
is applied. |
104 | 436 |
|
3108 | 437 |
The infix operation \ttindex{addsimps} adds rewrite rules to a |
438 |
simpset, while \ttindex{delsimps} deletes them. They are used to |
|
439 |
implement \ttindex{Addsimps} and \ttindex{Delsimps}, e.g. |
|
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
440 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
441 |
fun Addsimps thms = (simpset := (!simpset addsimps thms)); |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
442 |
\end{ttbox} |
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
443 |
and can also be used directly, even in the presence of a current simpset. For |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
444 |
example |
1213 | 445 |
\begin{ttbox} |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
446 |
Addsimps \(thms\); |
2479 | 447 |
by (Simp_tac \(i\)); |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
448 |
Delsimps \(thms\); |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
449 |
\end{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
450 |
can be compressed into |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
451 |
\begin{ttbox} |
2479 | 452 |
by (simp_tac (!simpset addsimps \(thms\)) \(i\)); |
1213 | 453 |
\end{ttbox} |
1860
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
454 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
455 |
The simpset associated with a particular theory can be retrieved via the name |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
456 |
of the theory using the function |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
457 |
\begin{ttbox} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
458 |
simpset_of: string -> simpset |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
459 |
\end{ttbox}\index{*simpset_of} |
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
460 |
|
71bfeecfa96c
Documented simplification tactics which make use of the implicit simpset.
nipkow
parents:
1387
diff
changeset
|
461 |
To remind yourself of what is in a simpset, use the function \verb$rep_ss$ to |
104 | 462 |
return its simplification and congruence rules. |
463 |
||
286 | 464 |
\section{Examples using the simplifier} |
3112 | 465 |
\index{examples!of simplification} Assume we are working within {\tt |
466 |
FOL} (cf.\ \texttt{FOL/ex/Nat}) and that |
|
323 | 467 |
\begin{ttdescription} |
468 |
\item[Nat.thy] |
|
469 |
is a theory including the constants $0$, $Suc$ and $+$, |
|
470 |
\item[add_0] |
|
471 |
is the rewrite rule $0+\Var{n} = \Var{n}$, |
|
472 |
\item[add_Suc] |
|
473 |
is the rewrite rule $Suc(\Var{m})+\Var{n} = Suc(\Var{m}+\Var{n})$, |
|
474 |
\item[induct] |
|
475 |
is the induction rule $\List{\Var{P}(0);\; \Forall x. \Var{P}(x)\Imp |
|
476 |
\Var{P}(Suc(x))} \Imp \Var{P}(\Var{n})$. |
|
477 |
\end{ttdescription} |
|
3112 | 478 |
We augment the implicit simpset of {\FOL} with the basic rewrite rules |
479 |
for natural numbers: |
|
104 | 480 |
\begin{ttbox} |
3112 | 481 |
Addsimps [add_0, add_Suc]; |
104 | 482 |
\end{ttbox} |
323 | 483 |
|
484 |
\subsection{A trivial example} |
|
286 | 485 |
Proofs by induction typically involve simplification. Here is a proof |
486 |
that~0 is a right identity: |
|
104 | 487 |
\begin{ttbox} |
488 |
goal Nat.thy "m+0 = m"; |
|
489 |
{\out Level 0} |
|
490 |
{\out m + 0 = m} |
|
491 |
{\out 1. m + 0 = m} |
|
286 | 492 |
\end{ttbox} |
493 |
The first step is to perform induction on the variable~$m$. This returns a |
|
494 |
base case and inductive step as two subgoals: |
|
495 |
\begin{ttbox} |
|
104 | 496 |
by (res_inst_tac [("n","m")] induct 1); |
497 |
{\out Level 1} |
|
498 |
{\out m + 0 = m} |
|
499 |
{\out 1. 0 + 0 = 0} |
|
500 |
{\out 2. !!x. x + 0 = x ==> Suc(x) + 0 = Suc(x)} |
|
501 |
\end{ttbox} |
|
286 | 502 |
Simplification solves the first subgoal trivially: |
104 | 503 |
\begin{ttbox} |
3112 | 504 |
by (Simp_tac 1); |
104 | 505 |
{\out Level 2} |
506 |
{\out m + 0 = m} |
|
507 |
{\out 1. !!x. x + 0 = x ==> Suc(x) + 0 = Suc(x)} |
|
508 |
\end{ttbox} |
|
3112 | 509 |
The remaining subgoal requires \ttindex{Asm_simp_tac} in order to use the |
104 | 510 |
induction hypothesis as a rewrite rule: |
511 |
\begin{ttbox} |
|
3112 | 512 |
by (Asm_simp_tac 1); |
104 | 513 |
{\out Level 3} |
514 |
{\out m + 0 = m} |
|
515 |
{\out No subgoals!} |
|
516 |
\end{ttbox} |
|
517 |
||
323 | 518 |
\subsection{An example of tracing} |
3108 | 519 |
\index{tracing!of simplification|(}\index{*trace_simp} |
323 | 520 |
Let us prove a similar result involving more complex terms. The two |
521 |
equations together can be used to prove that addition is commutative. |
|
104 | 522 |
\begin{ttbox} |
523 |
goal Nat.thy "m+Suc(n) = Suc(m+n)"; |
|
524 |
{\out Level 0} |
|
525 |
{\out m + Suc(n) = Suc(m + n)} |
|
526 |
{\out 1. m + Suc(n) = Suc(m + n)} |
|
286 | 527 |
\end{ttbox} |
528 |
We again perform induction on~$m$ and get two subgoals: |
|
529 |
\begin{ttbox} |
|
104 | 530 |
by (res_inst_tac [("n","m")] induct 1); |
531 |
{\out Level 1} |
|
532 |
{\out m + Suc(n) = Suc(m + n)} |
|
533 |
{\out 1. 0 + Suc(n) = Suc(0 + n)} |
|
286 | 534 |
{\out 2. !!x. x + Suc(n) = Suc(x + n) ==>} |
535 |
{\out Suc(x) + Suc(n) = Suc(Suc(x) + n)} |
|
536 |
\end{ttbox} |
|
537 |
Simplification solves the first subgoal, this time rewriting two |
|
538 |
occurrences of~0: |
|
539 |
\begin{ttbox} |
|
3112 | 540 |
by (Simp_tac 1); |
104 | 541 |
{\out Level 2} |
542 |
{\out m + Suc(n) = Suc(m + n)} |
|
286 | 543 |
{\out 1. !!x. x + Suc(n) = Suc(x + n) ==>} |
544 |
{\out Suc(x) + Suc(n) = Suc(Suc(x) + n)} |
|
104 | 545 |
\end{ttbox} |
546 |
Switching tracing on illustrates how the simplifier solves the remaining |
|
547 |
subgoal: |
|
548 |
\begin{ttbox} |
|
549 |
trace_simp := true; |
|
3112 | 550 |
by (Asm_simp_tac 1); |
323 | 551 |
\ttbreak |
3112 | 552 |
{\out Adding rewrite rule:} |
553 |
{\out .x + Suc(n) == Suc(.x + n)} |
|
323 | 554 |
\ttbreak |
104 | 555 |
{\out Rewriting:} |
3112 | 556 |
{\out Suc(.x) + Suc(n) == Suc(.x + Suc(n))} |
323 | 557 |
\ttbreak |
104 | 558 |
{\out Rewriting:} |
3112 | 559 |
{\out .x + Suc(n) == Suc(.x + n)} |
323 | 560 |
\ttbreak |
104 | 561 |
{\out Rewriting:} |
3112 | 562 |
{\out Suc(.x) + n == Suc(.x + n)} |
563 |
\ttbreak |
|
564 |
{\out Rewriting:} |
|
565 |
{\out Suc(Suc(.x + n)) = Suc(Suc(.x + n)) == True} |
|
323 | 566 |
\ttbreak |
104 | 567 |
{\out Level 3} |
568 |
{\out m + Suc(n) = Suc(m + n)} |
|
569 |
{\out No subgoals!} |
|
570 |
\end{ttbox} |
|
286 | 571 |
Many variations are possible. At Level~1 (in either example) we could have |
572 |
solved both subgoals at once using the tactical \ttindex{ALLGOALS}: |
|
104 | 573 |
\begin{ttbox} |
3112 | 574 |
by (ALLGOALS Asm_simp_tac); |
104 | 575 |
{\out Level 2} |
576 |
{\out m + Suc(n) = Suc(m + n)} |
|
577 |
{\out No subgoals!} |
|
578 |
\end{ttbox} |
|
3108 | 579 |
\index{tracing!of simplification|)} |
104 | 580 |
|
323 | 581 |
\subsection{Free variables and simplification} |
104 | 582 |
Here is a conjecture to be proved for an arbitrary function~$f$ satisfying |
323 | 583 |
the law $f(Suc(\Var{n})) = Suc(f(\Var{n}))$: |
104 | 584 |
\begin{ttbox} |
585 |
val [prem] = goal Nat.thy |
|
586 |
"(!!n. f(Suc(n)) = Suc(f(n))) ==> f(i+j) = i+f(j)"; |
|
587 |
{\out Level 0} |
|
588 |
{\out f(i + j) = i + f(j)} |
|
589 |
{\out 1. f(i + j) = i + f(j)} |
|
323 | 590 |
\ttbreak |
286 | 591 |
{\out val prem = "f(Suc(?n)) = Suc(f(?n))} |
592 |
{\out [!!n. f(Suc(n)) = Suc(f(n))]" : thm} |
|
323 | 593 |
\end{ttbox} |
594 |
In the theorem~{\tt prem}, note that $f$ is a free variable while |
|
595 |
$\Var{n}$ is a schematic variable. |
|
596 |
\begin{ttbox} |
|
104 | 597 |
by (res_inst_tac [("n","i")] induct 1); |
598 |
{\out Level 1} |
|
599 |
{\out f(i + j) = i + f(j)} |
|
600 |
{\out 1. f(0 + j) = 0 + f(j)} |
|
601 |
{\out 2. !!x. f(x + j) = x + f(j) ==> f(Suc(x) + j) = Suc(x) + f(j)} |
|
602 |
\end{ttbox} |
|
603 |
We simplify each subgoal in turn. The first one is trivial: |
|
604 |
\begin{ttbox} |
|
3112 | 605 |
by (Simp_tac 1); |
104 | 606 |
{\out Level 2} |
607 |
{\out f(i + j) = i + f(j)} |
|
608 |
{\out 1. !!x. f(x + j) = x + f(j) ==> f(Suc(x) + j) = Suc(x) + f(j)} |
|
609 |
\end{ttbox} |
|
3112 | 610 |
The remaining subgoal requires rewriting by the premise, so we add it |
611 |
to the current simpset:\footnote{The previous simplifier required |
|
612 |
congruence rules for function variables like~$f$ in order to |
|
613 |
simplify their arguments. It was more general than the current |
|
614 |
simplifier, but harder to use and slower. The present simplifier |
|
615 |
can be given congruence rules to realize non-standard simplification |
|
616 |
of a function's arguments, but this is seldom necessary.} |
|
104 | 617 |
\begin{ttbox} |
3112 | 618 |
by (asm_simp_tac (!simpset addsimps [prem]) 1); |
104 | 619 |
{\out Level 3} |
620 |
{\out f(i + j) = i + f(j)} |
|
621 |
{\out No subgoals!} |
|
622 |
\end{ttbox} |
|
623 |
||
1213 | 624 |
\subsection{Reordering assumptions} |
625 |
\label{sec:reordering-asms} |
|
626 |
\index{assumptions!reordering} |
|
627 |
||
628 |
As mentioned above, \ttindex{asm_full_simp_tac} may require the assumptions |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
629 |
to be permuted to be more effective. Given the subgoal |
1213 | 630 |
\begin{ttbox} |
631 |
{\out 1. [| P(f(a)); Q; f(a) = t; R |] ==> S} |
|
632 |
\end{ttbox} |
|
633 |
we can rotate the assumptions two positions to the right |
|
634 |
\begin{ttbox} |
|
635 |
by (rotate_tac ~2 1); |
|
636 |
\end{ttbox} |
|
637 |
to obtain |
|
638 |
\begin{ttbox} |
|
639 |
{\out 1. [| f(a) = t; R; P(f(a)); Q |] ==> S} |
|
640 |
\end{ttbox} |
|
641 |
which enables \verb$asm_full_simp_tac$ to simplify \verb$P(f(a))$ to |
|
642 |
\verb$P(t)$. |
|
643 |
||
644 |
Since rotation alone cannot produce arbitrary permutations, you can also pick |
|
645 |
out a particular assumption which needs to be rewritten and move it the the |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
646 |
right end of the assumptions. In the above case rotation can be replaced by |
1213 | 647 |
\begin{ttbox} |
648 |
by (dres_inst_tac [("psi","P(f(a))")] asm_rl 1); |
|
649 |
\end{ttbox} |
|
650 |
which is more directed and leads to |
|
651 |
\begin{ttbox} |
|
652 |
{\out 1. [| Q; f(a) = t; R; P(f(a)) |] ==> S} |
|
653 |
\end{ttbox} |
|
654 |
||
655 |
Note that reordering assumptions usually leads to brittle proofs and should |
|
3485
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
paulson
parents:
3134
diff
changeset
|
656 |
therefore be avoided. Future versions of \verb$asm_full_simp_tac$ may remove |
1213 | 657 |
the need for such manipulations. |
286 | 658 |
|
332 | 659 |
\section{Permutative rewrite rules} |
323 | 660 |
\index{rewrite rules!permutative|(} |
661 |
||
662 |
A rewrite rule is {\bf permutative} if the left-hand side and right-hand |
|
663 |
side are the same up to renaming of variables. The most common permutative |
|
664 |
rule is commutativity: $x+y = y+x$. Other examples include $(x-y)-z = |
|
665 |
(x-z)-y$ in arithmetic and $insert(x,insert(y,A)) = insert(y,insert(x,A))$ |
|
666 |
for sets. Such rules are common enough to merit special attention. |
|
667 |
||
668 |
Because ordinary rewriting loops given such rules, the simplifier employs a |
|
669 |
special strategy, called {\bf ordered rewriting}\index{rewriting!ordered}. |
|
3108 | 670 |
There is a standard lexicographic ordering on terms. A permutative rewrite |
323 | 671 |
rule is applied only if it decreases the given term with respect to this |
672 |
ordering. For example, commutativity rewrites~$b+a$ to $a+b$, but then |
|
673 |
stops because $a+b$ is strictly less than $b+a$. The Boyer-Moore theorem |
|
674 |
prover~\cite{bm88book} also employs ordered rewriting. |
|
675 |
||
676 |
Permutative rewrite rules are added to simpsets just like other rewrite |
|
677 |
rules; the simplifier recognizes their special status automatically. They |
|
678 |
are most effective in the case of associative-commutative operators. |
|
679 |
(Associativity by itself is not permutative.) When dealing with an |
|
680 |
AC-operator~$f$, keep the following points in mind: |
|
681 |
\begin{itemize}\index{associative-commutative operators} |
|
682 |
\item The associative law must always be oriented from left to right, namely |
|
683 |
$f(f(x,y),z) = f(x,f(y,z))$. The opposite orientation, if used with |
|
684 |
commutativity, leads to looping! Future versions of Isabelle may remove |
|
685 |
this restriction. |
|
686 |
||
687 |
\item To complete your set of rewrite rules, you must add not just |
|
688 |
associativity~(A) and commutativity~(C) but also a derived rule, {\bf |
|
689 |
left-commutativity} (LC): $f(x,f(y,z)) = f(y,f(x,z))$. |
|
690 |
\end{itemize} |
|
691 |
Ordered rewriting with the combination of A, C, and~LC sorts a term |
|
692 |
lexicographically: |
|
693 |
\[\def\maps#1{\stackrel{#1}{\longmapsto}} |
|
694 |
(b+c)+a \maps{A} b+(c+a) \maps{C} b+(a+c) \maps{LC} a+(b+c) \] |
|
695 |
Martin and Nipkow~\cite{martin-nipkow} discuss the theory and give many |
|
696 |
examples; other algebraic structures are amenable to ordered rewriting, |
|
697 |
such as boolean rings. |
|
698 |
||
3108 | 699 |
\subsection{Example: sums of natural numbers} |
700 |
This example is set in \HOL\ (see \texttt{HOL/ex/NatSum}). Theory |
|
701 |
\thydx{Arith} contains natural numbers arithmetic. Its associated |
|
702 |
simpset contains many arithmetic laws including distributivity |
|
703 |
of~$\times$ over~$+$, while {\tt add_ac} is a list consisting of the |
|
704 |
A, C and LC laws for~$+$ on type \texttt{nat}. Let us prove the |
|
705 |
theorem |
|
323 | 706 |
\[ \sum@{i=1}^n i = n\times(n+1)/2. \] |
707 |
% |
|
708 |
A functional~{\tt sum} represents the summation operator under the |
|
3108 | 709 |
interpretation ${\tt sum} \, f \, (n + 1) = \sum@{i=0}^n f\,i$. We |
710 |
extend {\tt Arith} using a theory file: |
|
323 | 711 |
\begin{ttbox} |
712 |
NatSum = Arith + |
|
1387 | 713 |
consts sum :: [nat=>nat, nat] => nat |
3108 | 714 |
rules sum_0 "sum f 0 = 0" |
715 |
sum_Suc "sum f (Suc n) = f n + sum f n" |
|
323 | 716 |
end |
717 |
\end{ttbox} |
|
3108 | 718 |
We make the required simpset by adding the AC-rules for~$+$ and the |
719 |
axioms for~{\tt sum}: |
|
323 | 720 |
\begin{ttbox} |
3108 | 721 |
val natsum_ss = simpset_of "Arith" addsimps ([sum_0,sum_Suc] \at add_ac); |
722 |
{\out val natsum_ss = \ldots : simpset} |
|
323 | 723 |
\end{ttbox} |
3108 | 724 |
Our desired theorem now reads ${\tt sum} \, (\lambda i.i) \, (n+1) = |
323 | 725 |
n\times(n+1)/2$. The Isabelle goal has both sides multiplied by~$2$: |
726 |
\begin{ttbox} |
|
3108 | 727 |
goal NatSum.thy "2 * sum (\%i.i) (Suc n) = n * Suc n"; |
323 | 728 |
{\out Level 0} |
3108 | 729 |
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n} |
730 |
{\out 1. 2 * sum (\%i. i) (Suc n) = n * Suc n} |
|
323 | 731 |
\end{ttbox} |
3108 | 732 |
Induction should not be applied until the goal is in the simplest |
733 |
form: |
|
323 | 734 |
\begin{ttbox} |
735 |
by (simp_tac natsum_ss 1); |
|
736 |
{\out Level 1} |
|
3108 | 737 |
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n} |
738 |
{\out 1. n + (sum (\%i. i) n + sum (\%i. i) n) = n * n} |
|
323 | 739 |
\end{ttbox} |
3108 | 740 |
Ordered rewriting has sorted the terms in the left-hand side. The |
741 |
subgoal is now ready for induction: |
|
323 | 742 |
\begin{ttbox} |
743 |
by (nat_ind_tac "n" 1); |
|
744 |
{\out Level 2} |
|
3108 | 745 |
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n} |
746 |
{\out 1. 0 + (sum (\%i. i) 0 + sum (\%i. i) 0) = 0 * 0} |
|
323 | 747 |
\ttbreak |
3108 | 748 |
{\out 2. !!n1. n1 + (sum (\%i. i) n1 + sum (\%i. i) n1) = n1 * n1} |
749 |
{\out ==> Suc n1 + (sum (\%i. i) (Suc n1) + sum (\%i. i) (Suc n1)) =} |
|
750 |
{\out Suc n1 * Suc n1} |
|
323 | 751 |
\end{ttbox} |
752 |
Simplification proves both subgoals immediately:\index{*ALLGOALS} |
|
753 |
\begin{ttbox} |
|
754 |
by (ALLGOALS (asm_simp_tac natsum_ss)); |
|
755 |
{\out Level 3} |
|
3108 | 756 |
{\out 2 * sum (\%i. i) (Suc n) = n * Suc n} |
323 | 757 |
{\out No subgoals!} |
758 |
\end{ttbox} |
|
759 |
If we had omitted {\tt add_ac} from the simpset, simplification would have |
|
760 |
failed to prove the induction step: |
|
761 |
\begin{ttbox} |
|
3108 | 762 |
2 * sum (\%i. i) (Suc n) = n * Suc n |
763 |
1. !!n1. n1 + sum (\%i. i) n1 + (n1 + sum (\%i. i) n1) = n1 + n1 * n1 |
|
3128
d01d4c0c4b44
New acknowledgements; fixed overfull lines and tables
paulson
parents:
3112
diff
changeset
|
764 |
==> n1 + (n1 + sum (\%i. i) n1) + (n1 + (n1 + sum (\%i.i) n1)) = |
3108 | 765 |
n1 + (n1 + (n1 + n1 * n1)) |
323 | 766 |
\end{ttbox} |
767 |
Ordered rewriting proves this by sorting the left-hand side. Proving |
|
768 |
arithmetic theorems without ordered rewriting requires explicit use of |
|
769 |
commutativity. This is tedious; try it and see! |
|
770 |
||
771 |
Ordered rewriting is equally successful in proving |
|
772 |
$\sum@{i=1}^n i^3 = n^2\times(n+1)^2/4$. |
|
773 |
||
774 |
||
775 |
\subsection{Re-orienting equalities} |
|
776 |
Ordered rewriting with the derived rule {\tt symmetry} can reverse equality |
|
777 |
signs: |
|
778 |
\begin{ttbox} |
|
779 |
val symmetry = prove_goal HOL.thy "(x=y) = (y=x)" |
|
3128
d01d4c0c4b44
New acknowledgements; fixed overfull lines and tables
paulson
parents:
3112
diff
changeset
|
780 |
(fn _ => [Blast_tac 1]); |
323 | 781 |
\end{ttbox} |
782 |
This is frequently useful. Assumptions of the form $s=t$, where $t$ occurs |
|
783 |
in the conclusion but not~$s$, can often be brought into the right form. |
|
784 |
For example, ordered rewriting with {\tt symmetry} can prove the goal |
|
785 |
\[ f(a)=b \conj f(a)=c \imp b=c. \] |
|
786 |
Here {\tt symmetry} reverses both $f(a)=b$ and $f(a)=c$ |
|
787 |
because $f(a)$ is lexicographically greater than $b$ and~$c$. These |
|
788 |
re-oriented equations, as rewrite rules, replace $b$ and~$c$ in the |
|
789 |
conclusion by~$f(a)$. |
|
790 |
||
791 |
Another example is the goal $\neg(t=u) \imp \neg(u=t)$. |
|
792 |
The differing orientations make this appear difficult to prove. Ordered |
|
793 |
rewriting with {\tt symmetry} makes the equalities agree. (Without |
|
794 |
knowing more about~$t$ and~$u$ we cannot say whether they both go to $t=u$ |
|
795 |
or~$u=t$.) Then the simplifier can prove the goal outright. |
|
796 |
||
797 |
\index{rewrite rules!permutative|)} |
|
798 |
||
799 |
||
800 |
\section{*Setting up the simplifier}\label{sec:setting-up-simp} |
|
801 |
\index{simplification!setting up} |
|
286 | 802 |
|
803 |
Setting up the simplifier for new logics is complicated. This section |
|
323 | 804 |
describes how the simplifier is installed for intuitionistic first-order |
805 |
logic; the code is largely taken from {\tt FOL/simpdata.ML}. |
|
286 | 806 |
|
323 | 807 |
The simplifier and the case splitting tactic, which reside on separate |
808 |
files, are not part of Pure Isabelle. They must be loaded explicitly: |
|
286 | 809 |
\begin{ttbox} |
810 |
use "../Provers/simplifier.ML"; |
|
811 |
use "../Provers/splitter.ML"; |
|
812 |
\end{ttbox} |
|
813 |
||
814 |
Simplification works by reducing various object-equalities to |
|
323 | 815 |
meta-equality. It requires rules stating that equal terms and equivalent |
816 |
formulae are also equal at the meta-level. The rule declaration part of |
|
3108 | 817 |
the file {\tt FOL/IFOL.thy} contains the two lines |
323 | 818 |
\begin{ttbox}\index{*eq_reflection theorem}\index{*iff_reflection theorem} |
286 | 819 |
eq_reflection "(x=y) ==> (x==y)" |
820 |
iff_reflection "(P<->Q) ==> (P==Q)" |
|
821 |
\end{ttbox} |
|
323 | 822 |
Of course, you should only assert such rules if they are true for your |
286 | 823 |
particular logic. In Constructive Type Theory, equality is a ternary |
824 |
relation of the form $a=b\in A$; the type~$A$ determines the meaning of the |
|
332 | 825 |
equality essentially as a partial equivalence relation. The present |
323 | 826 |
simplifier cannot be used. Rewriting in {\tt CTT} uses another simplifier, |
827 |
which resides in the file {\tt typedsimp.ML} and is not documented. Even |
|
828 |
this does not work for later variants of Constructive Type Theory that use |
|
829 |
intensional equality~\cite{nordstrom90}. |
|
286 | 830 |
|
831 |
||
832 |
\subsection{A collection of standard rewrite rules} |
|
833 |
The file begins by proving lots of standard rewrite rules about the logical |
|
323 | 834 |
connectives. These include cancellation and associative laws. To prove |
835 |
them easily, it defines a function that echoes the desired law and then |
|
286 | 836 |
supplies it the theorem prover for intuitionistic \FOL: |
837 |
\begin{ttbox} |
|
838 |
fun int_prove_fun s = |
|
839 |
(writeln s; |
|
840 |
prove_goal IFOL.thy s |
|
841 |
(fn prems => [ (cut_facts_tac prems 1), |
|
842 |
(Int.fast_tac 1) ])); |
|
843 |
\end{ttbox} |
|
844 |
The following rewrite rules about conjunction are a selection of those |
|
845 |
proved on {\tt FOL/simpdata.ML}. Later, these will be supplied to the |
|
846 |
standard simpset. |
|
847 |
\begin{ttbox} |
|
848 |
val conj_rews = map int_prove_fun |
|
849 |
["P & True <-> P", "True & P <-> P", |
|
850 |
"P & False <-> False", "False & P <-> False", |
|
851 |
"P & P <-> P", |
|
852 |
"P & ~P <-> False", "~P & P <-> False", |
|
853 |
"(P & Q) & R <-> P & (Q & R)"]; |
|
854 |
\end{ttbox} |
|
855 |
The file also proves some distributive laws. As they can cause exponential |
|
856 |
blowup, they will not be included in the standard simpset. Instead they |
|
323 | 857 |
are merely bound to an \ML{} identifier, for user reference. |
286 | 858 |
\begin{ttbox} |
859 |
val distrib_rews = map int_prove_fun |
|
860 |
["P & (Q | R) <-> P&Q | P&R", |
|
861 |
"(Q | R) & P <-> Q&P | R&P", |
|
862 |
"(P | Q --> R) <-> (P --> R) & (Q --> R)"]; |
|
863 |
\end{ttbox} |
|
864 |
||
865 |
||
866 |
\subsection{Functions for preprocessing the rewrite rules} |
|
323 | 867 |
\label{sec:setmksimps} |
868 |
% |
|
286 | 869 |
The next step is to define the function for preprocessing rewrite rules. |
870 |
This will be installed by calling {\tt setmksimps} below. Preprocessing |
|
871 |
occurs whenever rewrite rules are added, whether by user command or |
|
872 |
automatically. Preprocessing involves extracting atomic rewrites at the |
|
873 |
object-level, then reflecting them to the meta-level. |
|
874 |
||
875 |
To start, the function {\tt gen_all} strips any meta-level |
|
876 |
quantifiers from the front of the given theorem. Usually there are none |
|
877 |
anyway. |
|
878 |
\begin{ttbox} |
|
879 |
fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th; |
|
880 |
\end{ttbox} |
|
881 |
The function {\tt atomize} analyses a theorem in order to extract |
|
882 |
atomic rewrite rules. The head of all the patterns, matched by the |
|
883 |
wildcard~{\tt _}, is the coercion function {\tt Trueprop}. |
|
884 |
\begin{ttbox} |
|
885 |
fun atomize th = case concl_of th of |
|
886 |
_ $ (Const("op &",_) $ _ $ _) => atomize(th RS conjunct1) \at |
|
887 |
atomize(th RS conjunct2) |
|
888 |
| _ $ (Const("op -->",_) $ _ $ _) => atomize(th RS mp) |
|
889 |
| _ $ (Const("All",_) $ _) => atomize(th RS spec) |
|
890 |
| _ $ (Const("True",_)) => [] |
|
891 |
| _ $ (Const("False",_)) => [] |
|
892 |
| _ => [th]; |
|
893 |
\end{ttbox} |
|
894 |
There are several cases, depending upon the form of the conclusion: |
|
895 |
\begin{itemize} |
|
896 |
\item Conjunction: extract rewrites from both conjuncts. |
|
897 |
||
898 |
\item Implication: convert $P\imp Q$ to the meta-implication $P\Imp Q$ and |
|
899 |
extract rewrites from~$Q$; these will be conditional rewrites with the |
|
900 |
condition~$P$. |
|
901 |
||
902 |
\item Universal quantification: remove the quantifier, replacing the bound |
|
903 |
variable by a schematic variable, and extract rewrites from the body. |
|
904 |
||
905 |
\item {\tt True} and {\tt False} contain no useful rewrites. |
|
906 |
||
907 |
\item Anything else: return the theorem in a singleton list. |
|
908 |
\end{itemize} |
|
909 |
The resulting theorems are not literally atomic --- they could be |
|
323 | 910 |
disjunctive, for example --- but are broken down as much as possible. See |
286 | 911 |
the file {\tt ZF/simpdata.ML} for a sophisticated translation of |
912 |
set-theoretic formulae into rewrite rules. |
|
104 | 913 |
|
286 | 914 |
The simplified rewrites must now be converted into meta-equalities. The |
323 | 915 |
rule {\tt eq_reflection} converts equality rewrites, while {\tt |
286 | 916 |
iff_reflection} converts if-and-only-if rewrites. The latter possibility |
917 |
can arise in two other ways: the negative theorem~$\neg P$ is converted to |
|
323 | 918 |
$P\equiv{\tt False}$, and any other theorem~$P$ is converted to |
286 | 919 |
$P\equiv{\tt True}$. The rules {\tt iff_reflection_F} and {\tt |
920 |
iff_reflection_T} accomplish this conversion. |
|
921 |
\begin{ttbox} |
|
922 |
val P_iff_F = int_prove_fun "~P ==> (P <-> False)"; |
|
923 |
val iff_reflection_F = P_iff_F RS iff_reflection; |
|
924 |
\ttbreak |
|
925 |
val P_iff_T = int_prove_fun "P ==> (P <-> True)"; |
|
926 |
val iff_reflection_T = P_iff_T RS iff_reflection; |
|
927 |
\end{ttbox} |
|
928 |
The function {\tt mk_meta_eq} converts a theorem to a meta-equality |
|
929 |
using the case analysis described above. |
|
930 |
\begin{ttbox} |
|
931 |
fun mk_meta_eq th = case concl_of th of |
|
932 |
_ $ (Const("op =",_)$_$_) => th RS eq_reflection |
|
933 |
| _ $ (Const("op <->",_)$_$_) => th RS iff_reflection |
|
934 |
| _ $ (Const("Not",_)$_) => th RS iff_reflection_F |
|
935 |
| _ => th RS iff_reflection_T; |
|
936 |
\end{ttbox} |
|
937 |
The three functions {\tt gen_all}, {\tt atomize} and {\tt mk_meta_eq} will |
|
938 |
be composed together and supplied below to {\tt setmksimps}. |
|
939 |
||
940 |
||
941 |
\subsection{Making the initial simpset} |
|
942 |
It is time to assemble these items. We open module {\tt Simplifier} to |
|
323 | 943 |
gain access to its components. We define the infix operator |
944 |
\ttindexbold{addcongs} to insert congruence rules; given a list of theorems, |
|
945 |
it converts their conclusions into meta-equalities and passes them to |
|
946 |
\ttindex{addeqcongs}. |
|
286 | 947 |
\begin{ttbox} |
948 |
open Simplifier; |
|
949 |
\ttbreak |
|
950 |
infix addcongs; |
|
951 |
fun ss addcongs congs = |
|
952 |
ss addeqcongs (congs RL [eq_reflection,iff_reflection]); |
|
953 |
\end{ttbox} |
|
954 |
The list {\tt IFOL_rews} contains the default rewrite rules for first-order |
|
955 |
logic. The first of these is the reflexive law expressed as the |
|
323 | 956 |
equivalence $(a=a)\bimp{\tt True}$; the rewrite rule $a=a$ is clearly useless. |
286 | 957 |
\begin{ttbox} |
958 |
val IFOL_rews = |
|
959 |
[refl RS P_iff_T] \at conj_rews \at disj_rews \at not_rews \at |
|
960 |
imp_rews \at iff_rews \at quant_rews; |
|
961 |
\end{ttbox} |
|
962 |
The list {\tt triv_rls} contains trivial theorems for the solver. Any |
|
963 |
subgoal that is simplified to one of these will be removed. |
|
964 |
\begin{ttbox} |
|
965 |
val notFalseI = int_prove_fun "~False"; |
|
966 |
val triv_rls = [TrueI,refl,iff_refl,notFalseI]; |
|
967 |
\end{ttbox} |
|
323 | 968 |
% |
286 | 969 |
The basic simpset for intuitionistic \FOL{} starts with \ttindex{empty_ss}. |
970 |
It preprocess rewrites using {\tt gen_all}, {\tt atomize} and {\tt |
|
971 |
mk_meta_eq}. It solves simplified subgoals using {\tt triv_rls} and |
|
2613 | 972 |
assumptions, and by detecting contradictions. |
973 |
It uses \ttindex{asm_simp_tac} to tackle subgoals of |
|
286 | 974 |
conditional rewrites. It takes {\tt IFOL_rews} as rewrite rules. |
975 |
Other simpsets built from {\tt IFOL_ss} will inherit these items. |
|
323 | 976 |
In particular, {\tt FOL_ss} extends {\tt IFOL_ss} with classical rewrite |
977 |
rules such as $\neg\neg P\bimp P$. |
|
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
978 |
\index{*setmksimps}\index{*setSSolver}\index{*setSolver}\index{*setsubgoaler} |
286 | 979 |
\index{*addsimps}\index{*addcongs} |
980 |
\begin{ttbox} |
|
2632 | 981 |
fun unsafe_solver prems = FIRST'[resolve_tac (triv_rls \at prems), |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
982 |
atac, etac FalseE]; |
2632 | 983 |
fun safe_solver prems = FIRST'[match_tac (triv_rls \at prems), |
2628
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
984 |
eq_assume_tac, ematch_tac [FalseE]]; |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
985 |
val IFOL_ss = empty_ss setsubgoaler asm_simp_tac |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
986 |
setSSolver safe_solver |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
987 |
setSolver unsafe_solver |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
988 |
setmksimps (map mk_meta_eq o atomize o gen_all) |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
989 |
addsimps IFOL_simps |
1fe7c9f599c2
description of del(eq)congs, safe and unsafe solver
oheimb
parents:
2613
diff
changeset
|
990 |
addcongs [imp_cong]; |
286 | 991 |
\end{ttbox} |
992 |
This simpset takes {\tt imp_cong} as a congruence rule in order to use |
|
993 |
contextual information to simplify the conclusions of implications: |
|
994 |
\[ \List{\Var{P}\bimp\Var{P'};\; \Var{P'} \Imp \Var{Q}\bimp\Var{Q'}} \Imp |
|
995 |
(\Var{P}\imp\Var{Q}) \bimp (\Var{P'}\imp\Var{Q'}) |
|
996 |
\] |
|
997 |
By adding the congruence rule {\tt conj_cong}, we could obtain a similar |
|
998 |
effect for conjunctions. |
|
999 |
||
1000 |
||
1001 |
\subsection{Case splitting} |
|
323 | 1002 |
To set up case splitting, we must prove the theorem below and pass it to |
1003 |
\ttindexbold{mk_case_split_tac}. The tactic \ttindexbold{split_tac} uses |
|
1004 |
{\tt mk_meta_eq}, defined above, to convert the splitting rules to |
|
1005 |
meta-equalities. |
|
286 | 1006 |
\begin{ttbox} |
1007 |
val meta_iffD = |
|
1008 |
prove_goal FOL.thy "[| P==Q; Q |] ==> P" |
|
1009 |
(fn [prem1,prem2] => [rewtac prem1, rtac prem2 1]) |
|
1010 |
\ttbreak |
|
1011 |
fun split_tac splits = |
|
1012 |
mk_case_split_tac meta_iffD (map mk_meta_eq splits); |
|
1013 |
\end{ttbox} |
|
1014 |
% |
|
323 | 1015 |
The splitter replaces applications of a given function; the right-hand side |
1016 |
of the replacement can be anything. For example, here is a splitting rule |
|
1017 |
for conditional expressions: |
|
286 | 1018 |
\[ \Var{P}(if(\Var{Q},\Var{x},\Var{y})) \bimp (\Var{Q} \imp \Var{P}(\Var{x})) |
1019 |
\conj (\lnot\Var{Q} \imp \Var{P}(\Var{y})) |
|
1020 |
\] |
|
323 | 1021 |
Another example is the elimination operator (which happens to be |
1022 |
called~$split$) for Cartesian products: |
|
286 | 1023 |
\[ \Var{P}(split(\Var{f},\Var{p})) \bimp (\forall a~b. \Var{p} = |
1024 |
\langle a,b\rangle \imp \Var{P}(\Var{f}(a,b))) |
|
1025 |
\] |
|
1026 |
Case splits should be allowed only when necessary; they are expensive |
|
1027 |
and hard to control. Here is a typical example of use, where {\tt |
|
1028 |
expand_if} is the first rule above: |
|
1029 |
\begin{ttbox} |
|
3112 | 1030 |
by (simp_tac (!simpset setloop (split_tac [expand_if])) 1); |
286 | 1031 |
\end{ttbox} |
1032 |
||
104 | 1033 |
|
1034 |
||
1035 |
\index{simplification|)} |
|
1036 |
||
286 | 1037 |