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

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