/* (declare-fun p_name (E3 E3 ...) Bool) */
Z3_func_decl gen_pred(z3_wrapper *z3, const char *name, const pred *pred) {
  Z3_symbol symb = Z3_mk_string_symbol(z3->ctx, name);
  Z3_sort domain[pred->arity];
  for(size_t i = 0; i < pred->arity; ++i) {
      domain[i] = z3->Ek_sort;
  }
  Z3_sort range = Z3_mk_bool_sort(z3->ctx);
  Z3_func_decl p = Z3_mk_func_decl(z3->ctx, symb, pred->arity, domain, range);
  
  /* create assertions */
  for(size_t xs = 0; xs < int_pow(K, pred->arity); ++xs) {
    /* represent `xs` in the K-ary form,
     * with digits[0] being the highest digit. */
    uint32_t digits[pred->arity];
    get_K_digits(digits, pred->arity, xs);
    
    Z3_ast args[pred->arity];
    for(size_t i = 0; i < pred->arity; ++i) {
      args[i] = z3->Ek_consts[digits[i]];
    }
    Z3_ast phi = Z3_mk_app(z3->ctx, p, pred->arity, args);

    /* if the predicate equals to false on `xs` */
    if(!pred_compute(pred, xs)) {
      phi = Z3_mk_not(z3->ctx, phi);
    }

    /* printf("\n%s\n", Z3_ast_to_string(z3->ctx, phi)); */
    Z3_solver_assert(z3->ctx, z3->solver, phi);
  }

  return p;
}
Example #2
0
 Z3_sort Z3_API Z3_mk_set_sort(Z3_context c, Z3_sort ty) {
     Z3_TRY;
     return Z3_mk_array_sort(c, ty, Z3_mk_bool_sort(c));
     Z3_CATCH_RETURN(0);
 }
Example #3
0
int test(){
  int i;

  /* Create a Z3 context to contain formulas */

  Z3_config cfg = Z3_mk_config();
  Z3_context ctx = iz3_mk_context(cfg);
    
  int num = 2;

  Z3_ast *constraints = (Z3_ast *)malloc(num * sizeof(Z3_ast));

#if 1
  Z3_sort arr = Z3_mk_array_sort(ctx,Z3_mk_int_sort(ctx),Z3_mk_bool_sort(ctx)); 
  Z3_symbol  as  = Z3_mk_string_symbol(ctx, "a");
  Z3_symbol  bs  = Z3_mk_string_symbol(ctx, "b");
  Z3_symbol  xs  = Z3_mk_string_symbol(ctx, "x");

  Z3_ast a = Z3_mk_const(ctx,as,arr);
  Z3_ast b = Z3_mk_const(ctx,bs,arr);
  Z3_ast x = Z3_mk_const(ctx,xs,Z3_mk_int_sort(ctx));
  
  Z3_ast c1 = Z3_mk_eq(ctx,a,Z3_mk_store(ctx,b,x,Z3_mk_true(ctx)));
  Z3_ast c2 = Z3_mk_not(ctx,Z3_mk_select(ctx,a,x));
#else
  Z3_symbol  xs  = Z3_mk_string_symbol(ctx, "x");
  Z3_ast x = Z3_mk_const(ctx,xs,Z3_mk_bool_sort(ctx));
  Z3_ast c1 = Z3_mk_eq(ctx,x,Z3_mk_true(ctx));
  Z3_ast c2 = Z3_mk_eq(ctx,x,Z3_mk_false(ctx));

#endif

  constraints[0] = c1;
  constraints[1] = c2;
  
  /* print out the result for grins. */

  // Z3_string smtout = Z3_benchmark_to_smtlib_string (ctx, "foo", "QFLIA", "sat", "", num, constraints, Z3_mk_true(ctx));

  // Z3_string smtout = Z3_ast_to_string(ctx,constraints[0]);
  // Z3_string smtout = Z3_context_to_string(ctx);
  // puts(smtout);

  iz3_print(ctx,num,constraints,"iZ3temp.smt");

  /* Make room for interpolants. */

  Z3_ast *interpolants = (Z3_ast *)malloc((num-1) * sizeof(Z3_ast));

  /* Make room for the model. */

  Z3_model model = 0;

  /* Call the prover */

  Z3_lbool result = iz3_interpolate(ctx, num, constraints, interpolants, &model);

  switch (result) {
  
    /* If UNSAT, print the interpolants */
  case Z3_L_FALSE:
    printf("unsat, interpolants:\n");
    for(i = 0; i < num-1; i++)
      printf("%s\n", Z3_ast_to_string(ctx, interpolants[i]));
    break;
  case Z3_L_UNDEF:
    printf("fail\n");
    break;
  case Z3_L_TRUE:
    printf("sat\n");
    printf("model:\n%s\n", Z3_model_to_string(ctx, model));
    break;
  }

  /* Delete the model if there is one */
  
  if (model)
    Z3_del_model(ctx, model);
  
  /* Delete logical context (note, we call iz3_del_context, not
     Z3_del_context */

  iz3_del_context(ctx);

  return 1;
}
Example #4
0
/**
   \brief Create a fresh boolean variable.
*/
Z3_ast mk_fresh_bool_var(Z3_context ctx) 
{
    return Z3_mk_fresh_const(ctx, "k", Z3_mk_bool_sort(ctx));
}
Example #5
0
/**
   \brief Create a boolean variable using the given name.
*/
Z3_ast mk_bool_var(Z3_context ctx, const char * name) 
{
    Z3_sort ty = Z3_mk_bool_sort(ctx);
    return mk_var(ctx, name, ty);
}
    void test_smt_relation_api() {

        enable_trace("smt_relation");
        enable_trace("smt_relation2");
        enable_trace("quant_elim");
        Z3_config cfg = Z3_mk_config();
        Z3_set_param_value(cfg, "DL_DEFAULT_RELATION", "smt_relation2");
        Z3_context ctx = Z3_mk_context(cfg);
        Z3_fixedpoint dl = Z3_mk_fixedpoint(ctx);
        Z3_fixedpoint_inc_ref(ctx,dl);
        Z3_del_config(cfg);

        Z3_sort int_sort = Z3_mk_int_sort(ctx);
        Z3_sort bool_sort = Z3_mk_bool_sort(ctx);
        Z3_func_decl nil_decl, is_nil_decl;
        Z3_func_decl cons_decl, is_cons_decl, head_decl, tail_decl;

        Z3_sort list = Z3_mk_list_sort(
            ctx, 
            Z3_mk_string_symbol(ctx, "list"),
            int_sort, 
            &nil_decl,
            &is_nil_decl,
            &cons_decl,
            &is_cons_decl,
            &head_decl,
            &tail_decl);

        Z3_sort listint[2] = { list, int_sort };
        Z3_symbol p_sym = Z3_mk_string_symbol(ctx, "p");
        Z3_symbol q_sym = Z3_mk_string_symbol(ctx, "q");


        Z3_func_decl p = Z3_mk_func_decl(ctx, p_sym, 2, listint, bool_sort);
        Z3_func_decl q = Z3_mk_func_decl(ctx, q_sym, 2, listint, bool_sort);
        Z3_fixedpoint_register_relation(ctx, dl, p);
        Z3_fixedpoint_register_relation(ctx, dl, q);


        Z3_ast zero = Z3_mk_numeral(ctx, "0", int_sort);
        Z3_ast one  = Z3_mk_numeral(ctx, "1", int_sort);
        Z3_ast two  = Z3_mk_numeral(ctx, "2", int_sort);
        Z3_ast x = Z3_mk_bound(ctx, 0, list);
        Z3_ast y = Z3_mk_bound(ctx, 1, int_sort);
        Z3_ast z = Z3_mk_bound(ctx, 2, list);
        Z3_ast zero_x[2] = { zero, x };
        Z3_ast fx = Z3_mk_app(ctx, cons_decl, 2, zero_x);
        Z3_ast zero_fx[2] = { zero, fx };
        Z3_ast ffx = Z3_mk_app(ctx, cons_decl, 2, zero_fx);
        Z3_ast xy[2] = { x, y };
        Z3_ast zy[2] = { z, y };
        // Z3_ast ffxy[2] = { ffx, y };
        // Z3_ast fxy[2] = { fx, y };
        Z3_ast zero_nil[2] = { zero, Z3_mk_app(ctx, nil_decl, 0, 0) };
        Z3_ast f0 = Z3_mk_app(ctx, cons_decl, 2, zero_nil);
        Z3_ast zero_f0[2] = { zero, f0 };
        Z3_ast f1 = Z3_mk_app(ctx, cons_decl, 2, zero_f0);
        Z3_ast zero_f1[2] = { zero, f1 };
        Z3_ast f2 = Z3_mk_app(ctx, cons_decl, 2, zero_f1);
        Z3_ast zero_f2[2] = { zero, f2 };
        Z3_ast f3 = Z3_mk_app(ctx, cons_decl, 2, zero_f2);
        Z3_ast zero_f3[2] = { zero, f3 };
        Z3_ast f4 = Z3_mk_app(ctx, cons_decl, 2, zero_f3);
        Z3_ast zero_f4[2] = { zero, f4 };
        Z3_ast f5 = Z3_mk_app(ctx, cons_decl, 2, zero_f4);
        Z3_ast zero_z[2] = { zero, z };
        Z3_ast fz = Z3_mk_app(ctx, cons_decl, 2, zero_z);
        
        Z3_ast pxy = Z3_mk_app(ctx, p, 2, xy);
        Z3_ast pzy    = Z3_mk_app(ctx, p, 2, zy);
        Z3_ast qxy = Z3_mk_app(ctx, q, 2, xy);
        Z3_ast qzy = Z3_mk_app(ctx, q, 2, zy);
        Z3_ast even_y = Z3_mk_eq(ctx, zero, Z3_mk_mod(ctx, y, two)); 
        Z3_ast odd_y  = Z3_mk_eq(ctx, one, Z3_mk_mod(ctx, y, two));
        

        // p(x, y) :- odd(y), p(z,y), f(z) = x . // dead rule.
        // q(x, y) :- p(f(f(x)), y).
        // p(x, y) :- q(f(x), y)                 // x decreases
        // p(x, y) :- even y, x = f^5(0)         // initial condition.

        Z3_ast body1[3] = { pzy, Z3_mk_eq(ctx, fz, x), odd_y };
        Z3_ast body2[2] = { pzy, Z3_mk_eq(ctx, ffx, z) };
        Z3_ast body3[2] = { qzy, Z3_mk_eq(ctx, fx, z) };
        Z3_ast body4[2] = { even_y, Z3_mk_eq(ctx, x, f5) };
        Z3_fixedpoint_add_rule(ctx, dl, Z3_mk_implies(ctx, Z3_mk_and(ctx, 3, body1), pxy), 0);
        Z3_fixedpoint_add_rule(ctx, dl, Z3_mk_implies(ctx, Z3_mk_and(ctx, 2, body2), qxy), 0);
        Z3_fixedpoint_add_rule(ctx, dl, Z3_mk_implies(ctx, Z3_mk_and(ctx, 2, body3), pxy), 0);
        Z3_fixedpoint_add_rule(ctx, dl, Z3_mk_implies(ctx, Z3_mk_and(ctx, 2, body4), pxy), 0);

        Z3_lbool r = Z3_fixedpoint_query(ctx, dl, pxy);
        if (r != Z3_L_UNDEF) {
            std::cout << Z3_ast_to_string(ctx, Z3_fixedpoint_get_answer(ctx, dl)) << "\n";
        }

        Z3_del_context(ctx);

    }