Skip to content

Commit 996afe1

Browse files
marcoeilersAurel300
authored andcommitted
Update termination.md
- 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
1 parent 1a772f7 commit 996afe1

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/termination-custom-orders.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ v1 <_ v2 <==> decreasing(v1, v2) && bounded(v2)
4343
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:
4444

4545
```viper
46+
import <decreases/declaration.vpr>
47+
4648
domain MyIntWellFoundedOrder {
4749
axiom {
4850
forall i1: MyInt, i2: MyInt :: {decreasing(i1, i2)}
@@ -57,7 +59,8 @@ domain MyIntWellFoundedOrder {
5759

5860
> **Note**
5961
>
60-
> 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.
62+
> 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.
63+
> 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.
6164
6265
> **Exercise**
6366
> * Change the `factorial` function in the program above such that parameter `m` is used as its termination measure. The termination check should then fail because no well-founded order for `MyInt` has been defined.

src/termination-measures.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,22 @@ function length(this: Ref): Int
6565
>
6666
> Change the body of `length` to just the recursive call `length(this)`. Which error message do you expect?
6767
68+
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:
69+
70+
```silver-runnable
71+
adt List[T] {
72+
Nil()
73+
Cons(hd: T, tl: List[T])
74+
}
75+
76+
function llen(l: List[Int]): Int
77+
decreases l
78+
{
79+
l.isNil ? 0 : 1 + llen(l.tl)
80+
}
81+
```
82+
6883
Final remarks:
6984

7085
* 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.
71-
* For simplicity, all standard well-founded orders can be imported via `decreases/all.vpr`.
7286
* Users can also define [custom well-founded orders](./termination-custom-orders.md).

src/termination-mutual-recursion.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ function fun2(x: Int): Int
4747
At indirectly recursive calls, two decreases tuples are compared by lexicographical order of their longest commonly typed prefix (as does, e.g., Dafny). E.g., for the indirectly recursive call `fun2(y-1)` in function `fun1`, Viper verifies that `y-1 <_ y`, while for the recursive call `fun1(y, false)`, it verifies that `y <_ y || (y == y && false <_ b)`.
4848

4949
> **Exercise**
50-
> * Comment the import of `bool.vpr`, and reverify the program. Can you explain the resulting verification error?
5150
> * 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)`?

src/termination-syntax.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,3 @@ For the first recursive call `ack(m - 1, 1)`, and the second (outer) recursive c
3434
> **Exercise**
3535
>
3636
> Swap the tuple elements, i.e., change the decreases clause to `n, m`. For which of the recursive calls do you expect error messages?
37-
38-
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.

0 commit comments

Comments
 (0)