Example #1
0
 Z3Sort Z3Expr::GetSort() const
 {
     if (Ctx == nullptr || AST == nullptr) {
         return Z3Sort();
     } else {
         return Z3Sort(Ctx, Z3_get_sort(Ctx, AST));
     }
 }
Example #2
0
bool Z3SolverImpl::validateZ3Model(::Z3_solver &theSolver, ::Z3_model &theModel) {
  bool success = true;
  ::Z3_ast_vector constraints =
      Z3_solver_get_assertions(builder->ctx, theSolver);
  Z3_ast_vector_inc_ref(builder->ctx, constraints);

  unsigned size = Z3_ast_vector_size(builder->ctx, constraints);

  for (unsigned index = 0; index < size; ++index) {
    Z3ASTHandle constraint = Z3ASTHandle(
        Z3_ast_vector_get(builder->ctx, constraints, index), builder->ctx);

    ::Z3_ast rawEvaluatedExpr;
    bool successfulEval =
        Z3_model_eval(builder->ctx, theModel, constraint,
                      /*model_completion=*/Z3_TRUE, &rawEvaluatedExpr);
    assert(successfulEval && "Failed to evaluate model");

    // Use handle to do ref-counting.
    Z3ASTHandle evaluatedExpr(rawEvaluatedExpr, builder->ctx);

    Z3SortHandle sort =
        Z3SortHandle(Z3_get_sort(builder->ctx, evaluatedExpr), builder->ctx);
    assert(Z3_get_sort_kind(builder->ctx, sort) == Z3_BOOL_SORT &&
           "Evaluated expression has wrong sort");

    Z3_lbool evaluatedValue =
        Z3_get_bool_value(builder->ctx, evaluatedExpr);
    if (evaluatedValue != Z3_L_TRUE) {
      llvm::errs() << "Validating model failed:\n"
                   << "The expression:\n";
      constraint.dump();
      llvm::errs() << "evaluated to \n";
      evaluatedExpr.dump();
      llvm::errs() << "But should be true\n";
      success = false;
    }
  }

  if (!success) {
    llvm::errs() << "Solver state:\n" << Z3_solver_to_string(builder->ctx, theSolver) << "\n";
    llvm::errs() << "Model:\n" << Z3_model_to_string(builder->ctx, theModel) << "\n";
  }

  Z3_ast_vector_dec_ref(builder->ctx, constraints);
  return success;
}
Example #3
0
void display_solver_term(SOLVER_CONTEXT ctx, FILE * out, SOLVER_TERM term){

  Z3_context c = (Z3_context) ctx;
  Z3_ast     v = (Z3_ast) term;

  switch (Z3_get_ast_kind(c, v)) {
  case Z3_NUMERAL_AST: {
    Z3_sort t;
    fprintf(out, "%s", Z3_get_numeral_string(c, v));
    t = Z3_get_sort(c, v);
    fprintf(out, ":");
    display_sort(c, out, t);
    break;
  }
  case Z3_APP_AST: {
    unsigned i;
    Z3_app app = Z3_to_app(c, v);
    unsigned num_fields = Z3_get_app_num_args(c, app);
    Z3_func_decl d = Z3_get_app_decl(c, app);
    fprintf(out, "%s", Z3_func_decl_to_string(c, d));
    if (num_fields > 0) {
      fprintf(out, "[");
      for (i = 0; i < num_fields; i++) {
	if (i > 0) {
	  fprintf(out, ", ");
	}
	display_solver_term((SOLVER_CONTEXT) c, 
			    out, 
			    (SOLVER_TERM) Z3_get_app_arg(c, app, i));
      }
      fprintf(out, "]");
    }
    break;
  }
  case Z3_QUANTIFIER_AST: {
    fprintf(out, "quantifier");
    ;
  }
  default:
    fprintf(out, "#unknown");
  }
}