Indexset3
Here, we prove a basic fact about finite sets: is X and Y are disjoint subsets of S, then |X| + |Y| <= |S|. This can be used to show that any two majorities of S have an element in common.
We set up the problem with two types: the basis type (which we can think of as ordinal numbers) and the index type (which we can think of as cardinal numbers). Both are isomorphic to the integers. Our sets are subsets of the basis type, while their cardinalities a members of the index type. The separation of these two types is used to keep us in the "essentially uninterpreted" fragment, as we will see below.
We definte a module that takes basis and index types, and provides the following interface:
1) a type set representing finite subsets of the basis type
2) a basis element n that is an upper bound on set elements (so the basis is finite)
3) a member relation
4) a relation disjoint beteween sets
5) a function card giving the cardinality of a set as an index
6) a function cnt gives the cardinality of the set of basis elements less than E
7) an action empty returning the empty set
8) an action add that adds an element to a set
The spec can then be stated as disjoint(X,Y) -> card(X)+card(Y) <= cnt(n).
The implementation defines card and cnt as recursive functions over the basis type.
We then give an inductive invariant with parameter B. This states that the theorem holds for elements of the sets X and Y less than B. This fact is proved by induction on B, using the induction schema for the basis type.
This all seems straightforward, but it was tricky to figure out how to set up the problem without function cycles. The key was to give ordinals and cardinal distinct types. The functions are stratified in this order: set -> basis.t -> index.t. In particular, this allows us to state the inductive lemma in a way that does not involve quantified variables under arithmetic operators.
include collections
include order
module indexset(basis,index) = {
type set
individual n : basis.t
relation member(E:basis.t,S:set)
function card(S:set) : index.t
relation majority(S:set)
action empty returns(s:set)
action add(s:set,e:basis.t) returns (s:set)
axiom [n_positive] n > 0
object spec = {
after empty {
assert ~member(E,s);
}
after add {
assert member(X,s) <-> (member(X,old s) | X = e)
}
}
function cnt(E:basis.t) : index.t
relation disjoint(X:set,Y:set)
isolate disjointness = {
Note here we use a constant symbol x for the argument. This tells IVy that the defintion should only be unfolded for ground terms x occurring in the proof goal. Without this, we would exit the decidable fragment, due to a quantified variable under an arithmetic operator.
definition cnt(x:basis.t) = 0 if x <= 0 else cnt(x-1) + 1
proof rec[basis.t]
function cardUpTo(S:set,B:basis.t) : index.t
definition cardUpTo(s:set,b:basis.t) =
0 if b <= 0 else (cardUpTo(s,b-1)+1 if member(b-1,s) else cardUpTo(s,b-1))
proof rec[basis.t]
definition card(S) = cardUpTo(S,n)
definition disjoint(X,Y) = forall E. ~(member(E,X) & member(E,Y))
definition majority(X) = 2 * card(X) > cnt(n)
Most importantly, notice how arithmetic is used here, We have a + operation over a quantified variable B. However, B is of ordinal type, while the + operator is over cardinals. This means the formulas in our proof context are still essentially uninterpreted. Without the separate types, IVy would complain about this property. Question: is it possible to automatically detect this kind of sorting of theories so we don't have to explicitly use distinct types?
property disjoint(Xa,Y) -> cardUpTo(Xa,B) + cardUpTo(Y,B) <= cnt(B)
proof ind[basis.t] with X = B
property disjoint(X,Y) -> card(X) + card(Y) <= cnt(n)
property majority(X) & majority(Y) -> exists E. (member(E,X) & member(E,Y))
interpret index.t -> int
interpret basis.t -> int
}
with nodeset.n_positive
nodeset.n_positive is a workaround to an IVY bug. We
should be able to say just n_positive.
isolate api = {
instance arridx : unbounded_sequence
instance arr:array(arridx.t,basis.t)
set into a struct
with just one field called repr. This field gives the concrete representation of a
set as an array. To an isolate that doesn't use the definition of member below,
the tpye set will still appear to be uninterpreted.
destructor repr(X:set) : arr.t
definition member(y:basis.t,X:set) = exists Z. 0 <= Z & Z < repr(X).end & repr(X).value(Z) = y
implement empty {
repr(s) := arr.create(0,0)
}
implement add {
if ~member(e,s) {
repr(s) := repr(s).resize(repr(s).end.next,e)
}
}
} with spec
}
instance nodeord : unbounded_sequence
instance nodecard : unbounded_sequence
instance nodeset : indexset(nodeord,nodecard)
export nodeset.empty
export nodeset.add