3 \def\isabellecontext{simp}%
5 \isamarkupsection{Simplification}
8 \label{sec:simplification-II}\index{simplification|(}
9 This section discusses some additional nifty features not covered so far and
10 gives a short introduction to the simplification process itself. The latter
11 is helpful to understand why a particular rule does or does not apply in some
15 \isamarkupsubsection{Advanced features}
17 \isamarkupsubsubsection{Congruence rules}
19 \begin{isamarkuptext}%
21 It is hardwired into the simplifier that while simplifying the conclusion $Q$
22 of $P \isasymImp Q$ it is legal to make uses of the assumptions $P$. This
23 kind of contextual information can also be made available for other
24 operators. For example, \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongrightarrow}\ xs\ {\isacharat}\ xs\ {\isacharequal}\ xs} simplifies to \isa{True} because we may use \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} when simplifying \isa{xs\ {\isacharat}\ xs\ {\isacharequal}\ xs}. The generation of contextual information during simplification is
25 controlled by so-called \bfindex{congruence rules}. This is the one for
26 \isa{{\isasymlongrightarrow}}:
28 \ \ \ \ \ {\isasymlbrakk}P\ {\isacharequal}\ P{\isacharprime}{\isacharsemicolon}\ P{\isacharprime}\ {\isasymLongrightarrow}\ Q\ {\isacharequal}\ Q{\isacharprime}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}P\ {\isasymlongrightarrow}\ Q{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}P{\isacharprime}\ {\isasymlongrightarrow}\ Q{\isacharprime}{\isacharparenright}%
30 It should be read as follows:
31 In order to simplify \isa{P\ {\isasymlongrightarrow}\ Q} to \isa{P{\isacharprime}\ {\isasymlongrightarrow}\ Q{\isacharprime}},
32 simplify \isa{P} to \isa{P{\isacharprime}}
33 and assume \isa{P{\isacharprime}} when simplifying \isa{Q} to \isa{Q{\isacharprime}}.
35 Here are some more examples. The congruence rules for bounded
36 quantifiers supply contextual information about the bound variable:
38 \ \ \ \ \ {\isasymlbrakk}A\ {\isacharequal}\ B{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ x\ {\isasymin}\ B\ {\isasymLongrightarrow}\ P\ x\ {\isacharequal}\ Q\ x{\isasymrbrakk}\isanewline
39 \ \ \ \ \ {\isasymLongrightarrow}\ {\isacharparenleft}{\isasymforall}x{\isasymin}A{\isachardot}\ P\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isasymforall}x{\isasymin}B{\isachardot}\ Q\ x{\isacharparenright}%
41 The congruence rule for conditional expressions supply contextual
42 information for simplifying the arms:
44 \ \ \ \ \ {\isasymlbrakk}b\ {\isacharequal}\ c{\isacharsemicolon}\ c\ {\isasymLongrightarrow}\ x\ {\isacharequal}\ u{\isacharsemicolon}\ {\isasymnot}\ c\ {\isasymLongrightarrow}\ y\ {\isacharequal}\ v{\isasymrbrakk}\isanewline
45 \ \ \ \ \ {\isasymLongrightarrow}\ {\isacharparenleft}if\ b\ then\ x\ else\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ c\ then\ u\ else\ v{\isacharparenright}%
47 A congruence rule can also \emph{prevent} simplification of some arguments.
48 Here is an alternative congruence rule for conditional expressions:
50 \ \ \ \ \ b\ {\isacharequal}\ c\ {\isasymLongrightarrow}\ {\isacharparenleft}if\ b\ then\ x\ else\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ c\ then\ x\ else\ y{\isacharparenright}%
52 Only the first argument is simplified; the others remain unchanged.
53 This makes simplification much faster and is faithful to the evaluation
54 strategy in programming languages, which is why this is the default
55 congruence rule for \isa{if}. Analogous rules control the evaluaton of
56 \isa{case} expressions.
58 You can delare your own congruence rules with the attribute \isa{cong},
59 either globally, in the usual manner,
61 \isacommand{declare} \textit{theorem-name} \isa{{\isacharbrackleft}cong{\isacharbrackright}}
63 or locally in a \isa{simp} call by adding the modifier
65 \isa{cong{\isacharcolon}} \textit{list of theorem names}
67 The effect is reversed by \isa{cong\ del} instead of \isa{cong}.
70 The congruence rule \isa{conj{\isacharunderscore}cong}
72 \ \ \ \ \ {\isasymlbrakk}P\ {\isacharequal}\ P{\isacharprime}{\isacharsemicolon}\ P{\isacharprime}\ {\isasymLongrightarrow}\ Q\ {\isacharequal}\ Q{\isacharprime}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}P\ {\isasymand}\ Q{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}P{\isacharprime}\ {\isasymand}\ Q{\isacharprime}{\isacharparenright}%
74 is occasionally useful but not a default rule; you have to use it explicitly.
78 \isamarkupsubsubsection{Permutative rewrite rules}
80 \begin{isamarkuptext}%
81 \index{rewrite rule!permutative|bold}
82 \index{rewriting!ordered|bold}
83 \index{ordered rewriting|bold}
84 \index{simplification!ordered|bold}
85 An equation is a \bfindex{permutative rewrite rule} if the left-hand
86 side and right-hand side are the same up to renaming of variables. The most
87 common permutative rule is commutativity: \isa{x\ {\isacharplus}\ y\ {\isacharequal}\ y\ {\isacharplus}\ x}. Other examples
88 include \isa{x\ {\isacharminus}\ y\ {\isacharminus}\ z\ {\isacharequal}\ x\ {\isacharminus}\ z\ {\isacharminus}\ y} in arithmetic and \isa{insert\ x\ {\isacharparenleft}insert\ y\ A{\isacharparenright}\ {\isacharequal}\ insert\ y\ {\isacharparenleft}insert\ x\ A{\isacharparenright}} for sets. Such rules are problematic because
89 once they apply, they can be used forever. The simplifier is aware of this
90 danger and treats permutative rules by means of a special strategy, called
91 \bfindex{ordered rewriting}: a permutative rewrite
92 rule is only applied if the term becomes ``smaller'' (w.r.t.\ some fixed
93 lexicographic ordering on terms). For example, commutativity rewrites
94 \isa{b\ {\isacharplus}\ a} to \isa{a\ {\isacharplus}\ b}, but then stops because \isa{a\ {\isacharplus}\ b} is strictly
95 smaller than \isa{b\ {\isacharplus}\ a}. Permutative rewrite rules can be turned into
96 simplification rules in the usual manner via the \isa{simp} attribute; the
97 simplifier recognizes their special status automatically.
99 Permutative rewrite rules are most effective in the case of
100 associative-commutative functions. (Associativity by itself is not
101 permutative.) When dealing with an AC-function~$f$, keep the
102 following points in mind:
103 \begin{itemize}\index{associative-commutative function}
105 \item The associative law must always be oriented from left to right,
106 namely $f(f(x,y),z) = f(x,f(y,z))$. The opposite orientation, if
107 used with commutativity, can lead to nontermination.
109 \item To complete your set of rewrite rules, you must add not just
110 associativity~(A) and commutativity~(C) but also a derived rule, {\bf
111 left-com\-mut\-ativ\-ity} (LC): $f(x,f(y,z)) = f(y,f(x,z))$.
113 Ordered rewriting with the combination of A, C, and LC sorts a term
115 \[\def\maps#1{~\stackrel{#1}{\leadsto}~}
116 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)) \]
118 Note that ordered rewriting for \isa{{\isacharplus}} and \isa{{\isacharasterisk}} on numbers is rarely
119 necessary because the builtin arithmetic capabilities often take care of
123 \isamarkupsubsection{How it works}
125 \begin{isamarkuptext}%
127 Roughly speaking, the simplifier proceeds bottom-up (subterms are simplified
128 first) and a conditional equation is only applied if its condition could be
129 proved (again by simplification). Below we explain some special features of the rewriting process.%
132 \isamarkupsubsubsection{Higher-order patterns}
134 \begin{isamarkuptext}%
135 \index{simplification rule|(}
136 So far we have pretended the simplifier can deal with arbitrary
137 rewrite rules. This is not quite true. Due to efficiency (and
138 potentially also computability) reasons, the simplifier expects the
139 left-hand side of each rule to be a so-called \emph{higher-order
140 pattern}~\cite{nipkow-patterns}\indexbold{higher-order
141 pattern}\indexbold{pattern, higher-order}. This restricts where
142 unknowns may occur. Higher-order patterns are terms in $\beta$-normal
143 form (this will always be the case unless you have done something
144 strange) where each occurrence of an unknown is of the form
145 $\Var{f}~x@1~\dots~x@n$, where the $x@i$ are distinct bound
146 variables. Thus all ``standard'' rewrite rules, where all unknowns are
147 of base type, for example \isa{{\isacharquery}m\ {\isacharplus}\ {\isacharquery}n\ {\isacharplus}\ {\isacharquery}k\ {\isacharequal}\ {\isacharquery}m\ {\isacharplus}\ {\isacharparenleft}{\isacharquery}n\ {\isacharplus}\ {\isacharquery}k{\isacharparenright}}, are OK: if an unknown is
148 of base type, it cannot have any arguments. Additionally, the rule
149 \isa{{\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}P\ x\ {\isasymand}\ {\isacharquery}Q\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}P\ x{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}Q\ x{\isacharparenright}{\isacharparenright}} is also OK, in
150 both directions: all arguments of the unknowns \isa{{\isacharquery}P} and
151 \isa{{\isacharquery}Q} are distinct bound variables.
153 If the left-hand side is not a higher-order pattern, not all is lost
154 and the simplifier will still try to apply the rule, but only if it
155 matches ``directly'', i.e.\ without much $\lambda$-calculus hocus
156 pocus. For example, \isa{{\isacharquery}f\ {\isacharquery}x\ {\isasymin}\ range\ {\isacharquery}f\ {\isacharequal}\ True} rewrites
157 \isa{g\ a\ {\isasymin}\ range\ g} to \isa{True}, but will fail to match
158 \isa{g{\isacharparenleft}h\ b{\isacharparenright}\ {\isasymin}\ range{\isacharparenleft}{\isasymlambda}x{\isachardot}\ g{\isacharparenleft}h\ x{\isacharparenright}{\isacharparenright}}. However, you can
159 replace the offending subterms (in our case \isa{{\isacharquery}f\ {\isacharquery}x}, which
160 is not a pattern) by adding new variables and conditions: \isa{{\isacharquery}y\ {\isacharequal}\ {\isacharquery}f\ {\isacharquery}x\ {\isasymLongrightarrow}\ {\isacharquery}y\ {\isasymin}\ range\ {\isacharquery}f\ {\isacharequal}\ True} is fine
161 as a conditional rewrite rule since conditions can be arbitrary
162 terms. However, this trick is not a panacea because the newly
163 introduced conditions may be hard to prove, which has to take place
164 before the rule can actually be applied.
166 There is basically no restriction on the form of the right-hand
167 sides. They may not contain extraneous term or type variables, though.%
170 \isamarkupsubsubsection{The preprocessor}
172 \begin{isamarkuptext}%
173 When some theorem is declared a simplification rule, it need not be a
174 conditional equation already. The simplifier will turn it into a set of
175 conditional equations automatically. For example, given \isa{f\ x\ {\isacharequal}\ g\ x\ {\isasymand}\ h\ x\ {\isacharequal}\ k\ x} the simplifier will turn this into the two separate
176 simplifiction rules \isa{f\ x\ {\isacharequal}\ g\ x} and \isa{h\ x\ {\isacharequal}\ k\ x}. In
177 general, the input theorem is converted as follows:
179 \neg P &\mapsto& P = False \nonumber\\
180 P \longrightarrow Q &\mapsto& P \Longrightarrow Q \nonumber\\
181 P \land Q &\mapsto& P,\ Q \nonumber\\
182 \forall x.~P~x &\mapsto& P~\Var{x}\nonumber\\
183 \forall x \in A.\ P~x &\mapsto& \Var{x} \in A \Longrightarrow P~\Var{x} \nonumber\\
184 \isa{if}\ P\ \isa{then}\ Q\ \isa{else}\ R &\mapsto&
185 P \Longrightarrow Q,\ \neg P \Longrightarrow R \nonumber
187 Once this conversion process is finished, all remaining non-equations
188 $P$ are turned into trivial equations $P = True$.
189 For example, the formula \isa{{\isacharparenleft}p\ {\isasymlongrightarrow}\ q\ {\isasymand}\ r{\isacharparenright}\ {\isasymand}\ s} is converted into the three rules
191 \isa{p\ {\isasymLongrightarrow}\ q\ {\isacharequal}\ True},\quad \isa{p\ {\isasymLongrightarrow}\ r\ {\isacharequal}\ True},\quad \isa{s\ {\isacharequal}\ True}.
193 \index{simplification rule|)}
194 \index{simplification|)}%
199 %%% TeX-master: "root"