summaryrefslogtreecommitdiff
path: root/examples/api/sets.cpp
blob: cac7054154378de5fcc242d44427a3da50dd1b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*********************                                                        */
/*! \file sets.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Aina Niemetz, Kshitij Bansal, Mudathir Mohamed
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 ** in the top-level source directory and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Reasoning about sets with CVC4.
 **
 ** A simple demonstration of reasoning about sets with CVC4.
 **/

#include <iostream>

#include <cvc4/api/cvc4cpp.h>

using namespace std;
using namespace CVC5::api;

int main()
{
  Solver slv;

  // Optionally, set the logic. We need at least UF for equality predicate,
  // integers (LIA) and sets (FS).
  slv.setLogic("QF_UFLIAFS");

  // Produce models
  slv.setOption("produce-models", "true");
  slv.setOption("output-language", "smt2");

  Sort integer = slv.getIntegerSort();
  Sort set = slv.mkSetSort(integer);

  // Verify union distributions over intersection
  // (A union B) intersection C = (A intersection C) union (B intersection C)
  {
    Term A = slv.mkConst(set, "A");
    Term B = slv.mkConst(set, "B");
    Term C = slv.mkConst(set, "C");

    Term unionAB = slv.mkTerm(UNION, A, B);
    Term lhs = slv.mkTerm(INTERSECTION, unionAB, C);

    Term intersectionAC = slv.mkTerm(INTERSECTION, A, C);
    Term intersectionBC = slv.mkTerm(INTERSECTION, B, C);
    Term rhs = slv.mkTerm(UNION, intersectionAC, intersectionBC);

    Term theorem = slv.mkTerm(EQUAL, lhs, rhs);

    cout << "CVC4 reports: " << theorem << " is " << slv.checkEntailed(theorem)
         << "." << endl;
  }

  // Verify emptset is a subset of any set
  {
    Term A = slv.mkConst(set, "A");
    Term emptyset = slv.mkEmptySet(set);

    Term theorem = slv.mkTerm(SUBSET, emptyset, A);

    cout << "CVC4 reports: " << theorem << " is " << slv.checkEntailed(theorem)
         << "." << endl;
  }

  // Find me an element in {1, 2} intersection {2, 3}, if there is one.
  {
    Term one = slv.mkInteger(1);
    Term two = slv.mkInteger(2);
    Term three = slv.mkInteger(3);

    Term singleton_one = slv.mkTerm(SINGLETON, one);
    Term singleton_two = slv.mkTerm(SINGLETON, two);
    Term singleton_three = slv.mkTerm(SINGLETON, three);
    Term one_two = slv.mkTerm(UNION, singleton_one, singleton_two);
    Term two_three = slv.mkTerm(UNION, singleton_two, singleton_three);
    Term intersection = slv.mkTerm(INTERSECTION, one_two, two_three);

    Term x = slv.mkConst(integer, "x");

    Term e = slv.mkTerm(MEMBER, x, intersection);

    Result result = slv.checkSatAssuming(e);
    cout << "CVC4 reports: " << e << " is " << result << "." << endl;

    if (result.isSat())
    {
      cout << "For instance, " << slv.getValue(x) << " is a member." << endl;
    }
  }
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback