SyGuS: Invariants

examples/api/cpp/sygus-inv.cpp

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Abdalrhman Mohamed, Mathias Preiner, Mudathir Mohamed
 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 the Sygus API.
14 *
15 * A simple demonstration of how to use the Sygus API to synthesize a simple
16 * invariant.
17 */
18
19#include <cvc5/cvc5.h>
20
21#include <iostream>
22
23#include "utils.h"
24
25using namespace cvc5;
26
27int main()
28{
29  Solver slv;
30
31  // required options
32  slv.setOption("sygus", "true");
33  slv.setOption("incremental", "false");
34
35  // set the logic
36  slv.setLogic("LIA");
37
38  Sort integer = slv.getIntegerSort();
39  Sort boolean = slv.getBooleanSort();
40
41  Term zero = slv.mkInteger(0);
42  Term one = slv.mkInteger(1);
43  Term ten = slv.mkInteger(10);
44
45  // declare input variables for functions
46  Term x = slv.mkVar(integer, "x");
47  Term xp = slv.mkVar(integer, "xp");
48
49  // (ite (< x 10) (= xp (+ x 1)) (= xp x))
50  Term ite = slv.mkTerm(
51      Kind::ITE,
52      {slv.mkTerm(Kind::LT, {x, ten}),
53       slv.mkTerm(Kind::EQUAL, {xp, slv.mkTerm(Kind::ADD, {x, one})}),
54       slv.mkTerm(Kind::EQUAL, {xp, x})});
55
56  // define the pre-conditions, transition relations, and post-conditions
57  Term pre_f =
58      slv.defineFun("pre-f", {x}, boolean, slv.mkTerm(Kind::EQUAL, {x, zero}));
59  Term trans_f = slv.defineFun("trans-f", {x, xp}, boolean, ite);
60  Term post_f =
61      slv.defineFun("post-f", {x}, boolean, slv.mkTerm(Kind::LEQ, {x, ten}));
62
63  // declare the invariant-to-synthesize
64  Term inv_f = slv.synthFun("inv-f", {x}, boolean);
65
66  slv.addSygusInvConstraint(inv_f, pre_f, trans_f, post_f);
67
68  // print solutions if available
69  if (slv.checkSynth().hasSolution())
70  {
71    // Output should be equivalent to:
72    // (
73    //   (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
74    // )
75    std::vector<Term> terms = {inv_f};
76    utils::printSynthSolutions(terms, slv.getSynthSolutions(terms));
77  }
78
79  return 0;
80}

The utility method used for printing the synthesis solutions in the examples above is defined separately in the utils module:

examples/api/cpp/utils.h

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Abdalrhman Mohamed, Mathias Preiner
 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 * Utility methods.
14 */
15
16#ifndef CVC5__UTILS_H
17#define CVC5__UTILS_H
18
19#include <cvc5/cvc5.h>
20
21namespace utils {
22
23/**
24 * Print solutions for synthesis conjecture to the standard output stream.
25 * @param terms the terms for which the synthesis solutions were retrieved
26 * @param sols the synthesis solutions of the given terms
27 */
28void printSynthSolutions(const std::vector<cvc5::Term>& terms,
29                         const std::vector<cvc5::Term>& sols);
30
31}  // namespace utils
32
33#endif  // CVC5__UTILS_H