You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Removed imports of well-founded orders
- Added naming convention for domains for custom well-founded orders
- Added example showing that ADTs can be used as termination measures
Copy file name to clipboardExpand all lines: tutorial/termination.md
+29-34Lines changed: 29 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,8 +23,6 @@ Here, `<tuple>` denotes the termination measure: a tuple of comma-separated expr
23
23
A common example for termination is the standard `factorial` function, which terminates because its argument decreases with respect to the usual well-founded order over non-negative numbers.
24
24
25
25
```silver-runnable
26
-
import <decreases/int.vpr>
27
-
28
26
function factorial(n:Int) : Int
29
27
requires 0 <= n
30
28
decreases n
@@ -37,27 +35,24 @@ Viper successfully verifies that `factorial` terminates: at each recursive invoc
Viper's standard library provides definitions of well-founded orders for most types built into Viper, all of which can be imported from the `decreases` folder. The following table lists all provided orders; we write `s1 <_ s2` if `s1` is less than `s2` with respect to the order.
38
+
Viper's standard library provides definitions of well-founded orders for most types built into Viper. The following table lists all provided orders; we write `s1 <_ s2` if `s1` is less than `s2` with respect to the order.
41
39
42
-
|Build-In Type<br>(file name)| Provided Well-Founded Order |
All definitions are straightforward, except the last one, which is concerned with using predicate instances as termination measures. Due to the least fixed-point interpretation of predicates, any predicate instance has a finite depth, i.e., can be unfolded only a finite number of times. This implies that a predicate instance `p1`, which is nested inside a predicate instance `p2`, has a smaller (and non-negative) depth than `p2`.
55
52
56
53
Viper uses this nesting depth to enable termination checks based on predicate instances, as illustrated by the next example, the recursive computation of the length of a linked list: intuitively, the remainder of the linked list, represented by predicate instance `list(this)`, is used as the termination measure. This works because the recursive call is nested under the unfolding of `list(this)`, and takes the smaller predicate instance `list(this.next)`.
57
54
58
55
```silver-runnable
59
-
import <decreases/predicate_instance.vpr>
60
-
61
56
field next: Ref
62
57
63
58
predicate list(this: Ref) {
@@ -77,10 +72,24 @@ function length(this: Ref): Int
77
72
Change the body of `length` to just the recursive call `length(this)`. Which error message do you expect?
78
73
///
79
74
75
+
Additionally, Viper's ADT plugin automatically generated well-founded orders for any ADT defined in the program. Thus, ADT values can also be used as termination measures:
76
+
77
+
```silver-runnable
78
+
adt List[T] {
79
+
Nil()
80
+
Cons(hd: T, tl: List[T])
81
+
}
82
+
83
+
function llen(l: List[Int]): Int
84
+
decreases l
85
+
{
86
+
l.isNil ? 0 : 1 + llen(l.tl)
87
+
}
88
+
```
89
+
80
90
Final remarks:
81
91
82
92
* Note that `PredicateInstance` is an internal Viper type, and currently supported only in decreases tuples. The `nested` function is also internal and cannot be used by users.
83
-
* For simplicity, all standard well-founded orders can be imported via `decreases/all.vpr`.
84
93
* Users can also define [custom well-founded orders](#term_custom_orders).
85
94
86
95
## General Syntax of Decreases Tuples
@@ -100,8 +109,6 @@ Special cases, such as empty tuples, tuples of different length, and tuples of d
100
109
A typical example of a function for which a tuple as termination measure is used, is the Ackermann function:
101
110
102
111
```silver-runnable
103
-
import <decreases/int.vpr>
104
-
105
112
function ack(m:Int, n:Int):Int
106
113
decreases m, n
107
114
requires m >= 0
@@ -120,8 +127,6 @@ For the first recursive call `ack(m - 1, 1)`, and the second (outer) recursive c
120
127
Swap the tuple elements, i.e., change the decreases clause to `n, m`. For which of the recursive calls do you expect error messages?
121
128
///
122
129
123
-
The well-founded order over tuples need not be imported (and cannot be customised). However, the well-founded orders of the types appearing in the tuple must be.
124
-
125
130
## Special Decreases Tuples
126
131
127
132
Viper supports three decreases tuples whose semantics require further explanation.
@@ -189,8 +194,6 @@ As previously mentioned, Viper offers [predefined orders](#term_prov_wfo) for it
189
194
In the remainder of this subsection, both approaches will be illustrated using a combination of the `MyInt` example (from the earlier subsection on domains) and a `factorial` function operating on `MyInt`. In the example below, the destructor `get` is used to map a `MyInt` to a regular `Int`, which indirectly allows using `MyInt` in the function's decreases clause.
The necessary properties of `decreasing` and `bounded` for values of type `T` can be defined via axioms. For the `MyInt` type from before, suitable axioms would be:
The functions `decreasing` and `bounded` must be declared by the Viper program to verify, which is easiest done by importing `decreases/declaration.vpr`. This is also what the predefined orders, e.g., `decreases/int.vpr`, do.
248
+
The functions `decreasing` and `bounded` must be declared by the Viper program to verify, which is easiest done by importing `decreases/declaration.vpr`, as shown in the example. This is also what the predefined orders do.
249
+
Viper uses a naming convention where the well-founded order for type `T` should be defined in a domain called `TWellFoundedOrder`; giving it a different name will result in a warning.
244
250
///
245
251
246
252
//exercise//
@@ -257,8 +263,6 @@ For mutually recursive functions, Viper implements the following approach (as, e
257
263
A simple case of mutual recursion is illustrated next, by functions `is_even` and `is_odd`:
258
264
259
265
```silver-runnable
260
-
import <decreases/int.vpr>
261
-
262
266
function is_even(x: Int): Bool
263
267
requires x >= 0
264
268
decreases x
@@ -279,9 +283,6 @@ Consider function `is_even`: its termination measure `x` decreases at the indire
279
283
In the example above, the two termination measures are tuples of equal length and type. However, this is not required of mutually recursive functions in order to prove their termination. Consider the next example (which verifies successfully):
280
284
281
285
```silver-runnable
282
-
import <decreases/int.vpr>
283
-
import <decreases/bool.vpr>
284
-
285
286
function fun1(y: Int, b: Bool): Int
286
287
decreases y, b
287
288
{
@@ -300,8 +301,6 @@ At indirectly recursive calls, two decreases tuples are compared by lexicographi
300
301
301
302
//exercise//
302
303
303
-
* Comment the import of `bool.vpr`, and reverify the program. Can you explain the resulting verification error?
304
-
305
304
* In the above example, change the call `fun1(x-1, true)` to `fun1(x, true)` -- the program still verifies. That's because Viper appends a `Top` element (an internal value of any type, larger than any other value) to each tuple, a neat trick also implemented by, e.g., Dafny and F*. Can you explain how this helps with checking termination of the call `fun1(x, true)`?
306
305
307
306
///
@@ -326,8 +325,6 @@ To ensure soundness, only a *single* clause per kind of measure is allowed. More
326
325
The following example illustrates combined conditional termination clauses: function `sign` promises to decrease `x` if positive, and something (wildcard) if `x` is negative. In case `x` is zero, function `sign` does not (promise to) terminate.
327
326
328
327
```silver-runnable
329
-
import <decreases/int.vpr>
330
-
331
328
function sign(x: Int): Int
332
329
decreases x if 1 <= x
333
330
decreases _ if x <= -1
@@ -431,8 +428,6 @@ The currently implemented approach to checking termination of methods is similar
431
428
A straightforward example is method `sum`, shown next:
0 commit comments