summaryrefslogtreecommitdiff
path: root/Source/Dafny/RefinementTransformer.cs
diff options
context:
space:
mode:
authorGravatar Rustan Leino <unknown>2014-08-19 23:51:35 -0700
committerGravatar Rustan Leino <unknown>2014-08-19 23:51:35 -0700
commitfeedce5021404af3f08dfc23944196d2ad8ed7fc (patch)
tree7709e8987982c3c420163099c7615742c7105fc3 /Source/Dafny/RefinementTransformer.cs
parent78e74bf9fa5ad7175cafd171427f58f556256e4a (diff)
Change behavior of 'decreases *', which can be applied to loops and methods. Now, loops that may possibly
do an infinite number of iterations (that is, loops marked with 'decreases *') and calls to methods marked with 'decreases *' are allowed only in methods that themselves are marked with 'decreases *'. As before, ghost loops and ghost methods are not allowed to be marked with 'decreases *'. Previously, 'decreases *' was allowed on a method only if the method was tail recursive; this is no longer so. Note, however, that if the method is not tail recursive and engages in infinite recursion, then it will eventually run out of stack space. Previously, a 'decreases *' was not inherited in a refining module; this is no longer so. That is, 'decreases *' is now inherited. To refine a possibly non-terminating method or loop, the refining version simply provides a decreases clause that does not mention '*'. Note that if the refined method is not recursive, it still needs to have _some_ decreases clause in order not to inherit the 'decreases *' from the refined method, but the expression stated in the decreases clause can be arbitrary (for example, one can write 'decreases true' or 'decreases 7' or 'decreases x' for some 'x' in scope). Note, in the new design, a method needs to be declared with 'decreases *' if it may recurse forever _or_ if it contains a possibly infinite loop. Note that the absence of 'decreases *' on a loop does not mean the loop will terminate, but it does mean that the loop will iterate a finite number of times (the subtle distinction here is that a loop without a 'decreases *' is allowed to contain a nested loop that has a 'decreases *' -- provided the enclosing method is also declared with 'decreases *', as previously mentioned).
Diffstat (limited to 'Source/Dafny/RefinementTransformer.cs')
-rw-r--r--Source/Dafny/RefinementTransformer.cs28
1 files changed, 16 insertions, 12 deletions
diff --git a/Source/Dafny/RefinementTransformer.cs b/Source/Dafny/RefinementTransformer.cs
index a7989cba..afdd640b 100644
--- a/Source/Dafny/RefinementTransformer.cs
+++ b/Source/Dafny/RefinementTransformer.cs
@@ -744,14 +744,20 @@ namespace Microsoft.Dafny
if (m.Mod.Expressions.Count != 0) {
reporter.Error(m.Mod.Expressions[0].E.tok, "a refining method is not allowed to extend the modifies clause");
}
+ // If the previous method was not specified with "decreases *", then the new method is not allowed to provide any "decreases" clause.
+ // Any "decreases *" clause is not inherited, so if the previous method was specified with "decreases *", then the new method needs
+ // to either redeclare "decreases *", provided a termination-checking "decreases" clause, or give no "decreases" clause and thus
+ // get a default "decreases" loop.
Specification<Expression> decreases;
- if (Contract.Exists(prevMethod.Decreases.Expressions, e => e is WildcardExpr)) {
- decreases = m.Decreases;
+ if (m.Decreases.Expressions.Count == 0) {
+ // inherited whatever the previous loop used
+ decreases = refinementCloner.CloneSpecExpr(prevMethod.Decreases);
} else {
- if (m.Decreases.Expressions.Count != 0) {
+ if (!Contract.Exists(prevMethod.Decreases.Expressions, e => e is WildcardExpr)) {
+ // If the previous loop was not specified with "decreases *", then the new loop is not allowed to provide any "decreases" clause.
reporter.Error(m.Decreases.Expressions[0].tok, "decreases clause on refining method not supported, unless the refined method was specified with 'decreases *'");
}
- decreases = refinementCloner.CloneSpecExpr(prevMethod.Decreases);
+ decreases = m.Decreases;
}
if (prevMethod.IsStatic != m.IsStatic) {
reporter.Error(m, "a method in a refining module cannot be changed from static to non-static or vice versa: {0}", m.Name);
@@ -1402,18 +1408,16 @@ namespace Microsoft.Dafny
// the Specification structures with a null list).
Contract.Assume(cNew.Mod.Expressions == null);
- // If the previous loop was not specified with "decreases *", then the new loop is not allowed to provide any "decreases" clause.
- // Any "decreases *" clause is not inherited, so if the previous loop was specified with "decreases *", then the new loop needs
- // to either redeclare "decreases *", provided a termination-checking "decreases" clause, or give no "decreases" clause and thus
- // get a default "decreases" loop.
Specification<Expression> decr;
- if (Contract.Exists(cOld.Decreases.Expressions, e => e is WildcardExpr)) {
- decr = cNew.Decreases; // take the new decreases clauses, whatever they may be (including nothing at all)
+ if (cNew.Decreases.Expressions.Count == 0) {
+ // inherited whatever the previous loop used
+ decr = refinementCloner.CloneSpecExpr(cOld.Decreases);
} else {
- if (cNew.Decreases.Expressions.Count != 0) {
+ if (!Contract.Exists(cOld.Decreases.Expressions, e => e is WildcardExpr)) {
+ // If the previous loop was not specified with "decreases *", then the new loop is not allowed to provide any "decreases" clause.
reporter.Error(cNew.Decreases.Expressions[0].tok, "a refining loop can provide a decreases clause only if the loop being refined was declared with 'decreases *'");
}
- decr = refinementCloner.CloneSpecExpr(cOld.Decreases);
+ decr = cNew.Decreases;
}
var invs = cOld.Invariants.ConvertAll(refinementCloner.CloneMayBeFreeExpr);