Exemple #1
0
Expr Tptp::convertRatToUnsorted(Expr expr) {
  ExprManager* em = getExprManager();

  // Create the conversion function If they doesn't exists
  if (d_rtu_op.isNull()) {
    Type t;
    // Conversion from rational to unsorted
    t = em->mkFunctionType(em->realType(), d_unsorted);
    d_rtu_op = em->mkVar("$$rtu", t);
    preemptCommand(new DeclareFunctionCommand("$$rtu", d_rtu_op, t));
    // Conversion from unsorted to rational
    t = em->mkFunctionType(d_unsorted, em->realType());
    d_utr_op = em->mkVar("$$utr", t);
    preemptCommand(new DeclareFunctionCommand("$$utr", d_utr_op, t));
  }
  // Add the inverse in order to show that over the elements that
  // appear in the problem there is a bijection between unsorted and
  // rational
  Expr ret = em->mkExpr(kind::APPLY_UF, d_rtu_op, expr);
  if (d_r_converted.find(expr) == d_r_converted.end()) {
    d_r_converted.insert(expr);
    Expr eq = em->mkExpr(kind::EQUAL, expr,
                         em->mkExpr(kind::APPLY_UF, d_utr_op, ret));
    preemptCommand(new AssertCommand(eq));
  }
  return ret;
}
Exemple #2
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;
}
Exemple #3
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;
}
Exemple #4
0
int main() {
  ExprManager em;
  Expr helloworld = em.mkVar("Hello World!", em.booleanType());
  SmtEngine smt(&em);
  std::cout << helloworld << " is " << smt.query(helloworld) << std::endl;
  return 0;
}
Exemple #5
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;
}
Exemple #6
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;
}
Exemple #7
0
int main() {
    ExprManager em;
    SmtEngine smt(&em);
    smt.setLogic("QF_BV"); // Set the logic

    // The following example has been adapted from the book A Hacker's Delight by
    // Henry S. Warren.
    //
    // Given a variable x that can only have two values, a or b. We want to
    // assign to x a value other than the current one. The straightforward code
    // to do that is:
    //
    //(0) if (x == a ) x = b;
    //    else x = a;
    //
    // Two more efficient yet equivalent methods are:
    //
    //(1) x = a ⊕ b ⊕ x;
    //
    //(2) x = a + b - x;
    //
    // We will use CVC4 to prove that the three pieces of code above are all
    // equivalent by encoding the problem in the bit-vector theory.

    // Creating a bit-vector type of width 32
    Type bitvector32 = em.mkBitVectorType(32);

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

    // First encode the assumption that x must be equal to a or b
    Expr x_eq_a = em.mkExpr(kind::EQUAL, x, a);
    Expr x_eq_b = em.mkExpr(kind::EQUAL, x, b);
    Expr assumption = em.mkExpr(kind::OR, x_eq_a, x_eq_b);

    // Assert the assumption
    smt.assertFormula(assumption);

    // Introduce a new variable for the new value of x after assignment.
    Expr new_x = em.mkVar("new_x", bitvector32); // x after executing code (0)
    Expr new_x_ = em.mkVar("new_x_", bitvector32); // x after executing code (1) or (2)

    // Encoding code (0)
    // new_x = x == a ? b : a;
    Expr ite = em.mkExpr(kind::ITE, x_eq_a, b, a);
    Expr assignment0 = em.mkExpr(kind::EQUAL, new_x, ite);

    // Assert the encoding of code (0)
    cout << "Asserting " << assignment0 << " to CVC4 " << endl;
    smt.assertFormula(assignment0);
    cout << "Pushing a new context." << endl;
    smt.push();

    // Encoding code (1)
    // new_x_ = a xor b xor x
    Expr a_xor_b_xor_x = em.mkExpr(kind::BITVECTOR_XOR, a, b, x);
    Expr assignment1 = em.mkExpr(kind::EQUAL, new_x_, a_xor_b_xor_x);

    // Assert encoding to CVC4 in current context;
    cout << "Asserting " << assignment1 << " to CVC4 " << endl;
    smt.assertFormula(assignment1);
    Expr new_x_eq_new_x_ = em.mkExpr(kind::EQUAL, new_x, new_x_);

    cout << " Querying: " << new_x_eq_new_x_ << endl;
    cout << " Expect valid. " << endl;
    cout << " CVC4: " << smt.query(new_x_eq_new_x_) << endl;
    cout << " Popping context. " << endl;
    smt.pop();

    // Encoding code (2)
    // new_x_ = a + b - x
    Expr a_plus_b = em.mkExpr(kind::BITVECTOR_PLUS, a, b);
    Expr a_plus_b_minus_x = em.mkExpr(kind::BITVECTOR_SUB, a_plus_b, x);
    Expr assignment2 = em.mkExpr(kind::EQUAL, new_x_, a_plus_b_minus_x);

    // Assert encoding to CVC4 in current context;
    cout << "Asserting " << assignment2 << " to CVC4 " << endl;
    smt.assertFormula(assignment2);

    cout << " Querying: " << new_x_eq_new_x_ << endl;
    cout << " Expect valid. " << endl;
    cout << " CVC4: " << smt.query(new_x_eq_new_x_) << endl;

    return 0;
}
Exemple #8
0
  Expr add(SetType t, Expr e) {

    if(setTypes.find(t) == setTypes.end() ) {
      // mark as processed
      setTypes.insert(t);

      Type elementType = t.getElementType();
      ostringstream oss_type;
      oss_type << language::SetLanguage(language::output::LANG_SMTLIB_V2)
               << elementType;
      string elementTypeAsString = oss_type.str();
      elementTypeAsString.erase(
        remove_if(elementTypeAsString.begin(), elementTypeAsString.end(), nonsense),
        elementTypeAsString.end());

      // define-sort
      ostringstream oss_name;
      oss_name << language::SetLanguage(language::output::LANG_SMTLIB_V2)
               << "(Set " << elementType << ")";
      string name = oss_name.str();
      Type newt = em->mkArrayType(t.getElementType(), em->booleanType());
      mapTypes[t] = newt;

      // diffent types
      vector<Type> t_t;
      t_t.push_back(t);
      t_t.push_back(t);
      vector<Type> elet_t;
      elet_t.push_back(elementType);
      elet_t.push_back(t);

      if(!enableAxioms)
        sout << "(define-fun emptyset" << elementTypeAsString << "    "
             << " ()"
             << " " << name
             << " ( (as const " << name << ") false ) )" << endl;
      setoperators[ make_pair(t, kind::EMPTYSET) ] =
        em->mkVar( std::string("emptyset") + elementTypeAsString,
                   t);

      if(!enableAxioms)
        sout << "(define-fun singleton" << elementTypeAsString << "     "
             << " ( (x " << elementType << ") )"
             << " " << name << ""
             << " (store emptyset" << elementTypeAsString << " x true) )" << endl;
      setoperators[ make_pair(t, kind::SINGLETON) ] =
        em->mkVar( std::string("singleton") + elementTypeAsString,
                   em->mkFunctionType( elementType, t ) );

      if(!enableAxioms)
        sout << "(define-fun union" << elementTypeAsString << "       "
             << " ( (s1 " << name << ") (s2 " << name << ") )"
             << " " << name << ""
             << " ((_ map or) s1 s2))" << endl;
      setoperators[ make_pair(t, kind::UNION) ] =
        em->mkVar( std::string("union") + elementTypeAsString,
                   em->mkFunctionType( t_t, t ) );

      if(!enableAxioms)
        sout << "(define-fun intersection" << elementTypeAsString << ""
             << " ( (s1 " << name << ") (s2 " << name << ") )"
             << " " << name << ""
             << " ((_ map and) s1 s2))" << endl;
      setoperators[ make_pair(t, kind::INTERSECTION) ] =
        em->mkVar( std::string("intersection") + elementTypeAsString,
                   em->mkFunctionType( t_t, t ) );

      if(!enableAxioms)
        sout << "(define-fun setminus" << elementTypeAsString << "    "
             << " ( (s1 " << name << ") (s2 " << name << ") )"
             << " " << name << ""
             << " (intersection" << elementTypeAsString << " s1 ((_ map not) s2)))" << endl;
      setoperators[ make_pair(t, kind::SETMINUS) ] =
        em->mkVar( std::string("setminus") + elementTypeAsString,
                   em->mkFunctionType( t_t, t ) );

      if(!enableAxioms)
        sout << "(define-fun member" << elementTypeAsString << "          "
             << " ( (x " << elementType << ")" << " (s " << name << "))"
             << " Bool"
             << " (select s x) )" << endl;
      setoperators[ make_pair(t, kind::MEMBER) ] =
        em->mkVar( std::string("member") + elementTypeAsString,
                   em->mkPredicateType( elet_t ) );

      if(!enableAxioms)
        sout << "(define-fun subset" << elementTypeAsString << "    "
             << " ( (s1 " << name << ") (s2 " << name << ") )"
             << " Bool"
             <<" (= emptyset" << elementTypeAsString << " (setminus" << elementTypeAsString << " s1 s2)) )" << endl;
      setoperators[ make_pair(t, kind::SUBSET) ] =
        em->mkVar( std::string("subset") + elementTypeAsString,
                   em->mkPredicateType( t_t ) );

      if(enableAxioms) {
        int N = sizeof(setaxioms) / sizeof(setaxioms[0]);
        for(int i = 0; i < N; ++i) {
          string s = setaxioms[i];
          ostringstream oss;
          oss << language::SetLanguage(language::output::LANG_SMTLIB_V2) << elementType;
          boost::replace_all(s, "HOLDA", elementTypeAsString);
          boost::replace_all(s, "HOLDB", oss.str());
          if( s == "" ) continue;
          sout << s << endl;
        }
      }

    }
    Expr ret;
    if(e.getKind() == kind::EMPTYSET) {
      ret = setoperators[ make_pair(t, e.getKind()) ];
    } else {
      vector<Expr> children = e.getChildren();
      children.insert(children.begin(), setoperators[ make_pair(t, e.getKind()) ]);
      ret = em->mkExpr(kind::APPLY, children);
    }
    // cout << "returning " << ret  << endl;
    return ret;
  }
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;
}
Exemple #10
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;
  }
}