SyGuS: Functions

examples/api/cpp/sygus-fun.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 max and min
 16 * functions.
 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  // declare input variables for the functions-to-synthesize
 43  Term x = tm.mkVar(integer, "x");
 44  Term y = tm.mkVar(integer, "y");
 45
 46  // declare the grammar non-terminals
 47  Term start = tm.mkVar(integer, "Start");
 48  Term start_bool = tm.mkVar(boolean, "StartBool");
 49
 50  // define the rules
 51  Term zero = tm.mkInteger(0);
 52  Term one = tm.mkInteger(1);
 53
 54  Term plus = tm.mkTerm(Kind::ADD, {start, start});
 55  Term minus = tm.mkTerm(Kind::SUB, {start, start});
 56  Term ite = tm.mkTerm(Kind::ITE, {start_bool, start, start});
 57
 58  Term And = tm.mkTerm(Kind::AND, {start_bool, start_bool});
 59  Term Not = tm.mkTerm(Kind::NOT, {start_bool});
 60  Term leq = tm.mkTerm(Kind::LEQ, {start, start});
 61
 62  // create the grammar object
 63  Grammar g = slv.mkGrammar({x, y}, {start, start_bool});
 64
 65  // bind each non-terminal to its rules
 66  g.addRules(start, {zero, one, x, y, plus, minus, ite});
 67  g.addRules(start_bool, {And, Not, leq});
 68
 69  // declare the functions-to-synthesize. Optionally, provide the grammar
 70  // constraints
 71  Term max = slv.synthFun("max", {x, y}, integer, g);
 72  Term min = slv.synthFun("min", {x, y}, integer);
 73
 74  // declare universal variables.
 75  Term varX = slv.declareSygusVar("x", integer);
 76  Term varY = slv.declareSygusVar("y", integer);
 77
 78  Term max_x_y = tm.mkTerm(Kind::APPLY_UF, {max, varX, varY});
 79  Term min_x_y = tm.mkTerm(Kind::APPLY_UF, {min, varX, varY});
 80
 81  // add semantic constraints
 82  // (constraint (>= (max x y) x))
 83  slv.addSygusConstraint(tm.mkTerm(Kind::GEQ, {max_x_y, varX}));
 84
 85  // (constraint (>= (max x y) y))
 86  slv.addSygusConstraint(tm.mkTerm(Kind::GEQ, {max_x_y, varY}));
 87
 88  // (constraint (or (= x (max x y))
 89  //                 (= y (max x y))))
 90  slv.addSygusConstraint(tm.mkTerm(Kind::OR,
 91                                   {tm.mkTerm(Kind::EQUAL, {max_x_y, varX}),
 92                                    tm.mkTerm(Kind::EQUAL, {max_x_y, varY})}));
 93
 94  // (constraint (= (+ (max x y) (min x y))
 95  //                (+ x y)))
 96  slv.addSygusConstraint(tm.mkTerm(Kind::EQUAL,
 97                                   {tm.mkTerm(Kind::ADD, {max_x_y, min_x_y}),
 98                                    tm.mkTerm(Kind::ADD, {varX, varY})}));
 99
100  // print solutions if available
101  if (slv.checkSynth().hasSolution())
102  {
103    // Output should be equivalent to:
104    // (
105    //   (define-fun max ((x Int) (y Int)) Int (ite (<= x y) y x))
106    //   (define-fun min ((x Int) (y Int)) Int (ite (<= x y) x y))
107    // )
108    std::vector<Term> terms = {max, min};
109    utils::printSynthSolutions(terms, slv.getSynthSolutions(terms));
110  }
111
112  return 0;
113}

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