Exemplo n.º 1
0
int main() {
  ExprManager em;
  SmtEngine smt(&em);

  // Prove that for integers x and y:
  //   x > 0 AND y > 0  =>  2x + y >= 3

  Type integer = em.integerType();

  Expr x = em.mkVar("x", integer);
  Expr y = em.mkVar("y", integer);
  Expr zero = em.mkConst(Rational(0));

  Expr x_positive = em.mkExpr(kind::GT, x, zero);
  Expr y_positive = em.mkExpr(kind::GT, y, zero);

  Expr two = em.mkConst(Rational(2));
  Expr twox = em.mkExpr(kind::MULT, two, x);
  Expr twox_plus_y = em.mkExpr(kind::PLUS, twox, y);

  Expr three = em.mkConst(Rational(3));
  Expr twox_plus_y_geq_3 = em.mkExpr(kind::GEQ, twox_plus_y, three);

  Expr formula =
    em.mkExpr(kind::AND, x_positive, y_positive).
    impExpr(twox_plus_y_geq_3);

  cout << "Checking validity of formula " << formula << " with CVC4." << endl;
  cout << "CVC4 should report VALID." << endl;
  cout << "Result from CVC4 is: " << smt.query(formula) << endl;

  return 0;
}
Exemplo n.º 2
0
int main() {
  ExprManager em;
  SmtEngine smt(&em);
  smt.setLogic("QF_BV"); // Set the logic

  Type bitvector32 = em.mkBitVectorType(32);

  Expr x = em.mkVar("a", bitvector32);

  Expr ext_31_1 = em.mkConst(CVC4::BitVectorExtract(31,1));
  Expr x_31_1 = em.mkExpr(ext_31_1, x);

  Expr ext_30_0 = em.mkConst(CVC4::BitVectorExtract(30,0));
  Expr x_30_0 = em.mkExpr(ext_30_0, x);

  Expr ext_31_31 = em.mkConst(CVC4::BitVectorExtract(31,31));
  Expr x_31_31 = em.mkExpr(ext_31_31, x);

  Expr ext_0_0 = em.mkConst(CVC4::BitVectorExtract(0,0));
  Expr x_0_0 = em.mkExpr(ext_0_0, x);

  Expr eq = em.mkExpr(kind::EQUAL, x_31_1, x_30_0);
  cout << " Asserting: " << eq << endl;
  smt.assertFormula(eq);

  Expr eq2 = em.mkExpr(kind::EQUAL, x_31_31, x_0_0);
  cout << " Querying: " << eq2 << endl;
  cout << " Expect valid. " << endl;
  cout << " CVC4: " << smt.query(eq2) << endl;

  return 0;
}
Exemplo n.º 3
0
void Tptp::addTheory(Theory theory) {
  ExprManager * em = getExprManager();
  switch(theory) {
  case THEORY_CORE:
    //TPTP (CNF and FOF) is unsorted so we define this common type
    {
      std::string d_unsorted_name = "$$unsorted";
      d_unsorted = em->mkSort(d_unsorted_name);
      preemptCommand( new DeclareTypeCommand(d_unsorted_name, 0, d_unsorted) );
    }
    // propositionnal
    defineType("Bool", em->booleanType());
    defineVar("$true", em->mkConst(true));
    defineVar("$false", em->mkConst(false));
    addOperator(kind::AND);
    addOperator(kind::EQUAL);
    addOperator(kind::IMPLIES);
    //addOperator(kind::ITE); //only for tff thf
    addOperator(kind::NOT);
    addOperator(kind::OR);
    addOperator(kind::XOR);
    addOperator(kind::APPLY_UF);
    //Add quantifiers?
    break;

  default:
    std::stringstream ss;
    ss << "internal error: Tptp::addTheory(): unhandled theory " << theory;
    throw ParserException(ss.str());
  }
}
Exemplo n.º 4
0
int main() {
    ExprManager em;
    Options opts;
    SmtEngine smt(&em);
    SmtEngine smt2(&em);
    Result r = smt.query(em.mkConst(true));
    Result r2 = smt2.query(em.mkConst(true));

    return r == Result::VALID && r2 == Result::VALID ? 0 : 1;
}
Exemplo n.º 5
0
int main() {
  ExprManager em;
  Options opts;
  SmtEngine smt(&em);
  Result r = smt.query(em.mkConst(true));

  return (Result::VALID == r) ? 0 : 1;
}
Exemplo n.º 6
0
int main() {
  ExprManager em;
  Options opts;
  SmtEngine smt(&em);
  smt.setOption("incremental", SExpr("true"));
  Expr x = em.mkVar("x", em.integerType());
  Expr y = em.mkVar("y", em.integerType());
  smt.assertFormula(em.mkExpr(kind::GT, em.mkExpr(kind::PLUS, x, y), em.mkConst(Rational(5))));
  Expr q = em.mkExpr(kind::GT, x, em.mkConst(Rational(0)));
  Result r = smt.query(q);

  if(r != Result::INVALID) {
    exit(1);
  }

  Statistics stats = smt.getStatistics();
  for(Statistics::iterator i = stats.begin(); i != stats.end(); ++i) {
    cout << "stat " << (*i).first << " is " << (*i).second << endl;
  }

  smt.assertFormula(em.mkExpr(kind::LT, y, em.mkConst(Rational(5))));
  r = smt.query(q);
  Statistics stats2 = smt.getStatistics();
  bool different = false;
  for(Statistics::iterator i = stats2.begin(); i != stats2.end(); ++i) {
    cout << "stat1 " << (*i).first << " is " << stats.getStatistic((*i).first) << endl;
    cout << "stat2 " << (*i).first << " is " << (*i).second << endl;
    if(smt.getStatistic((*i).first) != (*i).second) {
      cout << "SMT engine reports different value for statistic "
           << (*i).first << ": " << smt.getStatistic((*i).first) << endl;
      exit(1);
    }
    different = different || stats.getStatistic((*i).first) != (*i).second;
  }

#ifdef CVC4_STATISTICS_ON
  if(!different) {
    cout << "stats are the same!  bailing.." << endl;
    exit(1);
  }
#endif /* CVC4_STATISTICS_ON */

  return r == Result::VALID ? 0 : 1;
}
Exemplo n.º 7
0
int main() {
  ExprManager em;
  SmtEngine smt(&em);
  smt.setOption("produce-models", true); // Produce Models
  smt.setOption("output-language", "cvc4"); // Set the output-language to CVC's
  smt.setOption("default-dag-thresh", 0); //Disable dagifying the output
  smt.setLogic(string("QF_UFLIRA"));

  // Sorts
  SortType u = em.mkSort("u");
  Type integer = em.integerType();
  Type boolean = em.booleanType();
  Type uToInt = em.mkFunctionType(u, integer);
  Type intPred = em.mkFunctionType(integer, boolean);

  // Variables
  Expr x = em.mkVar("x", u);
  Expr y = em.mkVar("y", u);

  // Functions
  Expr f = em.mkVar("f", uToInt);
  Expr p = em.mkVar("p", intPred);

  // Constants
  Expr zero = em.mkConst(Rational(0));
  Expr one = em.mkConst(Rational(1));

  // Terms
  Expr f_x = em.mkExpr(kind::APPLY_UF, f, x);
  Expr f_y = em.mkExpr(kind::APPLY_UF, f, y);
  Expr sum = em.mkExpr(kind::PLUS, f_x, f_y);
  Expr p_0 = em.mkExpr(kind::APPLY_UF, p, zero);
  Expr p_f_y = em.mkExpr(kind::APPLY_UF, p, f_y);

  // Construct the assumptions
  Expr assumptions =
    em.mkExpr(kind::AND,
              em.mkExpr(kind::LEQ, zero, f_x), // 0 <= f(x)
              em.mkExpr(kind::LEQ, zero, f_y), // 0 <= f(y)
              em.mkExpr(kind::LEQ, sum, one),  // f(x) + f(y) <= 1
              p_0.notExpr(),                   // not p(0)
              p_f_y);                          // p(f(y))
  smt.assertFormula(assumptions);

  cout << "Given the following assumptions:" << endl
       << assumptions << endl
       << "Prove x /= y is valid. "
       << "CVC4 says: " << smt.query(em.mkExpr(kind::DISTINCT, x, y))
       << "." << endl;

  cout << "Now we call checksat on a trivial query to show that" << endl
       << "the assumptions are satisfiable: "
       << smt.checkSat(em.mkConst(true)) << "."<< endl;

  cout << "Finally, after a SAT call, we recursively call smt.getValue(...) on"
       << "all of the assumptions to see what the satisfying model looks like."
       << endl;
  prefixPrintGetValue(smt, assumptions);

  return 0;
}
Exemplo n.º 8
0
int main() {
  ExprManager em;
  SmtEngine smt(&em);
  smt.setOption("produce-models", true);      // Produce Models
  smt.setOption("output-language", "smtlib"); // output-language
  smt.setLogic("QF_AUFBV");                   // Set the logic

  // Consider the following code (where size is some previously defined constant):
  //
  //
  //   Assert (current_array[0] > 0);
  //   for (unsigned i = 1; i < k; ++i) {
  //     current_array[i] = 2 * current_array[i - 1];
  //     Assert (current_array[i-1] < current_array[i]);
  //     }
  //
  // We want to check whether the assertion in the body of the for loop holds
  // throughout the loop.

  // Setting up the problem parameters
  unsigned k = 4;                // number of unrollings (should be a power of 2)
  unsigned index_size = log2(k); // size of the index


  // Types
  Type elementType = em.mkBitVectorType(32);
  Type indexType = em.mkBitVectorType(index_size);
  Type arrayType = em.mkArrayType(indexType, elementType);

  // Variables
  Expr current_array = em.mkVar("current_array", arrayType);

  // Making a bit-vector constant
  Expr zero = em.mkConst(BitVector(index_size, 0u));

  // Asserting that current_array[0] > 0
  Expr current_array0 = em.mkExpr(kind::SELECT, current_array, zero);
  Expr current_array0_gt_0 = em.mkExpr(kind::BITVECTOR_SGT, current_array0, em.mkConst(BitVector(32, 0u)));
  smt.assertFormula(current_array0_gt_0);

  // Building the assertions in the loop unrolling
  Expr index = em.mkConst(BitVector(index_size, 0u));
  Expr old_current = em.mkExpr(kind::SELECT, current_array, index);
  Expr two = em.mkConst(BitVector(32, 2u));

  std::vector<Expr> assertions;
  for (unsigned i = 1; i < k; ++i) {
    index = em.mkConst(BitVector(index_size, Integer(i)));
    Expr new_current = em.mkExpr(kind::BITVECTOR_MULT, two, old_current);
    // current[i] = 2 * current[i-1]
    current_array = em.mkExpr(kind::STORE, current_array, index, new_current);
    // current[i-1] < current [i]
    Expr current_slt_new_current = em.mkExpr(kind::BITVECTOR_SLT, old_current, new_current);
    assertions.push_back(current_slt_new_current);

    old_current = em.mkExpr(kind::SELECT, current_array, index);
  }

  Expr query = em.mkExpr(kind::NOT, em.mkExpr(kind::AND, assertions));

  cout << "Asserting " << query << " to CVC4 " << endl;
  smt.assertFormula(query);
  cout << "Expect sat. " << endl;
  cout << "CVC4: " << smt.checkSat(em.mkConst(true)) << endl;

  // Getting the model
  cout << "The satisfying model is: " << endl;
  cout << "  current_array = " << smt.getValue(current_array) << endl;
  cout << "  current_array[0] = " << smt.getValue(current_array0) << endl;
  return 0;
}
Exemplo n.º 9
0
int main() {
  ExprManager em;
  SmtEngine smt(&em);

  // Set the logic
  smt.setLogic("S");

  // Produce models
  smt.setOption("produce-models", true);

  // The option strings-exp is needed
  smt.setOption("strings-exp", true);

  // Set output language to SMTLIB2
  std::cout << language::SetLanguage(language::output::LANG_SMTLIB_V2);

  // String type
  Type string = em.stringType();

  // std::string
  std::string std_str_ab("ab");
  // CVC4::String
  CVC4::String cvc4_str_ab(std_str_ab);
  CVC4::String cvc4_str_abc("abc");
  // String constants
  Expr ab  = em.mkConst(cvc4_str_ab);
  Expr abc = em.mkConst(CVC4::String("abc"));
  // String variables
  Expr x = em.mkVar("x", string);
  Expr y = em.mkVar("y", string);
  Expr z = em.mkVar("z", string);

  // String concatenation: x.ab.y
  Expr lhs = em.mkExpr(kind::STRING_CONCAT, x, ab, y);
  // String concatenation: abc.z
  Expr rhs = em.mkExpr(kind::STRING_CONCAT, abc, z);
  // x.ab.y = abc.z
  Expr formula1 = em.mkExpr(kind::EQUAL, lhs, rhs);

  // Length of y: |y|
  Expr leny = em.mkExpr(kind::STRING_LENGTH, y);
  // |y| >= 0
  Expr formula2 = em.mkExpr(kind::GEQ, leny, em.mkConst(Rational(0)));

  // Regular expression: (ab[c-e]*f)|g|h
  Expr r = em.mkExpr(kind::REGEXP_UNION,
    em.mkExpr(kind::REGEXP_CONCAT,
      em.mkExpr(kind::STRING_TO_REGEXP, em.mkConst(String("ab"))),
      em.mkExpr(kind::REGEXP_STAR,
        em.mkExpr(kind::REGEXP_RANGE, em.mkConst(String("c")), em.mkConst(String("e")))),
      em.mkExpr(kind::STRING_TO_REGEXP, em.mkConst(String("f")))),
    em.mkExpr(kind::STRING_TO_REGEXP, em.mkConst(String("g"))),
    em.mkExpr(kind::STRING_TO_REGEXP, em.mkConst(String("h"))));

  // String variables
  Expr s1 = em.mkVar("s1", string);
  Expr s2 = em.mkVar("s2", string);
  // String concatenation: s1.s2
  Expr s = em.mkExpr(kind::STRING_CONCAT, s1, s2);

  // s1.s2 in (ab[c-e]*f)|g|h
  Expr formula3 = em.mkExpr(kind::STRING_IN_REGEXP, s, r);

  // Make a query
  Expr q = em.mkExpr(kind::AND,
    formula1,
    formula2,
    formula3);

  // check sat
  Result result = smt.checkSat(q);
  std::cout << "CVC4 reports: " << q << " is " << result << "." << std::endl;

  if(result == Result::SAT) {
    std::cout << "  x  = " << smt.getValue(x) << std::endl;
    std::cout << "  s1.s2 = " << smt.getValue(s) << std::endl;
  }
}