/* * Check satisfiability and get a model * - ctx = the context * - parameters = heuristic settings (if parameters is NULL, the defaults are used) * - var = array of n uninterpreted terms * - n = size of array evar and value * Output parameters; * - value = array of n terms (to receive the value of each var) * - model = to export the model (if model is NULL, nothing is exported) * * The return code is as in check_context: * 1) if code = STATUS_SAT then the context is satisfiable * and a model is stored in value[0 ... n-1] * - value[i] = a constant term mapped to evar[i] in the model * 2) code = STATUS_UNSAT: not satisfiable * * 3) other codes report an error of some kind or STATUS_INTTERRUPT */ static smt_status_t satisfy_context(context_t *ctx, const param_t *parameters, term_t *var, uint32_t n, term_t *value, model_t **model) { smt_status_t stat; model_t *mdl; int32_t code; assert(context_status(ctx) == STATUS_IDLE); stat = check_context(ctx, parameters); switch (stat) { case STATUS_SAT: case STATUS_UNKNOWN: mdl = yices_get_model(ctx, true); code = yices_term_array_value(mdl, n, var, value); if (model != NULL) { *model = mdl; } else { yices_free_model(mdl); } if (code < 0) { // can't convert the model to constant terms stat = STATUS_ERROR; } break; default: // can't build a model break; } return stat; }
int example2_main(void) { type_t real; term_t t, a; int32_t code; context_t *ctx; model_t *mdl; /* * Global initialization: this must called first. */ yices_init(); /* * Create an uninterpreted term of type real and call it "x" */ real = yices_real_type(); t = yices_new_uninterpreted_term(real); code = yices_set_term_name(t, "x"); /* * If code is negative, something went wrong */ if (code < 0) { printf("Error in yices_set_term\n"); yices_print_error(stdout); fflush(stdout); goto done; } /* * Create the atom (x > 0) */ a = yices_arith_gt0_atom(t); /* * Print the atom: */ printf("Atom: "); yices_pp_term(stdout, a, 120, 2, 6); /* * Check that (x > 0) is satisfiable and get a model * - create a context * - assert atom 'a' in this context * - check that the context is satisfiable */ ctx = yices_new_context(NULL); // NULL means use the default configuration code = yices_assert_formula(ctx, a); if (code < 0) { printf("Assert failed: "); yices_print_error(stdout); fflush(stdout); goto done; } switch (yices_check_context(ctx, NULL)) { // NULL means default heuristics case STATUS_SAT: // build the model and print it printf("Satisfiable\n"); mdl = yices_get_model(ctx, true); code = yices_pp_model(stdout, mdl, 120, 100, 0); if (code < 0) { printf("Print model failed: "); yices_print_error(stdout); fflush(stdout); goto done; } // cleanup: delete the model yices_free_model(mdl); break; case STATUS_UNSAT: printf("Unsatisfiable\n"); break; case STATUS_UNKNOWN: printf("Status is unknown\n"); break; case STATUS_IDLE: case STATUS_SEARCHING: case STATUS_INTERRUPTED: case STATUS_ERROR: // these codes should not be returned printf("Bug: unexpected status returned\n"); break; } /* * Delete the context: this is optional since yices_exit * would do it anyway. */ yices_free_context(ctx); done: /* * Global cleanup: free all memory used by Yices */ yices_exit(); return 0; }