Theory Reference: Sets and Relations

Finite Sets

cvc5 supports the theory of finite sets using the following sorts, constants, functions and predicates. More details can be found in [BBRT17].

For the C++ API examples in the table below, we assume that we have created a cvc5::Solver solver object.

SMTLIB language

C++ API

Logic String

append FS for finite sets

(set-logic QF_UFLIAFS)

append FS for finite sets

solver.setLogic("QF_UFLIAFS");

Sort

(Set <Sort>)

solver.mkSetSort(cvc5::Sort elementSort);

Constants

(declare-const X (Set Int))

Sort s = solver.mkSetSort(solver.getIntegerSort());

Term X = solver.mkConst(s, "X");

Union

(set.union X Y)

Term Y = solver.mkConst(s, "Y");

Term t = solver.mkTerm(Kind::SET_UNION, {X, Y});

Intersection

(set.inter X Y)

Term t = solver.mkTerm(Kind::SET_INTER, {X, Y});

Minus

(set.minus X Y)

Term t = solver.mkTerm(Kind::SET_MINUS, {X, Y});

Membership

(set.member x X)

Term x = solver.mkConst(solver.getIntegerSort(), "x");

Term t = solver.mkTerm(Kind::SET_MEMBER, {x, X});

Subset

(set.subset X Y)

Term t = solver.mkTerm(Kind::SET_SUBSET, {X, Y});

Emptyset

(as set.empty (Set Int))

Term t = solver.mkEmptySet(s);

Singleton Set

(set.singleton 1)

Term t = solver.mkTerm(Kind::SET_SINGLETON, {solver.mkInteger(1)});

Emptyset tester

(set.is_empty X)

Term t = solver.mkTerm(Kind::SET_IS_EMPTY, {X});

Singleton tester

(set.is_singleton X)

Term t = solver.mkTerm(Kind::SET_IS_SINGLETON, {X});

Cardinality

(set.card X)

Term t = solver.mkTerm(Kind::SET_CARD, {X});

Insert / Finite Sets

(set.insert 1 2 3 (set.singleton 4))

Term one = solver.mkInteger(1);

Term two = solver.mkInteger(2);

Term three = solver.mkInteger(3);

Term sgl = solver.mkTerm(Kind::SET_SINGLETON, {solver.mkInteger(4)});

Term t = solver.mkTerm(Kind::SET_INSERT, {one, two, three, sgl});

Complement

(set.complement X)

Term t = solver.mkTerm(Kind::SET_COMPLEMENT, {X});

Universe Set

(as set.universe (Set Int))

Term t = solver.mkUniverseSet(s);

Semantics

The semantics of most of the above operators (e.g., set.union, set.inter, difference) are straightforward. The semantics for the universe set and complement are more subtle and explained in the following.

The universe set (as set.universe (Set T)) is not interpreted as the set containing all elements of sort T. Instead it may be interpreted as any set such that all sets of sort (Set T) are interpreted as subsets of it. In other words, it is the union of the interpretations of all (finite) sets in our input.

For example:

(declare-fun x () (Set Int))
(declare-fun y () (Set Int))
(declare-fun z () (Set Int))
(assert (set.member 0 x))
(assert (set.member 1 y))
(assert (= z (as set.universe (Set Int))))
(check-sat)

Here, a possible model is:

(define-fun x () (set.singleton 0))
(define-fun y () (set.singleton 1))
(define-fun z () (set.union (set.singleton 1) (set.singleton 0)))

Notice that the universe set in this example is interpreted the same as z, and is such that all sets in this example (x, y, and z) are subsets of it.

The set complement operator for (Set T) is interpreted relative to the interpretation of the universe set for (Set T), and not relative to the set of all elements of sort T. That is, for all sets X of sort (Set T), the complement operator is such that (= (set.complement X) (set.minus (as set.universe (Set T)) X)) holds in all models.

The motivation for these semantics is to ensure that the universe set for sort T and applications of set complement can always be interpreted as a finite set in (quantifier-free) inputs, even if the cardinality of T is infinite. Above, notice that we were able to find a model for the universe set of sort (Set Int) that contained two elements only.

Note

In the presence of quantifiers, cvc5’s implementation of the above theory allows infinite sets. In particular, the following formula is SAT (even though cvc5 is not able to say this):

(set-logic ALL)
(declare-fun x () (Set Int))
(assert (forall ((z Int) (set.member (* 2 z) x)))
(check-sat)

The reason for that is that making this formula (and similar ones) unsat is counter-intuitive when quantifiers are present.

Below is a more extensive example on how to use finite sets:

examples/api/cpp/sets.cpp

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Aina Niemetz, Kshitij Bansal, Andrew Reynolds
 4 *
 5 * This file is part of the cvc5 project.
 6 *
 7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
 8 * in the top-level source directory and their institutional affiliations.
 9 * All rights reserved.  See the file COPYING in the top-level source
10 * directory for licensing information.
11 * ****************************************************************************
12 *
13 * A simple demonstration of reasoning about sets with cvc5.
14 */
15
16#include <cvc5/cvc5.h>
17
18#include <iostream>
19
20using namespace std;
21using namespace cvc5;
22
23int main()
24{
25  TermManager tm;
26  Solver slv(tm);
27
28  // Optionally, set the logic. We need at least UF for equality predicate,
29  // integers (LIA) and sets (FS).
30  slv.setLogic("QF_UFLIAFS");
31
32  // Produce models
33  slv.setOption("produce-models", "true");
34  slv.setOption("output-language", "smt2");
35
36  Sort integer = tm.getIntegerSort();
37  Sort set = tm.mkSetSort(integer);
38
39  // Verify union distributions over intersection
40  // (A union B) intersection C = (A intersection C) union (B intersection C)
41  {
42    Term A = tm.mkConst(set, "A");
43    Term B = tm.mkConst(set, "B");
44    Term C = tm.mkConst(set, "C");
45
46    Term unionAB = tm.mkTerm(Kind::SET_UNION, {A, B});
47    Term lhs = tm.mkTerm(Kind::SET_INTER, {unionAB, C});
48
49    Term intersectionAC = tm.mkTerm(Kind::SET_INTER, {A, C});
50    Term intersectionBC = tm.mkTerm(Kind::SET_INTER, {B, C});
51    Term rhs = tm.mkTerm(Kind::SET_UNION, {intersectionAC, intersectionBC});
52
53    Term theorem = tm.mkTerm(Kind::EQUAL, {lhs, rhs});
54
55    cout << "cvc5 reports: " << theorem << " is "
56         << slv.checkSatAssuming(theorem.notTerm()) << "." << endl;
57  }
58
59  // Verify emptset is a subset of any set
60  {
61    Term A = tm.mkConst(set, "A");
62    Term emptyset = tm.mkEmptySet(set);
63
64    Term theorem = tm.mkTerm(Kind::SET_SUBSET, {emptyset, A});
65
66    cout << "cvc5 reports: " << theorem << " is "
67         << slv.checkSatAssuming(theorem.notTerm()) << "." << endl;
68  }
69
70  // Find me an element in {1, 2} intersection {2, 3}, if there is one.
71  {
72    Term one = tm.mkInteger(1);
73    Term two = tm.mkInteger(2);
74    Term three = tm.mkInteger(3);
75
76    Term singleton_one = tm.mkTerm(Kind::SET_SINGLETON, {one});
77    Term singleton_two = tm.mkTerm(Kind::SET_SINGLETON, {two});
78    Term singleton_three = tm.mkTerm(Kind::SET_SINGLETON, {three});
79    Term one_two = tm.mkTerm(Kind::SET_UNION, {singleton_one, singleton_two});
80    Term two_three =
81        tm.mkTerm(Kind::SET_UNION, {singleton_two, singleton_three});
82    Term intersection = tm.mkTerm(Kind::SET_INTER, {one_two, two_three});
83
84    Term x = tm.mkConst(integer, "x");
85
86    Term e = tm.mkTerm(Kind::SET_MEMBER, {x, intersection});
87
88    Result result = slv.checkSatAssuming(e);
89    cout << "cvc5 reports: " << e << " is " << result << "." << endl;
90
91    if (result.isSat())
92    {
93      cout << "For instance, " << slv.getValue(x) << " is a member." << endl;
94    }
95  }
96}

Finite Relations

cvc5 also supports the theory of finite relations, using the following sorts, constants, functions and predicates. More details can be found in [MRTB17].

SMTLIB language

C++ API

Logic String

(set-logic QF_ALL)

solver.setLogic("QF_ALL");

Tuple Sort

(Tuple <Sort_1>, ..., <Sort_n>)

std::vector<cvc5::Sort> sorts = { ... };

Sort s = solver.mkTupleSort(sorts);

(declare-const t (Tuple Int Int))

Sort s_int = solver.getIntegerSort();

Sort s = solver.mkTupleSort({s_int, s_int});

Term t = solver.mkConst(s, "t");

Tuple Constructor

(tuple <Term_1>, ..., <Term_n>)

Term t = solver.mkTuple({Term_1>, ..., <Term_n>});

Unit Tuple Sort

UnitTuple

Sort s = solver.mkTupleSort({});

Unit Tuple

tuple.unit

Term t = solver.mkTuple({});

Tuple Selector

((_ tuple.select i) t)

Sort s = solver.mkTupleSort(sorts);

Datatype dt = s.getDatatype();

Term c = dt[0].getSelector();

Term t = solver.mkTerm(Kind::APPLY_SELECTOR, {s, t});

Relation Sort

(Relation <Sort_1>, ..., <Sort_n>)

which is a syntax sugar for

(Set (Tuple <Sort_1>, ..., <Sort_n>))

Sort s = solver.mkSetSort(cvc5::Sort tupleSort);

Constants

(declare-const X (Set (Tuple Int Int)

Sort s = solver.mkSetSort(solver.mkTupleSort({s_int, s_int});

Term X = solver.mkConst(s, "X");

Transpose

(rel.transpose X)

Term t = solver.mkTerm(Kind::RELATION_TRANSPOSE, X);

Transitive Closure

(rel.tclosure X)

Term t = solver.mkTerm(Kind::RELATION_TCLOSURE, X);

Join

(rel.join X Y)

Term t = solver.mkTerm(Kind::RELATION_JOIN, X, Y);

Product

(rel.product X Y)

Term t = solver.mkTerm(Kind::RELATION_PRODUCT, X, Y);

Example:

examples/api/cpp/relations.cpp

  1/******************************************************************************
  2 * Top contributors (to current version):
  3 *   Mudathir Mohamed, Aina Niemetz, Mathias Preiner
  4 *
  5 * This file is part of the cvc5 project.
  6 *
  7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
  8 * in the top-level source directory and their institutional affiliations.
  9 * All rights reserved.  See the file COPYING in the top-level source
 10 * directory for licensing information.
 11 * ****************************************************************************
 12 *
 13 * A simple demonstration of reasoning about relations with cvc5 via C++ API.
 14 */
 15
 16#include <cvc5/cvc5.h>
 17
 18#include <iostream>
 19
 20using namespace cvc5;
 21
 22int main()
 23{
 24  TermManager tm;
 25  Solver solver(tm);
 26
 27  // Set the logic
 28  solver.setLogic("ALL");
 29
 30  // options
 31  solver.setOption("produce-models", "true");
 32  // we need finite model finding to answer sat problems with universal
 33  // quantified formulas
 34  solver.setOption("finite-model-find", "true");
 35  // we need sets extension to support set.universe operator
 36  solver.setOption("sets-ext", "true");
 37
 38  // (declare-sort Person 0)
 39  Sort personSort = tm.mkUninterpretedSort("Person");
 40
 41  // (Tuple Person)
 42  Sort tupleArity1 = tm.mkTupleSort({personSort});
 43  // (Relation Person)
 44  Sort relationArity1 = tm.mkSetSort(tupleArity1);
 45
 46  // (Tuple Person Person)
 47  Sort tupleArity2 = tm.mkTupleSort({personSort, personSort});
 48  // (Relation Person Person)
 49  Sort relationArity2 = tm.mkSetSort(tupleArity2);
 50
 51  // empty set
 52  Term emptySetTerm = tm.mkEmptySet(relationArity1);
 53
 54  // empty relation
 55  Term emptyRelationTerm = tm.mkEmptySet(relationArity2);
 56
 57  // universe set
 58  Term universeSet = tm.mkUniverseSet(relationArity1);
 59
 60  // variables
 61  Term people = tm.mkConst(relationArity1, "people");
 62  Term males = tm.mkConst(relationArity1, "males");
 63  Term females = tm.mkConst(relationArity1, "females");
 64  Term father = tm.mkConst(relationArity2, "father");
 65  Term mother = tm.mkConst(relationArity2, "mother");
 66  Term parent = tm.mkConst(relationArity2, "parent");
 67  Term ancestor = tm.mkConst(relationArity2, "ancestor");
 68  Term descendant = tm.mkConst(relationArity2, "descendant");
 69
 70  Term isEmpty1 = tm.mkTerm(Kind::EQUAL, {males, emptySetTerm});
 71  Term isEmpty2 = tm.mkTerm(Kind::EQUAL, {females, emptySetTerm});
 72
 73  // (assert (= people (as set.universe (Relation Person))))
 74  Term peopleAreTheUniverse = tm.mkTerm(Kind::EQUAL, {people, universeSet});
 75  // (assert (not (= males (as set.empty (Relation Person)))))
 76  Term maleSetIsNotEmpty = tm.mkTerm(Kind::NOT, {isEmpty1});
 77  // (assert (not (= females (as set.empty (Relation Person)))))
 78  Term femaleSetIsNotEmpty = tm.mkTerm(Kind::NOT, {isEmpty2});
 79
 80  // (assert (= (set.inter males females)
 81  //            (as set.empty (Relation Person))))
 82  Term malesFemalesIntersection = tm.mkTerm(Kind::SET_INTER, {males, females});
 83  Term malesAndFemalesAreDisjoint =
 84      tm.mkTerm(Kind::EQUAL, {malesFemalesIntersection, emptySetTerm});
 85
 86  // (assert (not (= father (as set.empty (Relation Person Person)))))
 87  // (assert (not (= mother (as set.empty (Relation Person Person)))))
 88  Term isEmpty3 = tm.mkTerm(Kind::EQUAL, {father, emptyRelationTerm});
 89  Term isEmpty4 = tm.mkTerm(Kind::EQUAL, {mother, emptyRelationTerm});
 90  Term fatherIsNotEmpty = tm.mkTerm(Kind::NOT, {isEmpty3});
 91  Term motherIsNotEmpty = tm.mkTerm(Kind::NOT, {isEmpty4});
 92
 93  // fathers are males
 94  // (assert (set.subset (rel.join father people) males))
 95  Term fathers = tm.mkTerm(Kind::RELATION_JOIN, {father, people});
 96  Term fathersAreMales = tm.mkTerm(Kind::SET_SUBSET, {fathers, males});
 97
 98  // mothers are females
 99  // (assert (set.subset (rel.join mother people) females))
100  Term mothers = tm.mkTerm(Kind::RELATION_JOIN, {mother, people});
101  Term mothersAreFemales = tm.mkTerm(Kind::SET_SUBSET, {mothers, females});
102
103  // (assert (= parent (set.union father mother)))
104  Term unionFatherMother = tm.mkTerm(Kind::SET_UNION, {father, mother});
105  Term parentIsFatherOrMother =
106      tm.mkTerm(Kind::EQUAL, {parent, unionFatherMother});
107
108  // (assert (= ancestor (rel.tclosure parent)))
109  Term transitiveClosure = tm.mkTerm(Kind::RELATION_TCLOSURE, {parent});
110  Term ancestorFormula = tm.mkTerm(Kind::EQUAL, {ancestor, transitiveClosure});
111
112  // (assert (= descendant (rel.transpose descendant)))
113  Term transpose = tm.mkTerm(Kind::RELATION_TRANSPOSE, {ancestor});
114  Term descendantFormula = tm.mkTerm(Kind::EQUAL, {descendant, transpose});
115
116  // (assert (forall ((x Person)) (not (set.member (tuple x x) ancestor))))
117  Term x = tm.mkVar(personSort, "x");
118  Term xxTuple = tm.mkTuple({x, x});
119  Term member = tm.mkTerm(Kind::SET_MEMBER, {xxTuple, ancestor});
120  Term notMember = tm.mkTerm(Kind::NOT, {member});
121
122  Term quantifiedVariables = tm.mkTerm(Kind::VARIABLE_LIST, {x});
123  Term noSelfAncestor =
124      tm.mkTerm(Kind::FORALL, {quantifiedVariables, notMember});
125
126  // formulas
127  solver.assertFormula(peopleAreTheUniverse);
128  solver.assertFormula(maleSetIsNotEmpty);
129  solver.assertFormula(femaleSetIsNotEmpty);
130  solver.assertFormula(malesAndFemalesAreDisjoint);
131  solver.assertFormula(fatherIsNotEmpty);
132  solver.assertFormula(motherIsNotEmpty);
133  solver.assertFormula(fathersAreMales);
134  solver.assertFormula(mothersAreFemales);
135  solver.assertFormula(parentIsFatherOrMother);
136  solver.assertFormula(descendantFormula);
137  solver.assertFormula(ancestorFormula);
138  solver.assertFormula(noSelfAncestor);
139
140  // check sat
141  Result result = solver.checkSat();
142
143  // output
144  std::cout << "Result     = " << result << std::endl;
145  std::cout << "people     = " << solver.getValue(people) << std::endl;
146  std::cout << "males      = " << solver.getValue(males) << std::endl;
147  std::cout << "females    = " << solver.getValue(females) << std::endl;
148  std::cout << "father     = " << solver.getValue(father) << std::endl;
149  std::cout << "mother     = " << solver.getValue(mother) << std::endl;
150  std::cout << "parent     = " << solver.getValue(parent) << std::endl;
151  std::cout << "descendant = " << solver.getValue(descendant) << std::endl;
152  std::cout << "ancestor   = " << solver.getValue(ancestor) << std::endl;
153}