Python API: Add support for sequences (#4757)
[cvc5.git] / examples / api / sequences.cpp
1 /********************* */
2 /*! \file sequences.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Andres Noetzli
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 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.\endverbatim
11 **
12 ** \brief Reasoning about sequences with CVC4 via C++ API.
13 **
14 ** A simple demonstration of reasoning about sequences with CVC4 via C++ API.
15 **/
16
17 #include <cvc4/api/cvc4cpp.h>
18
19 #include <iostream>
20
21 using namespace CVC4::api;
22
23 int main()
24 {
25 Solver slv;
26
27 // Set the logic
28 slv.setLogic("QF_SLIA");
29 // Produce models
30 slv.setOption("produce-models", "true");
31 // The option strings-exp is needed
32 slv.setOption("strings-exp", "true");
33 // Set output language to SMTLIB2
34 slv.setOption("output-language", "smt2");
35
36 // Sequence sort
37 Sort intSeq = slv.mkSequenceSort(slv.getIntegerSort());
38
39 // Sequence variables
40 Term x = slv.mkConst(intSeq, "x");
41 Term y = slv.mkConst(intSeq, "y");
42
43 // Empty sequence
44 Term empty = slv.mkEmptySequence(slv.getIntegerSort());
45 // Sequence concatenation: x.y.empty
46 Term concat = slv.mkTerm(SEQ_CONCAT, x, y, empty);
47 // Sequence length: |x.y.empty|
48 Term concat_len = slv.mkTerm(SEQ_LENGTH, concat);
49 // |x.y.empty| > 1
50 Term formula1 = slv.mkTerm(GT, concat_len, slv.mkReal(1));
51 // Sequence unit: seq(1)
52 Term unit = slv.mkTerm(SEQ_UNIT, slv.mkReal(1));
53 // x = seq(1)
54 Term formula2 = slv.mkTerm(EQUAL, x, unit);
55
56 // Make a query
57 Term q = slv.mkTerm(AND, formula1, formula2);
58
59 // check sat
60 Result result = slv.checkSatAssuming(q);
61 std::cout << "CVC4 reports: " << q << " is " << result << "." << std::endl;
62
63 if (result.isSat())
64 {
65 std::cout << " x = " << slv.getValue(x) << std::endl;
66 std::cout << " y = " << slv.getValue(y) << std::endl;
67 }
68 }