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(ITE,
51                        {slv.mkTerm(LT, {x, ten}),
52                         slv.mkTerm(EQUAL, {xp, slv.mkTerm(ADD, {x, one})}),
53                         slv.mkTerm(EQUAL, {xp, x})});
54
55  // define the pre-conditions, transition relations, and post-conditions
56  Term pre_f =
57      slv.defineFun("pre-f", {x}, boolean, slv.mkTerm(EQUAL, {x, zero}));
58  Term trans_f = slv.defineFun("trans-f", {x, xp}, boolean, ite);
59  Term post_f =
60      slv.defineFun("post-f", {x}, boolean, slv.mkTerm(LEQ, {x, ten}));
61
62  // declare the invariant-to-synthesize
63  Term inv_f = slv.synthFun("inv-f", {x}, boolean);
64
65  slv.addSygusInvConstraint(inv_f, pre_f, trans_f, post_f);
66
67  // print solutions if available
68  if (slv.checkSynth().hasSolution())
69  {
70    // Output should be equivalent to:
71    // (
72    //   (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
73    // )
74    std::vector<Term> terms = {inv_f};
75    utils::printSynthSolutions(terms, slv.getSynthSolutions(terms));
76  }
77
78  return 0;
79}

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