Theory of Sequences

examples/api/cpp/sequences.cpp

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Andres Noetzli, Mathias Preiner, Aina Niemetz
 4 *
 5 * This file is part of the cvc5 project.
 6 *
 7 * Copyright (c) 2009-2022 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 sequences 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  Solver slv;
25
26  // Set the logic
27  slv.setLogic("QF_SLIA");
28  // Produce models
29  slv.setOption("produce-models", "true");
30  // The option strings-exp is needed
31  slv.setOption("strings-exp", "true");
32  // Set output language to SMTLIB2
33  slv.setOption("output-language", "smt2");
34
35  // Sequence sort
36  Sort intSeq = slv.mkSequenceSort(slv.getIntegerSort());
37
38  // Sequence variables
39  Term x = slv.mkConst(intSeq, "x");
40  Term y = slv.mkConst(intSeq, "y");
41
42  // Empty sequence
43  Term empty = slv.mkEmptySequence(slv.getIntegerSort());
44  // Sequence concatenation: x.y.empty
45  Term concat = slv.mkTerm(Kind::SEQ_CONCAT, {x, y, empty});
46  // Sequence length: |x.y.empty|
47  Term concat_len = slv.mkTerm(Kind::SEQ_LENGTH, {concat});
48  // |x.y.empty| > 1
49  Term formula1 = slv.mkTerm(Kind::GT, {concat_len, slv.mkInteger(1)});
50  // Sequence unit: seq(1)
51  Term unit = slv.mkTerm(Kind::SEQ_UNIT, {slv.mkInteger(1)});
52  // x = seq(1)
53  Term formula2 = slv.mkTerm(Kind::EQUAL, {x, unit});
54
55  // Make a query
56  Term q = slv.mkTerm(Kind::AND, {formula1, formula2});
57
58  // check sat
59  Result result = slv.checkSatAssuming(q);
60  std::cout << "cvc5 reports: " << q << " is " << result << "." << std::endl;
61
62  if (result.isSat())
63  {
64    std::cout << "  x = " << slv.getValue(x) << std::endl;
65    std::cout << "  y = " << slv.getValue(y) << std::endl;
66  }
67}