author | wenzelm |
Mon, 11 Sep 2023 19:30:48 +0200 | |
changeset 78659 | b5f3d1051b13 |
parent 76987 | 4c275405faae |
permissions | -rw-r--r-- |
9958 | 1 |
(*<*) |
48506
af1dabad14c0
avoid clash of Advanced/simp.thy vs. Misc/simp.thy;
wenzelm
parents:
19792
diff
changeset
|
2 |
theory simp2 imports Main begin |
9958 | 3 |
(*>*) |
4 |
||
67406 | 5 |
section\<open>Simplification\<close> |
9958 | 6 |
|
67406 | 7 |
text\<open>\label{sec:simplification-II}\index{simplification|(} |
11494 | 8 |
This section describes features not covered until now. It also |
9 |
outlines the simplification process itself, which can be helpful |
|
10 |
when the simplifier does not do what you expect of it. |
|
67406 | 11 |
\<close> |
9958 | 12 |
|
67406 | 13 |
subsection\<open>Advanced Features\<close> |
9958 | 14 |
|
67406 | 15 |
subsubsection\<open>Congruence Rules\<close> |
9958 | 16 |
|
67406 | 17 |
text\<open>\label{sec:simp-cong} |
11494 | 18 |
While simplifying the conclusion $Q$ |
13191 | 19 |
of $P \Imp Q$, it is legal to use the assumption $P$. |
11494 | 20 |
For $\Imp$ this policy is hardwired, but |
21 |
contextual information can also be made available for other |
|
69597 | 22 |
operators. For example, \<^prop>\<open>xs = [] --> xs@xs = xs\<close> simplifies to \<^term>\<open>True\<close> because we may use \<^prop>\<open>xs = []\<close> when simplifying \<^prop>\<open>xs@xs = |
23 |
xs\<close>. The generation of contextual information during simplification is |
|
9958 | 24 |
controlled by so-called \bfindex{congruence rules}. This is the one for |
69505 | 25 |
\<open>\<longrightarrow>\<close>: |
9958 | 26 |
@{thm[display]imp_cong[no_vars]} |
27 |
It should be read as follows: |
|
69597 | 28 |
In order to simplify \<^prop>\<open>P-->Q\<close> to \<^prop>\<open>P'-->Q'\<close>, |
29 |
simplify \<^prop>\<open>P\<close> to \<^prop>\<open>P'\<close> |
|
30 |
and assume \<^prop>\<open>P'\<close> when simplifying \<^prop>\<open>Q\<close> to \<^prop>\<open>Q'\<close>. |
|
9958 | 31 |
|
32 |
Here are some more examples. The congruence rules for bounded |
|
33 |
quantifiers supply contextual information about the bound variable: |
|
34 |
@{thm[display,eta_contract=false,margin=60]ball_cong[no_vars]} |
|
11494 | 35 |
One congruence rule for conditional expressions supplies contextual |
69505 | 36 |
information for simplifying the \<open>then\<close> and \<open>else\<close> cases: |
9958 | 37 |
@{thm[display]if_cong[no_vars]} |
11494 | 38 |
An alternative congruence rule for conditional expressions |
39 |
actually \emph{prevents} simplification of some arguments: |
|
9958 | 40 |
@{thm[display]if_weak_cong[no_vars]} |
41 |
Only the first argument is simplified; the others remain unchanged. |
|
42 |
This makes simplification much faster and is faithful to the evaluation |
|
43 |
strategy in programming languages, which is why this is the default |
|
69505 | 44 |
congruence rule for \<open>if\<close>. Analogous rules control the evaluation of |
45 |
\<open>case\<close> expressions. |
|
9958 | 46 |
|
11458 | 47 |
You can declare your own congruence rules with the attribute \attrdx{cong}, |
9958 | 48 |
either globally, in the usual manner, |
49 |
\begin{quote} |
|
69505 | 50 |
\isacommand{declare} \textit{theorem-name} \<open>[cong]\<close> |
9958 | 51 |
\end{quote} |
69505 | 52 |
or locally in a \<open>simp\<close> call by adding the modifier |
9958 | 53 |
\begin{quote} |
69505 | 54 |
\<open>cong:\<close> \textit{list of theorem names} |
9958 | 55 |
\end{quote} |
69505 | 56 |
The effect is reversed by \<open>cong del\<close> instead of \<open>cong\<close>. |
9958 | 57 |
|
58 |
\begin{warn} |
|
59 |
The congruence rule @{thm[source]conj_cong} |
|
60 |
@{thm[display]conj_cong[no_vars]} |
|
10885 | 61 |
\par\noindent |
62 |
is occasionally useful but is not a default rule; you have to declare it explicitly. |
|
9958 | 63 |
\end{warn} |
67406 | 64 |
\<close> |
9958 | 65 |
|
67406 | 66 |
subsubsection\<open>Permutative Rewrite Rules\<close> |
9958 | 67 |
|
67406 | 68 |
text\<open> |
11494 | 69 |
\index{rewrite rules!permutative|bold}% |
70 |
An equation is a \textbf{permutative rewrite rule} if the left-hand |
|
9958 | 71 |
side and right-hand side are the same up to renaming of variables. The most |
69597 | 72 |
common permutative rule is commutativity: \<^prop>\<open>x+y = y+x\<close>. Other examples |
73 |
include \<^prop>\<open>(x-y)-z = (x-z)-y\<close> in arithmetic and \<^prop>\<open>insert x (insert |
|
74 |
y A) = insert y (insert x A)\<close> for sets. Such rules are problematic because |
|
9958 | 75 |
once they apply, they can be used forever. The simplifier is aware of this |
76 |
danger and treats permutative rules by means of a special strategy, called |
|
77 |
\bfindex{ordered rewriting}: a permutative rewrite |
|
10978 | 78 |
rule is only applied if the term becomes smaller with respect to a fixed |
79 |
lexicographic ordering on terms. For example, commutativity rewrites |
|
69597 | 80 |
\<^term>\<open>b+a\<close> to \<^term>\<open>a+b\<close>, but then stops because \<^term>\<open>a+b\<close> is strictly |
81 |
smaller than \<^term>\<open>b+a\<close>. Permutative rewrite rules can be turned into |
|
69505 | 82 |
simplification rules in the usual manner via the \<open>simp\<close> attribute; the |
9958 | 83 |
simplifier recognizes their special status automatically. |
84 |
||
85 |
Permutative rewrite rules are most effective in the case of |
|
10281 | 86 |
associative-commutative functions. (Associativity by itself is not |
87 |
permutative.) When dealing with an AC-function~$f$, keep the |
|
9958 | 88 |
following points in mind: |
10281 | 89 |
\begin{itemize}\index{associative-commutative function} |
9958 | 90 |
|
91 |
\item The associative law must always be oriented from left to right, |
|
92 |
namely $f(f(x,y),z) = f(x,f(y,z))$. The opposite orientation, if |
|
93 |
used with commutativity, can lead to nontermination. |
|
94 |
||
95 |
\item To complete your set of rewrite rules, you must add not just |
|
96 |
associativity~(A) and commutativity~(C) but also a derived rule, {\bf |
|
97 |
left-com\-mut\-ativ\-ity} (LC): $f(x,f(y,z)) = f(y,f(x,z))$. |
|
98 |
\end{itemize} |
|
99 |
Ordered rewriting with the combination of A, C, and LC sorts a term |
|
100 |
lexicographically: |
|
101 |
\[\def\maps#1{~\stackrel{#1}{\leadsto}~} |
|
102 |
f(f(b,c),a) \maps{A} f(b,f(c,a)) \maps{C} f(b,f(a,c)) \maps{LC} f(a,f(b,c)) \] |
|
103 |
||
69505 | 104 |
Note that ordered rewriting for \<open>+\<close> and \<open>*\<close> on numbers is rarely |
10885 | 105 |
necessary because the built-in arithmetic prover often succeeds without |
11196 | 106 |
such tricks. |
67406 | 107 |
\<close> |
9958 | 108 |
|
67406 | 109 |
subsection\<open>How the Simplifier Works\<close> |
9958 | 110 |
|
67406 | 111 |
text\<open>\label{sec:SimpHow} |
11494 | 112 |
Roughly speaking, the simplifier proceeds bottom-up: subterms are simplified |
113 |
first. A conditional equation is only applied if its condition can be |
|
114 |
proved, again by simplification. Below we explain some special features of |
|
115 |
the rewriting process. |
|
67406 | 116 |
\<close> |
9958 | 117 |
|
67406 | 118 |
subsubsection\<open>Higher-Order Patterns\<close> |
9958 | 119 |
|
67406 | 120 |
text\<open>\index{simplification rule|(} |
10186 | 121 |
So far we have pretended the simplifier can deal with arbitrary |
11494 | 122 |
rewrite rules. This is not quite true. For reasons of feasibility, |
123 |
the simplifier expects the |
|
10186 | 124 |
left-hand side of each rule to be a so-called \emph{higher-order |
76987 | 125 |
pattern}~\<^cite>\<open>"nipkow-patterns"\<close>\indexbold{patterns!higher-order}. |
11494 | 126 |
This restricts where |
10186 | 127 |
unknowns may occur. Higher-order patterns are terms in $\beta$-normal |
11494 | 128 |
form. (This means there are no subterms of the form $(\lambda x. M)(N)$.) |
129 |
Each occurrence of an unknown is of the form |
|
10186 | 130 |
$\Var{f}~x@1~\dots~x@n$, where the $x@i$ are distinct bound |
10978 | 131 |
variables. Thus all ordinary rewrite rules, where all unknowns are |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
48985
diff
changeset
|
132 |
of base type, for example @{thm add.assoc}, are acceptable: if an unknown is |
10186 | 133 |
of base type, it cannot have any arguments. Additionally, the rule |
69505 | 134 |
\<open>(\<forall>x. ?P x \<and> ?Q x) = ((\<forall>x. ?P x) \<and> (\<forall>x. ?Q x))\<close> is also acceptable, in |
135 |
both directions: all arguments of the unknowns \<open>?P\<close> and |
|
136 |
\<open>?Q\<close> are distinct bound variables. |
|
10186 | 137 |
|
11494 | 138 |
If the left-hand side is not a higher-order pattern, all is not lost. |
139 |
The simplifier will still try to apply the rule provided it |
|
140 |
matches directly: without much $\lambda$-calculus hocus |
|
69505 | 141 |
pocus. For example, \<open>(?f ?x \<in> range ?f) = True\<close> rewrites |
69597 | 142 |
\<^term>\<open>g a \<in> range g\<close> to \<^const>\<open>True\<close>, but will fail to match |
69505 | 143 |
\<open>g(h b) \<in> range(\<lambda>x. g(h x))\<close>. However, you can |
11494 | 144 |
eliminate the offending subterms --- those that are not patterns --- |
145 |
by adding new variables and conditions. |
|
69505 | 146 |
In our example, we eliminate \<open>?f ?x\<close> and obtain |
147 |
\<open>?y = |
|
148 |
?f ?x \<Longrightarrow> (?y \<in> range ?f) = True\<close>, which is fine |
|
10186 | 149 |
as a conditional rewrite rule since conditions can be arbitrary |
11494 | 150 |
terms. However, this trick is not a panacea because the newly |
11196 | 151 |
introduced conditions may be hard to solve. |
10186 | 152 |
|
10885 | 153 |
There is no restriction on the form of the right-hand |
10186 | 154 |
sides. They may not contain extraneous term or type variables, though. |
67406 | 155 |
\<close> |
9958 | 156 |
|
67406 | 157 |
subsubsection\<open>The Preprocessor\<close> |
9958 | 158 |
|
67406 | 159 |
text\<open>\label{sec:simp-preprocessor} |
10885 | 160 |
When a theorem is declared a simplification rule, it need not be a |
10186 | 161 |
conditional equation already. The simplifier will turn it into a set of |
69597 | 162 |
conditional equations automatically. For example, \<^prop>\<open>f x = |
163 |
g x & h x = k x\<close> becomes the two separate |
|
164 |
simplification rules \<^prop>\<open>f x = g x\<close> and \<^prop>\<open>h x = k x\<close>. In |
|
10186 | 165 |
general, the input theorem is converted as follows: |
166 |
\begin{eqnarray} |
|
10885 | 167 |
\neg P &\mapsto& P = \hbox{\isa{False}} \nonumber\\ |
10186 | 168 |
P \longrightarrow Q &\mapsto& P \Longrightarrow Q \nonumber\\ |
169 |
P \land Q &\mapsto& P,\ Q \nonumber\\ |
|
170 |
\forall x.~P~x &\mapsto& P~\Var{x}\nonumber\\ |
|
171 |
\forall x \in A.\ P~x &\mapsto& \Var{x} \in A \Longrightarrow P~\Var{x} \nonumber\\ |
|
69505 | 172 |
\<open>if\<close>\ P\ \<open>then\<close>\ Q\ \<open>else\<close>\ R &\mapsto& |
10186 | 173 |
P \Longrightarrow Q,\ \neg P \Longrightarrow R \nonumber |
174 |
\end{eqnarray} |
|
175 |
Once this conversion process is finished, all remaining non-equations |
|
10885 | 176 |
$P$ are turned into trivial equations $P =\isa{True}$. |
177 |
For example, the formula |
|
69597 | 178 |
\begin{center}\<^prop>\<open>(p \<longrightarrow> t=u \<and> ~r) \<and> s\<close>\end{center} |
10845 | 179 |
is converted into the three rules |
10186 | 180 |
\begin{center} |
69597 | 181 |
\<^prop>\<open>p \<Longrightarrow> t = u\<close>,\quad \<^prop>\<open>p \<Longrightarrow> r = False\<close>,\quad \<^prop>\<open>s = True\<close>. |
10186 | 182 |
\end{center} |
183 |
\index{simplification rule|)} |
|
9958 | 184 |
\index{simplification|)} |
67406 | 185 |
\<close> |
9958 | 186 |
(*<*) |
187 |
end |
|
188 |
(*>*) |