(*<*) theory simplification imports Main begin (*>*)
text\<open>
Once we have proved all the termination conditions, the \isacommand{recdef}
recursion equations become simplification rules, just as with \isacommand{primrec}. In most cases this works fine, but there is a subtle
problem that must be mentioned: simplification may not
terminate because of automatic splitting of \<open>if\<close>. \index{*if expressions!splitting of} Let us look at an example: \<close>
consts gcd :: "nat\nat \ nat" recdef gcd "measure (\(m,n).n)" "gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"
text\<open>\noindent
According to the measure function, the second argument should decrease with
each recursive call. The resulting termination condition
@{term[display]"n ~= (0::nat) ==> m mod n < n"} is proved automatically because it is already present as a lemmain
HOL\@. Thus the recursion equation becomes a simplification
rule. Of course the equation is nonterminating if we are allowed to unfold
the recursive call inside the \<open>else\<close> branch, which is why programming
languages and our simplifier don't do that. Unfortunately the simplifier does
something else that leads to the same problem: it splits
each \<open>if\<close>-expression unless its
condition simplifies to @{term True} or @{term False}. For
example, simplification reduces
@{term[display]"gcd(m,n) = k"} in one step to
@{term[display]"(if n=0 then m else gcd(n, m mod n)) = k"} where the condition cannot be reduced further, and splitting leads to
@{term[display]"(n=0 --> m=k) & (n ~= 0 --> gcd(n, m mod n)=k)"}
Since the recursive call @{term"gcd(n, m mod n)"} is no longer protected by
an \<open>if\<close>, it is unfolded again, which leads to an infinite chain of
simplification steps. Fortunately, this problem can be avoided in many
different ways.
The most radical solution isto disable the offending theorem
@{thm[source]if_split},
as shown in\S\ref{sec:AutoCaseSplits}. However, we do not recommend this
approach: you will often havetoinvoke the rule explicitly when \<open>if\<close> is involved.
If possible, the definition should be given by pattern matching on the left
rather than \<open>if\<close> on the right. In the case of @{term gcd} the
following alternative definition suggests itself: \<close>
text\<open>\noindent
The order of equations is important: it hides the side condition
@{prop"n ~= (0::nat)"}. Unfortunately, in general the case distinction
may not be expressible by pattern matching.
A simple alternative isto replace \<open>if\<close> by \<open>case\<close>,
which isalso available for @{typ bool} andis not split automatically: \<close>
consts gcd2 :: "nat\nat \ nat" recdef gcd2 "measure (\(m,n).n)" "gcd2(m,n) = (case n=0 of True \ m | False \ gcd2(n,m mod n))"
text\<open>\noindent
This is probably the neatest solution nextto pattern matching, and it is
always available.
A final alternative isto replace the offending simplification rules by
derived conditional ones. For @{term gcd} it means we haveto prove
these lemmas: \<close>
lemma [simp]: "gcd (m, 0) = m" apply(simp) done
lemma [simp]: "n \ 0 \ gcd(m, n) = gcd(n, m mod n)" apply(simp) done
text\<open>\noindent
Simplification terminates for these proofs because the condition of the \<open>if\<close> simplifies to @{term True} or @{term False}.
Now we can disable the original simplification rule: \<close>
declare gcd.simps [simp del]
(*<*) end (*>*)
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.