equal
deleted
inserted
replaced
17 "gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"; |
17 "gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"; |
18 |
18 |
19 text{*\noindent |
19 text{*\noindent |
20 According to the measure function, the second argument should decrease with |
20 According to the measure function, the second argument should decrease with |
21 each recursive call. The resulting termination condition |
21 each recursive call. The resulting termination condition |
22 @{term[display]"n ~= 0 ==> m mod n < n"} |
22 @{term[display]"n ~= (0::nat) ==> m mod n < n"} |
23 is proved automatically because it is already present as a lemma in |
23 is proved automatically because it is already present as a lemma in |
24 HOL\@. Thus the recursion equation becomes a simplification |
24 HOL\@. Thus the recursion equation becomes a simplification |
25 rule. Of course the equation is nonterminating if we are allowed to unfold |
25 rule. Of course the equation is nonterminating if we are allowed to unfold |
26 the recursive call inside the @{text else} branch, which is why programming |
26 the recursive call inside the @{text else} branch, which is why programming |
27 languages and our simplifier don't do that. Unfortunately the simplifier does |
27 languages and our simplifier don't do that. Unfortunately the simplifier does |
56 "gcd1 (m, n) = gcd1(n, m mod n)"; |
56 "gcd1 (m, n) = gcd1(n, m mod n)"; |
57 |
57 |
58 |
58 |
59 text{*\noindent |
59 text{*\noindent |
60 The order of equations is important: it hides the side condition |
60 The order of equations is important: it hides the side condition |
61 @{prop"n ~= 0"}. Unfortunately, in general the case distinction |
61 @{prop"n ~= (0::nat)"}. Unfortunately, in general the case distinction |
62 may not be expressible by pattern matching. |
62 may not be expressible by pattern matching. |
63 |
63 |
64 A simple alternative is to replace @{text if} by @{text case}, |
64 A simple alternative is to replace @{text if} by @{text case}, |
65 which is also available for @{typ bool} and is not split automatically: |
65 which is also available for @{typ bool} and is not split automatically: |
66 *} |
66 *} |