SyGuS: Invariants

examples/api/cpp/sygus-inv.cpp

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Abdalrhman Mohamed, Aina Niemetz, Mathias Preiner
 4 *
 5 * This file is part of the cvc5 project.
 6 *
 7 * Copyright (c) 2009-2024 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  TermManager tm;
30  Solver slv(tm);
31
32  // required options
33  slv.setOption("sygus", "true");
34  slv.setOption("incremental", "false");
35
36  // set the logic
37  slv.setLogic("LIA");
38
39  Sort integer = tm.getIntegerSort();
40  Sort boolean = tm.getBooleanSort();
41
42  Term zero = tm.mkInteger(0);
43  Term one = tm.mkInteger(1);
44  Term ten = tm.mkInteger(10);
45
46  // declare input variables for functions
47  Term x = tm.mkVar(integer, "x");
48  Term xp = tm.mkVar(integer, "xp");
49
50  // (ite (< x 10) (= xp (+ x 1)) (= xp x))
51  Term ite =
52      tm.mkTerm(Kind::ITE,
53                {tm.mkTerm(Kind::LT, {x, ten}),
54                 tm.mkTerm(Kind::EQUAL, {xp, tm.mkTerm(Kind::ADD, {x, one})}),
55                 tm.mkTerm(Kind::EQUAL, {xp, x})});
56
57  // define the pre-conditions, transition relations, and post-conditions
58  Term pre_f =
59      slv.defineFun("pre-f", {x}, boolean, tm.mkTerm(Kind::EQUAL, {x, zero}));
60  Term trans_f = slv.defineFun("trans-f", {x, xp}, boolean, ite);
61  Term post_f =
62      slv.defineFun("post-f", {x}, boolean, tm.mkTerm(Kind::LEQ, {x, ten}));
63
64  // declare the invariant-to-synthesize
65  Term inv_f = slv.synthFun("inv-f", {x}, boolean);
66
67  slv.addSygusInvConstraint(inv_f, pre_f, trans_f, post_f);
68
69  // print solutions if available
70  if (slv.checkSynth().hasSolution())
71  {
72    // Output should be equivalent to:
73    // (
74    //   (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
75    // )
76    std::vector<Term> terms = {inv_f};
77    utils::printSynthSolutions(terms, slv.getSynthSolutions(terms));
78  }
79
80  return 0;
81}

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-2024 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