SyGuS: Invariants
examples/api/cpp/sygus-inv.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 the Sygus API.
11 *
12 * A simple demonstration of how to use the Sygus API to synthesize a simple
13 * invariant.
14 */
15
16#include <cvc5/cvc5.h>
17
18#include <iostream>
19
20#include "utils.h"
21
22using namespace cvc5;
23
24int main()
25{
26 TermManager tm;
27 Solver slv(tm);
28
29 // required options
30 slv.setOption("sygus", "true");
31 slv.setOption("incremental", "false");
32
33 // set the logic
34 slv.setLogic("LIA");
35
36 Sort integer = tm.getIntegerSort();
37 Sort boolean = tm.getBooleanSort();
38
39 Term zero = tm.mkInteger(0);
40 Term one = tm.mkInteger(1);
41 Term ten = tm.mkInteger(10);
42
43 // declare input variables for functions
44 Term x = tm.mkVar(integer, "x");
45 Term xp = tm.mkVar(integer, "xp");
46
47 // (ite (< x 10) (= xp (+ x 1)) (= xp x))
48 Term ite =
49 tm.mkTerm(Kind::ITE,
50 {tm.mkTerm(Kind::LT, {x, ten}),
51 tm.mkTerm(Kind::EQUAL, {xp, tm.mkTerm(Kind::ADD, {x, one})}),
52 tm.mkTerm(Kind::EQUAL, {xp, x})});
53
54 // define the pre-conditions, transition relations, and post-conditions
55 Term pre_f =
56 slv.defineFun("pre-f", {x}, boolean, tm.mkTerm(Kind::EQUAL, {x, zero}));
57 Term trans_f = slv.defineFun("trans-f", {x, xp}, boolean, ite);
58 Term post_f =
59 slv.defineFun("post-f", {x}, boolean, tm.mkTerm(Kind::LEQ, {x, ten}));
60
61 // declare the invariant-to-synthesize
62 Term inv_f = slv.synthFun("inv-f", {x}, boolean);
63
64 slv.addSygusInvConstraint(inv_f, pre_f, trans_f, post_f);
65
66 // print solutions if available
67 if (slv.checkSynth().hasSolution())
68 {
69 // Output should be equivalent to:
70 // (
71 // (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
72 // )
73 std::vector<Term> terms = {inv_f};
74 utils::printSynthSolutions(terms, slv.getSynthSolutions(terms));
75 }
76
77 return 0;
78}
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 the Sygus API.
11 *
12 * A simple demonstration of how to use the Sygus API to synthesize a simple
13 * invariant.
14 */
15
16#include <cvc5/c/cvc5.h>
17#include <stdio.h>
18
19#include "utils.h"
20
21int main()
22{
23 Cvc5TermManager* tm = cvc5_term_manager_new();
24 Cvc5* slv = cvc5_new(tm);
25
26 // required options
27 cvc5_set_option(slv, "sygus", "true");
28 cvc5_set_option(slv, "incremental", "false");
29
30 // set the logic
31 cvc5_set_logic(slv, "LIA");
32
33 Cvc5Sort int_sort = cvc5_get_integer_sort(tm);
34 Cvc5Sort bool_sort = cvc5_get_boolean_sort(tm);
35
36 Cvc5Term zero = cvc5_mk_integer_int64(tm, 0);
37 Cvc5Term one = cvc5_mk_integer_int64(tm, 1);
38 Cvc5Term ten = cvc5_mk_integer_int64(tm, 10);
39
40 // declare input variables for functions
41 Cvc5Term x = cvc5_mk_var(tm, int_sort, "x");
42 Cvc5Term xp = cvc5_mk_var(tm, int_sort, "xp");
43
44 // (ite (< x 10) (= xp (+ x 1)) (= xp x))
45 Cvc5Term args2[2] = {x, ten};
46 Cvc5Term cond = cvc5_mk_term(tm, CVC5_KIND_LT, 2, args2);
47 args2[0] = x;
48 args2[1] = one;
49 Cvc5Term add = cvc5_mk_term(tm, CVC5_KIND_ADD, 2, args2);
50 args2[0] = xp;
51 args2[1] = add;
52 Cvc5Term els = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
53 args2[0] = xp;
54 args2[1] = x;
55 Cvc5Term the = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
56 Cvc5Term args3[3] = {cond, els, the};
57 Cvc5Term ite = cvc5_mk_term(tm, CVC5_KIND_ITE, 3, args3);
58
59 // define the pre-conditions, transition relations, and post-conditions
60 Cvc5Term vars1[1] = {x};
61 args2[0] = x;
62 args2[1] = zero;
63 Cvc5Term pre_f = cvc5_define_fun(slv,
64 "pre-f",
65 1,
66 vars1,
67 bool_sort,
68 cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2),
69 false);
70 Cvc5Term vars2[2] = {x, xp};
71 Cvc5Term trans_f =
72 cvc5_define_fun(slv, "trans-f", 2, vars2, bool_sort, ite, false);
73 args2[0] = x;
74 args2[1] = ten;
75 Cvc5Term post_f = cvc5_define_fun(slv,
76 "post-f",
77 1,
78 vars1,
79 bool_sort,
80 cvc5_mk_term(tm, CVC5_KIND_LEQ, 2, args2),
81 false);
82
83 // declare the invariant-to-synthesize
84 Cvc5Term inv_f = cvc5_synth_fun(slv, "inv-f", 1, vars1, bool_sort);
85
86 cvc5_add_sygus_inv_constraint(slv, inv_f, pre_f, trans_f, post_f);
87
88 // print solutions if available
89 if (cvc5_synth_result_has_solution(cvc5_check_synth(slv)))
90 {
91 // Output should be equivalent to:
92 // (
93 // (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
94 // )
95 Cvc5Term args1[1] = {inv_f};
96 const Cvc5Term* sols = cvc5_get_synth_solutions(slv, 1, args1);
97 print_synth_solutions(1, args1, sols);
98 }
99
100 cvc5_delete(slv);
101 cvc5_term_manager_delete(tm);
102 return 0;
103}
examples/api/java/SygusInv.java
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 the Sygus API.
11 *
12 * A simple demonstration of how to use the Sygus API to synthesize a simple
13 * invariant. This is a direct translation of sygus-inv.cpp.
14 */
15
16import static io.github.cvc5.Kind.*;
17
18import io.github.cvc5.*;
19
20public class SygusInv
21{
22 public static void main(String args[]) throws CVC5ApiException
23 {
24 TermManager tm = new TermManager();
25 Solver slv = new Solver(tm);
26 {
27 // required options
28 slv.setOption("sygus", "true");
29 slv.setOption("incremental", "false");
30
31 // set the logic
32 slv.setLogic("LIA");
33
34 Sort integer = tm.getIntegerSort();
35 Sort bool = tm.getBooleanSort();
36
37 Term zero = tm.mkInteger(0);
38 Term one = tm.mkInteger(1);
39 Term ten = tm.mkInteger(10);
40
41 // declare input variables for functions
42 Term x = tm.mkVar(integer, "x");
43 Term xp = tm.mkVar(integer, "xp");
44
45 // (ite (< x 10) (= xp (+ x 1)) (= xp x))
46 Term ite = tm.mkTerm(ITE,
47 tm.mkTerm(LT, x, ten),
48 tm.mkTerm(EQUAL, xp, tm.mkTerm(ADD, x, one)),
49 tm.mkTerm(EQUAL, xp, x));
50
51 // define the pre-conditions, transition relations, and post-conditions
52 Term pre_f = slv.defineFun("pre-f", new Term[] {x}, bool, tm.mkTerm(EQUAL, x, zero));
53 Term trans_f = slv.defineFun("trans-f", new Term[] {x, xp}, bool, ite);
54 Term post_f = slv.defineFun("post-f", new Term[] {x}, bool, tm.mkTerm(LEQ, x, ten));
55
56 // declare the invariant-to-synthesize
57 Term inv_f = slv.synthFun("inv-f", new Term[] {x}, bool);
58
59 slv.addSygusInvConstraint(inv_f, pre_f, trans_f, post_f);
60
61 // print solutions if available
62 if (slv.checkSynth().hasSolution())
63 {
64 // Output should be equivalent to:
65 // (
66 // (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
67 // )
68 Term[] terms = new Term[] {inv_f};
69 Utils.printSynthSolutions(terms, slv.getSynthSolutions(terms));
70 }
71 }
72 Context.deletePointers();
73 }
74}
examples/api/python/sygus-inv.py
1#!/usr/bin/env python
2###############################################################################
3# This file is part of the cvc5 project.
4#
5# Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
6# in the top-level source directory and their institutional affiliations.
7# All rights reserved. See the file COPYING in the top-level source
8# directory for licensing information.
9# #############################################################################
10#
11# A simple demonstration of the solving capabilities of the cvc5
12# sygus solver through the Python API. This is a direct
13# translation of sygus-inv.cpp .
14##
15
16import utils
17import cvc5
18from cvc5 import Kind
19
20if __name__ == "__main__":
21 tm = cvc5.TermManager()
22 slv = cvc5.Solver(tm)
23
24 # required options
25 slv.setOption("sygus", "true")
26 slv.setOption("incremental", "false")
27
28 # set the logic
29 slv.setLogic("LIA")
30
31 integer = tm.getIntegerSort()
32 boolean = tm.getBooleanSort()
33
34 zero = tm.mkInteger(0)
35 one = tm.mkInteger(1)
36 ten = tm.mkInteger(10)
37
38 # declare input variables for functions
39 x = tm.mkVar(integer, "x")
40 xp = tm.mkVar(integer, "xp")
41
42 # (ite (< x 10) (= xp (+ x 1)) (= xp x))
43 ite = tm.mkTerm(Kind.ITE,
44 tm.mkTerm(Kind.LT, x, ten),
45 tm.mkTerm(Kind.EQUAL, xp, tm.mkTerm(Kind.ADD, x, one)),
46 tm.mkTerm(Kind.EQUAL, xp, x))
47
48 # define the pre-conditions, transition relations, and post-conditions
49 pre_f = slv.defineFun("pre-f", [x], boolean, tm.mkTerm(Kind.EQUAL, x, zero))
50 trans_f = slv.defineFun("trans-f", [x, xp], boolean, ite)
51 post_f = slv.defineFun("post-f", [x], boolean, tm.mkTerm(Kind.LEQ, x, ten))
52
53 # declare the invariant-to-synthesize
54 inv_f = slv.synthFun("inv-f", [x], boolean)
55
56 slv.addSygusInvConstraint(inv_f, pre_f, trans_f, post_f)
57
58 # print solutions if available
59 if slv.checkSynth().hasSolution():
60 # Output should be equivalent to:
61 # (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
62 terms = [inv_f]
63 utils.print_synth_solutions(terms, slv.getSynthSolutions(terms))
64
65
examples/api/smtlib/sygus-inv.sy
1; The printed output for this example should be equivalent to:
2; (
3; (define-fun inv-f ((x Int)) Bool (not (>= x 11)))
4; )
5
6(set-logic LIA)
7(synth-fun inv-f ((x Int)) Bool)
8(define-fun pre-f ((x Int)) Bool (= x 0))
9(define-fun trans-f ((x Int) (xp Int)) Bool (ite (< x 10) (= xp (+ x 1)) (= xp x)))
10(define-fun post-f ((x Int)) Bool (<= x 10))
11(inv-constraint inv-f pre-f trans-f post-f)
12(check-synth)
The utility method used for printing the synthesis solutions in the examples
above is defined separately in the utils module:
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 * Utility methods.
11 */
12
13#ifndef CVC5__UTILS_H
14#define CVC5__UTILS_H
15
16#include <cvc5/cvc5.h>
17
18namespace utils {
19
20/**
21 * Print solutions for synthesis conjecture to the standard output stream.
22 * @param terms the terms for which the synthesis solutions were retrieved
23 * @param sols the synthesis solutions of the given terms
24 */
25void printSynthSolutions(const std::vector<cvc5::Term>& terms,
26 const std::vector<cvc5::Term>& sols);
27
28} // namespace utils
29
30#endif // CVC5__UTILS_H
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 * Implementations of utility methods.
11 */
12
13#include "utils.h"
14
15#include <iostream>
16
17namespace utils {
18
19using namespace cvc5;
20
21/**
22 * Get the string version of define-fun command.
23 * @param f the function to print
24 * @param params the function parameters
25 * @param body the function body
26 * @return a string version of define-fun
27 */
28std::string defineFunToString(const cvc5::Term& f,
29 const std::vector<cvc5::Term>& params,
30 const cvc5::Term& body)
31{
32 cvc5::Sort sort = f.getSort();
33 if (sort.isFunction())
34 {
35 sort = sort.getFunctionCodomainSort();
36 }
37 std::stringstream ss;
38 ss << "(define-fun " << f << " (";
39 for (size_t i = 0, n = params.size(); i < n; ++i)
40 {
41 if (i > 0)
42 {
43 ss << ' ';
44 }
45 ss << '(' << params[i] << ' ' << params[i].getSort() << ')';
46 }
47 ss << ") " << sort << ' ' << body << ')';
48 return ss.str();
49}
50
51void printSynthSolutions(const std::vector<cvc5::Term>& terms,
52 const std::vector<cvc5::Term>& sols)
53{
54 std::cout << '(' << std::endl;
55 for (size_t i = 0, n = terms.size(); i < n; ++i)
56 {
57 std::vector<cvc5::Term> params;
58 cvc5::Term body = sols[i];
59 if (sols[i].getKind() == cvc5::Kind::LAMBDA)
60 {
61 params.insert(params.end(), sols[i][0].begin(), sols[i][0].end());
62 body = sols[i][1];
63 }
64 std::cout << " " << defineFunToString(terms[i], params, body) << std::endl;
65 }
66 std::cout << ')' << std::endl;
67}
68
69} // namespace utils
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 * Utility methods.
11 */
12
13#ifndef CVC5__C_UTILS_H
14#define CVC5__C_UTILS_H
15
16#include <cvc5/c/cvc5.h>
17
18/**
19 * Print solutions for synthesis conjecture to the stdout.
20 * @param nterms The number of terms.
21 * @param terms The terms for which the synthesis solutions were retrieved.
22 * @param sols The synthesis solutions of the given terms.
23 */
24void print_synth_solutions(size_t nterms,
25 const Cvc5Term terms[],
26 const Cvc5Term sols[]);
27
28#endif
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 * Implementations of utility methods.
11 */
12
13#include "utils.h"
14
15#include <stdio.h>
16#include <stdlib.h>
17
18/**
19 * Get the string version of define-fun command.
20 * @param f The function to print.
21 * @param nparams The number of function parameters.
22 * @param params The function parameters.
23 * @param body The function body.
24 * @return A string version of define-fun.
25 */
26void print_define_fun(const Cvc5Term f,
27 size_t nparams,
28 const Cvc5Term params[],
29 const Cvc5Term body)
30{
31 Cvc5Sort sort = cvc5_term_get_sort(f);
32 if (cvc5_sort_is_fun(sort))
33 {
34 sort = cvc5_sort_fun_get_codomain(sort);
35 }
36 printf("(define-fun %s (", cvc5_term_to_string(f));
37 for (size_t i = 0; i < nparams; i++)
38 {
39 printf("%s", i > 0 ? " " : "");
40 printf("(%s %s)",
41 cvc5_term_to_string(params[i]),
42 cvc5_sort_to_string(cvc5_term_get_sort(params[i])));
43 }
44 printf(") %s %s)", cvc5_sort_to_string(sort), cvc5_term_to_string(body));
45}
46
47void print_synth_solutions(size_t nterms,
48 const Cvc5Term terms[],
49 const Cvc5Term sols[])
50{
51 printf("(\n");
52 for (size_t i = 0; i < nterms; i++)
53 {
54 size_t nparams = 0;
55 Cvc5Term* params = NULL;
56 Cvc5Term body = sols[i];
57 if (cvc5_term_get_kind(sols[i]) == CVC5_KIND_LAMBDA)
58 {
59 Cvc5Term psols = cvc5_term_get_child(sols[i], 0);
60 nparams = cvc5_term_get_num_children(psols);
61 params = (Cvc5Term*)malloc(nparams * sizeof(Cvc5Term));
62 for (size_t k = 0; k < nparams; k++)
63 {
64 params[k] = cvc5_term_get_child(psols, k);
65 }
66 body = cvc5_term_get_child(sols[i], 1);
67 }
68 printf(" ");
69 print_define_fun(terms[i], nterms, params, body);
70 if (params)
71 {
72 free(params);
73 }
74 printf("\n");
75 }
76 printf(")\n");
77}
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 * Utility methods.
11 */
12
13import io.github.cvc5.CVC5ApiException;
14import io.github.cvc5.Kind;
15import io.github.cvc5.Sort;
16import io.github.cvc5.Term;
17import java.util.ArrayList;
18import java.util.List;
19
20public class Utils
21{
22 /**
23 * Get the string version of define-fun command.
24 *
25 * @param f the function to print
26 * @param params the function parameters
27 * @param body the function body
28 * @return a string version of define-fun
29 */
30 private static String defineFunToString(Term f, Term[] params, Term body)
31 {
32 Sort sort = f.getSort();
33 if (sort.isFunction())
34 {
35 sort = sort.getFunctionCodomainSort();
36 }
37 StringBuilder ss = new StringBuilder();
38 ss.append("(define-fun ").append(f).append(" (");
39 for (int i = 0; i < params.length; ++i)
40 {
41 if (i > 0)
42 {
43 ss.append(' ');
44 }
45 ss.append('(').append(params[i]).append(' ').append(params[i].getSort()).append(')');
46 }
47 ss.append(") ").append(sort).append(' ').append(body).append(')');
48 return ss.toString();
49 }
50
51 /**
52 * Print solutions for synthesis conjecture to the standard output stream.
53 *
54 * @param terms the terms for which the synthesis solutions were retrieved
55 * @param sols the synthesis solutions of the given terms
56 */
57 public static void printSynthSolutions(Term[] terms, Term[] sols) throws CVC5ApiException
58 {
59 System.out.println('(');
60 for (int i = 0; i < terms.length; ++i)
61 {
62 List<Term> params = new ArrayList<>();
63 Term body = sols[i];
64 if (sols[i].getKind() == Kind.LAMBDA)
65 {
66 for (Term t : sols[i].getChild(0))
67 {
68 params.add(t);
69 }
70 body = sols[i].getChild(1);
71 }
72 System.out.println(" " + defineFunToString(terms[i], params.toArray(new Term[0]), body));
73 }
74 System.out.println(')');
75 }
76}
1#!/usr/bin/env python
2###############################################################################
3# This file is part of the cvc5 project.
4#
5# Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
6# in the top-level source directory and their institutional affiliations.
7# All rights reserved. See the file COPYING in the top-level source
8# directory for licensing information.
9# #############################################################################
10#
11# Utility Methods, translated from examples/api/utils.h
12##
13
14import cvc5
15from cvc5 import Kind
16
17# Get the string version of define-fun command.
18# @param f the function to print
19# @param params the function parameters
20# @param body the function body
21# @return a string version of define-fun
22
23
24def define_fun_to_string(f, params, body):
25 sort = f.getSort()
26 if sort.isFunction():
27 sort = f.getSort().getFunctionCodomainSort()
28 result = "(define-fun " + str(f) + " ("
29 for i in range(0, len(params)):
30 if i > 0:
31 result += " "
32 result += "(" + str(params[i]) + " " + str(params[i].getSort()) + ")"
33 result += ") " + str(sort) + " " + str(body) + ")"
34 return result
35
36
37# Print solutions for synthesis conjecture to the standard output stream.
38# @param terms the terms for which the synthesis solutions were retrieved
39# @param sols the synthesis solutions of the given terms
40
41
42def print_synth_solutions(terms, sols):
43 result = "(\n"
44 for i in range(0, len(terms)):
45 params = []
46 body = sols[i]
47 if sols[i].getKind() == Kind.LAMBDA:
48 params += sols[i][0]
49 body = sols[i][1]
50 result += " " + define_fun_to_string(terms[i], params, body) + "\n"
51 result += ")"
52 print(result)