Theory of Sequences

examples/api/cpp/sequences.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 sequences 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 slv(tm);
23
24  // Set the logic
25  slv.setLogic("QF_SLIA");
26  // Produce models
27  slv.setOption("produce-models", "true");
28  // The option strings-exp is needed
29  slv.setOption("strings-exp", "true");
30
31  // Sequence sort
32  Sort intSeq = tm.mkSequenceSort(tm.getIntegerSort());
33
34  // Sequence variables
35  Term x = tm.mkConst(intSeq, "x");
36  Term y = tm.mkConst(intSeq, "y");
37
38  // Empty sequence
39  Term empty = tm.mkEmptySequence(tm.getIntegerSort());
40  // Sequence concatenation: x.y.empty
41  Term concat = tm.mkTerm(Kind::SEQ_CONCAT, {x, y, empty});
42  // Sequence length: |x.y.empty|
43  Term concat_len = tm.mkTerm(Kind::SEQ_LENGTH, {concat});
44  // |x.y.empty| > 1
45  Term formula1 = tm.mkTerm(Kind::GT, {concat_len, tm.mkInteger(1)});
46  // Sequence unit: seq(1)
47  Term unit = tm.mkTerm(Kind::SEQ_UNIT, {tm.mkInteger(1)});
48  // x = seq(1)
49  Term formula2 = tm.mkTerm(Kind::EQUAL, {x, unit});
50
51  // Make a query
52  Term q = tm.mkTerm(Kind::AND, {formula1, formula2});
53
54  // check sat
55  Result result = slv.checkSatAssuming(q);
56  std::cout << "cvc5 reports: " << q << " is " << result << "." << std::endl;
57
58  if (result.isSat())
59  {
60    std::cout << "  x = " << slv.getValue(x) << std::endl;
61    std::cout << "  y = " << slv.getValue(y) << std::endl;
62  }
63}