Hello:
I want to represent some relations in Dafny and state that they follow certain properties like transitivity, antisymmetry or totalness.
For example, suppose we want to declare a binary relation on a type T which is a total preorder relation. We may write the following predicate along with its three properties (Reflexive is superfluous because Total is harder, unless
A lemma might be used instead but it seems to be quite uncomfortable. I want to abstract from the particular relation definition. What is the better way to represent that in Dafny?
Thanks in advance.
I want to represent some relations in Dafny and state that they follow certain properties like transitivity, antisymmetry or totalness.
For example, suppose we want to declare a binary relation on a type T which is a total preorder relation. We may write the following predicate along with its three properties (Reflexive is superfluous because Total is harder, unless
==
does something strange, I think).predicate Leq<T>(x : T, y : T)
// Reflexive
ensures x == y ==> Leq(x, y)
// Transitive
ensures forall z : T :: Leq(x, z) && Leq(z, y) ==> Leq(x, y)
// Total
ensures Leq(x, y) || Leq(y, x)
method Main()
{
var a : int, b : int, c : int;
assume Leq(a, b);
assume Leq(b, c);
// Transitivity
assert Leq(a, c);
// Reflexivity
assert Leq(a, a);
var x : int, y : int;
assume !Leq(x, y);
// Totalness
assert Leq(y, x);
}
This function can be used in any other function or method taking advantage of its properties, as in this example. But the function verification itself fails and Dafny complains about termination in the second and third postconditions.A lemma might be used instead but it seems to be quite uncomfortable. I want to abstract from the particular relation definition. What is the better way to represent that in Dafny?
Thanks in advance.