Exemple #1
0
int main() {
  VC vc = vc_createValidityChecker(NULL);

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

  Type integer = vc_intType(vc);

  Expr x = vc_varExpr(vc, "x", integer);
  Expr y = vc_varExpr(vc, "y", integer);
  Expr zero = vc_ratExpr(vc, 0, 1);

  Expr x_positive = vc_gtExpr(vc, x, zero);
  Expr y_positive = vc_gtExpr(vc, y, zero);

  Expr two = vc_ratExpr(vc, 2, 1);
  Expr twox = vc_multExpr(vc, two, x);
  Expr twox_plus_y = vc_plusExpr(vc, twox, y);

  Expr three = vc_ratExpr(vc, 3, 1);
  Expr twox_plus_y_geq_3 = vc_geExpr(vc, twox_plus_y, three);

  Expr formula = vc_impliesExpr(vc, vc_andExpr(vc, x_positive, y_positive),
                                twox_plus_y_geq_3);

  char* formulaString = vc_printExprString(vc, formula);

  printf("Checking validity of formula %s with CVC4.\n", formulaString);
  printf("CVC4 should return 1 (meaning VALID).\n");
  printf("Result from CVC4 is: %d\n", vc_query(vc, formula));

  free(formulaString);

  return 0;
}
Exemple #2
0
Expr parseNumber(char* token)
{
  char ds[15];
  char* f = strchr(token, '.');
  if (f != NULL){
    //printf("%s\n",token);
    int p = strlen(f+1);
    strncpy(ds, token, strlen(token) - p - 1);
    
    int powOf10 = 1, i;
    for (i=0; i < p; i++)
      powOf10 *= 10;

    int num;
    if (token[0] == '-')
      num = -(powOf10 * atoi(ds+1) + atoi(f+1));
    else
      num = powOf10 * atoi(ds) + atoi(f+1);
    //printf("%d/%d\n",num,powOf10);
    return vc_ratExpr(vc, num, powOf10);
  }
  else{
    return vc_ratExpr(vc, atoi(token), 1);
  }
}
Exemple #3
0
void test1()
{
  Flags flags = vc_createFlags();
  VC vc;
  Type b;
  Expr p, np, e;
  Type r, real2real;
  Expr x, y, fx, fy, xeqy, fxeqfy, w, z, weqx, yeqz, one, two, xeqone, xeqtwo,
    simp, simp2;
  Op f;
  Expr* assertions;
  int i, size, res;
  Kind k;

  vc_setStringFlag(flags, "dump-log", ".testc1.cvc");
  vc_setStrSeqFlag(flags, "trace", "pushpop", 1);

  vc = vc_createValidityChecker(flags);

  // Check p OR ~p

  b = vc_boolType(vc);
  p = vc_varExpr(vc, "p", vc_boolType(vc));
  np = vc_notExpr(vc, p);
  e = vc_orExpr(vc, p, np);

  res = check(vc, e);
  FatalAssert(res == 1, "Expected Valid");

  FatalAssert(vc_getKind(e) == OR, "Expected TRUE for kind check");
  FatalAssert(vc_getKind(vc_getType(vc, e)) == BOOLEAN, "Expected TRUE for type kind check");

  vc_deleteType(b);
  vc_deleteExpr(p);
  vc_deleteExpr(np);
  vc_deleteExpr(e);

  /* Check x = y -> f(x) = f(y) */

  r = vc_realType(vc);

  x = vc_varExpr(vc, "x", r);
  y = vc_varExpr(vc, "y", r);

  real2real = vc_funType1(vc, r, r);
  f = vc_createOp(vc, "f", real2real);

  fx = vc_funExpr1(vc, f, x);
  fy = vc_funExpr1(vc, f, y);
  
  xeqy = vc_eqExpr(vc, x, y);
  fxeqfy = vc_eqExpr(vc, fx, fy);

  e = vc_impliesExpr(vc, xeqy, fxeqfy);
  res = check(vc, e);
  FatalAssert(res == 1, "Expected Valid");

  vc_deleteType(real2real);
  vc_deleteExpr(e);

  // Check f(x) = f(y) -> x = y

  e = vc_impliesExpr(vc, fxeqfy, xeqy);
  vc_push(vc);
  res = check(vc, e);
  FatalAssert(res == 0, "Expected Invalid");
  vc_deleteExpr(e);

  // Get counter-example

  printf("Stack level: %d\n", vc_stackLevel(vc));
  printf("Counter-example:\n");
  assertions = vc_getCounterExample(vc, &size);
  
  for (i = 0; i < size; ++i) {
    vc_printExpr(vc, assertions[i]);
  }
  vc_deleteVector(assertions);
  printf("End of counter-example\n\n");

  printf("Concrete model:\n");
  assertions = vc_getConcreteModel(vc, &size);
  
  for (i = 0; i < size; ++i) {
    vc_printExpr(vc, assertions[i]);
  }
  vc_deleteVector(assertions);
  printf("End of concrete model\n\n");

  // Reset to initial scope
  printf("Resetting\n");
  vc_pop(vc);
  printf("Stack level: %d\n\n", vc_stackLevel(vc));

  // Check w = x & x = y & y = z & f(x) = f(y) & x = 1 & z = 2

  w = vc_varExpr(vc, "w", r);
  z = vc_varExpr(vc, "z", r);

  printf("Push Scope\n\n");
  vc_push(vc);

  weqx = vc_eqExpr(vc, w, x);
  yeqz = vc_eqExpr(vc, y, z);
  
  one = vc_ratExpr(vc, 1, 1);
  two = vc_ratExpr(vc, 2, 1);
  xeqone = vc_eqExpr(vc, x, one);
  xeqtwo = vc_eqExpr(vc, x, two);

  newAssertion(vc, weqx);
  newAssertion(vc, xeqy);
  newAssertion(vc, yeqz);
  newAssertion(vc, fxeqfy);
  newAssertion(vc, xeqone);
  newAssertion(vc, xeqtwo);

  printf("\nsimplify(w) = ");

  simp = vc_simplify(vc, w);

  char* str = vc_printExprString(vc, simp);
  printf("%s\n", str);
  vc_deleteString(str);

  printf("Inconsistent?: %d\n", vc_inconsistent(vc, &assertions, &size));
  check_error("Error occured during inconsistency check");

  printf("Assumptions Used:\n");
  for (i = 0; i < size; ++i) {
    vc_printExpr(vc, assertions[i]);
  }
  vc_deleteVector(assertions);

  printf("\nPop Scope\n\n");
  vc_pop(vc);

  printf("simplify(w) = ");

  simp2 = vc_simplify(vc, w);
  vc_printExpr(vc, simp2);
  printf("\n");

  printf("Inconsistent?: %d\n", vc_inconsistent(vc, &assertions, &size));
  vc_deleteVector(assertions);

  vc_deleteType(r);
  vc_deleteExpr(x);
  vc_deleteExpr(y);
  vc_deleteOp(f);
  vc_deleteExpr(fx);
  vc_deleteExpr(fy);
  vc_deleteExpr(xeqy);
  vc_deleteExpr(fxeqfy);
  vc_deleteExpr(w);
  vc_deleteExpr(z);
  vc_deleteExpr(weqx);
  vc_deleteExpr(yeqz);
  vc_deleteExpr(one);
  vc_deleteExpr(two);
  vc_deleteExpr(xeqone);
  vc_deleteExpr(xeqtwo);
  vc_deleteExpr(simp);
  vc_deleteExpr(simp2);

  vc_destroyValidityChecker(vc);
  vc_deleteFlags(flags);
}
Exemple #4
0
void test4(int regressLevel)
{
  VC vc = vc_createValidityChecker(NULL);

  // Check x >= 10 /\ x >= 40 /\ y <= 0 -->
  //       x >= 1 /\ y < 10

  Type r = vc_realType(vc);
  Expr x = vc_varExpr(vc, "x", r);
  Expr y = vc_varExpr(vc, "y", r);

  Expr ten = vc_ratExpr(vc, 10, 1);
  Expr ge = vc_geExpr(vc, x, ten);

  Expr forty = vc_ratExpr(vc, 40, 1);
  Expr ge2 = vc_geExpr(vc, x, forty);

  Expr zero = vc_ratExpr(vc, 0, 1);
  Expr ge3 = vc_leExpr(vc, y, zero);

  Expr children[3];
  Expr hyp, one, conc, query;
  int i;

  children[0] = ge;
  children[1] = ge2;
  children[2] = ge3;

  hyp = vc_andExprN(vc, children, 3);

  vc_deleteType(r);
  vc_deleteExpr(ge);
  vc_deleteExpr(forty);
  vc_deleteExpr(ge2);
  vc_deleteExpr(zero);
  vc_deleteExpr(ge3);

  one = vc_ratExpr(vc, 1, 1);
  ge = vc_geExpr(vc, x, one);

  ge2 = vc_ltExpr(vc, y, ten);

  conc = vc_andExpr(vc, ge, ge2);
  query = vc_impliesExpr(vc, hyp, conc);

  vc_deleteExpr(x);
  vc_deleteExpr(y);
  vc_deleteExpr(ten);
  vc_deleteExpr(hyp);
  vc_deleteExpr(one);
  vc_deleteExpr(ge);
  vc_deleteExpr(ge2);
  vc_deleteExpr(conc);

  for (i = 0; i < 100*regressLevel; i++)
    vc_query(vc, query);

  vc_deleteExpr(query);

  vc_destroyValidityChecker(vc);
}
Exemple #5
0
// Create a rational number of numerator n and denominator d.
value caml_vc_ratExpr(value vc, value n, value d)
{
  CAMLparam3(vc,n,d);
  CAMLreturn(alloc_Expr(vc_ratExpr(VC_val(vc),Int_val(n),Int_val(d))));
}