def collect_defined_terms(self): defined_terms = yapi.term_vector_t() yapi.yices_init_term_vector(defined_terms) #yapi.yices_model_collect_defined_terms(self.model, defined_terms) Yices.model_collect_defined_terms(self.model, defined_terms) retval = [] for i in range(0, defined_terms.size): retval.append(defined_terms.data[i]) yapi.yices_delete_term_vector(defined_terms) return retval
def get_unsat_core(self): retval = [] unsat_core = yapi.term_vector_t() yapi.yices_init_term_vector(unsat_core) errcode = Yices.get_unsat_core(self.context, unsat_core) if errcode == -1: raise YicesException('yices_get_unsat_core') for i in range(0, unsat_core.size): retval.append(unsat_core.data[i]) return retval
def generalize_model(self, term, elim_array, mode): var_array = yapi.make_term_array(elim_array) termv = yapi.term_vector_t() yapi.yices_init_term_vector(termv) errcode = yapi.yices_generalize_model(self.model, term, len(elim_array), var_array, mode, termv) if errcode == -1: yapi.yices_delete_term_vector(termv) raise YicesException('yices_generalize_model') retval = [] for i in range(0, termv.size): retval.append(termv.data[i]) yapi.yices_delete_term_vector(termv) return retval
def implicant_for_formula(self, term): termv = yapi.term_vector_t() yapi.yices_init_term_vector(termv) try: code = yapi.yices_implicant_for_formula(self.model, term, termv) retval = [] if code != -1: for i in range(0, termv.size): retval.append(termv.data[i]) yapi.yices_delete_term_vector(termv) return retval except yapi.YicesAPIException: yapi.yices_delete_term_vector(termv) raise YicesException('implicant_for_formula')
def support_for_term(self, term): """Returns the list of uninterpreted terms that fix the value of the given term in the model.""" termv = yapi.term_vector_t() yapi.yices_init_term_vector(termv) try: code = yapi.yices_model_term_support(self.model, term, termv) retval = [] if code != -1: for i in range(0, termv.size): retval.append(termv.data[i]) yapi.yices_delete_term_vector(termv) return retval except yapi.YicesAPIException: yapi.yices_delete_term_vector(termv) raise YicesException('support_for_term')
def implicant_for_formulas(self, term_array): tarray = yapi.make_term_array(term_array) termv = yapi.term_vector_t() yapi.yices_init_term_vector(termv) try: code = yapi.yices_implicant_for_formulas(self.model, len(term_array), tarray, termv) retval = [] if code != -1: for i in range(0, termv.size): retval.append(termv.data[i]) yapi.yices_delete_term_vector(termv) return retval except yapi.YicesAPIException as catastrophy: yapi.yices_delete_term_vector(termv) raise YicesException('implicant_for_formulas') from catastrophy
def support_for_terms(self, term_array): """Returns the list of uninterpreted terms that fix the value in the model of every term in the given array.""" tarray = yapi.make_term_array(term_array) termv = yapi.term_vector_t() try: yapi.yices_init_term_vector(termv) code = yapi.yices_model_term_array_support(self.model, len(term_array), tarray, termv) retval = [] if code != -1: for i in range(0, termv.size): retval.append(termv.data[i]) yapi.yices_delete_term_vector(termv) return retval except yapi.YicesAPIException as catastrophy: yapi.yices_delete_term_vector(termv) raise YicesException('support_for_terms') from catastrophy
def __iter__(self): """Overloading of iterator from Model. We iterate only on the variables defined in the assignment. """ term_v = yicespy.term_vector_t() yicespy.yices_init_term_vector(term_v) #todo here status = yicespy.yices_model_collect_defined_terms(self.yices_model, term_v) self._check_error(status) for d in term_v: try: pysmt_d = self.converter.back(d()) yield pysmt_d, self.get_value(pysmt_d) except UndefinedSymbolError: # avoids problems with symbols generated by z3 pass yicespy.yices_delete_term_vector(term_v)
def get_named_unsat_core(self): """After a call to solve() yielding UNSAT, returns the unsat core as a dict of names to formulae""" if self.options.unsat_cores_mode is None: raise SolverNotConfiguredForUnsatCoresError if self.last_result is not False: raise SolverStatusError("The last call to solve() was not" \ " unsatisfiable") if self.last_command != "solve": raise SolverStatusError("The solver status has been modified by a" \ " '%s' command after the last call to" \ " solve()" % self.last_command) assumptions = yicespy.term_vector_t() yicespy.yices_init_term_vector(assumptions) code = yicespy.yices_get_unsat_core(self.yices, assumptions) if code != 0: msg = yicespy.yices_error_string() raise InternalSolverError("Yices returned non-zero code upon unsat core extraction"\ ": %s (code: %s)" % \ (msg, code)) # for i in range(assumptions.size): # d = assumptions.data[i] # print(yicespy.yices_term_to_string(d, 200, 10, 0)) pysmt_assumptions = set(self.converter.back(assumptions.data[i]) for i in range(assumptions.size)) yicespy.yices_delete_term_vector(assumptions) res = {} n_ass_map = self._named_assertions_map() cnt = 0 for key in pysmt_assumptions: if key in n_ass_map: (name, formula) = n_ass_map[key] if name is None: name = "_a_%d" % cnt cnt += 1 res[name] = formula return res
def test_implicant(self): i1 = define_const('i1', self.int_t) assert_formula('(and (> i1 2) (< i1 8) (/= i1 4))', self.ctx) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), yapi.STATUS_SAT) mdl = yapi.yices_get_model(self.ctx, 1) mdlstr = yapi.yices_model_to_string(mdl, 80, 100, 0) self.assertEqual(mdlstr, '(= i1 7)') fml = yapi.yices_parse_term('(>= i1 3)') tvec = yapi.term_vector_t() yapi.yices_init_term_vector(tvec) yapi.yices_implicant_for_formula(mdl, fml, tvec) self.assertEqual(tvec.size, 1) implstr = yapi.yices_term_to_string(tvec.data[0], 200, 10, 0) self.assertEqual(implstr, '(>= (+ -3 i1) 0)') fml2 = yapi.yices_parse_term('(<= i1 9)') fmls = yapi.make_term_array([fml, fml2]) tvec2 = yapi.term_vector_t() yapi.yices_init_term_vector(tvec2) yapi.yices_implicant_for_formulas(mdl, 2, fmls, tvec2) self.assertEqual(tvec2.size, 2) implstr2 = yapi.yices_term_to_string(tvec2.data[0], 200, 10, 0) self.assertEqual(implstr2, '(>= (+ -3 i1) 0)') implstr3 = yapi.yices_term_to_string(tvec2.data[1], 200, 10, 0) self.assertEqual(implstr3, '(>= (+ 9 (* -1 i1)) 0)') tvec3 = yapi.term_vector_t() yapi.yices_init_term_vector(tvec3) a_arr = yapi.term_t(i1) yapi.yices_generalize_model_array(mdl, 2, fmls, 1, a_arr, 0, tvec3)
def test_term_vector(self): term_v = term_vector_t() yices_init_term_vector(term_v) yices_delete_term_vector(term_v) yices_reset_term_vector(term_v)
def test_function_models(self): funtype = yapi.yices_function_type3(self.int_t, self.bool_t, self.real_t, self.real_t) ftystr = yapi.yices_type_to_string(funtype, 100, 80, 0) yapi.yices_pp_type_fd(1, funtype, 100, 80, 0) self.assertEqual(ftystr, '(-> int bool real real)') fun1 = define_const('fun1', funtype) define_const('b1', self.bool_t) i1 = define_const('i1', self.int_t) r1 = define_const('r1', self.real_t) assert_formula( '(> (fun1 i1 b1 r1) (fun1 (+ i1 1) (not b1) (- r1 i1)))', self.ctx) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), yapi.STATUS_SAT) mdl = yapi.yices_get_model(self.ctx, 1) mdlstr = yapi.yices_model_to_string(mdl, 80, 100, 0) self.assertEqual( mdlstr, '(= b1 false)\n(= i1 1463)\n(= r1 -579)\n(function fun1\n (type (-> int bool real real))\n (= (fun1 1463 false -579) 1)\n (= (fun1 1464 true -2042) 0)\n (default 2))' ) yv1 = yapi.yval_t() yapi.yices_get_value(mdl, fun1, yv1) self.assertEqual(yv1.node_tag, yapi.YVAL_FUNCTION) self.assertEqual(yapi.yices_val_function_arity(mdl, yv1), 3) def1 = yapi.yval_t() vec1 = yapi.yval_vector_t() yapi.yices_init_yval_vector(vec1) yapi.yices_val_expand_function(mdl, yv1, def1, vec1) self.assertEqual(def1.node_tag, yapi.YVAL_RATIONAL) i32val1 = c_int32() yapi.yices_val_get_int32(mdl, def1, i32val1) self.assertEqual(i32val1.value, 2) self.assertEqual(vec1.size, 2) map1 = vec1.data[0] map2 = vec1.data[1] self.assertEqual(map1.node_tag, yapi.YVAL_MAPPING) self.assertEqual(map2.node_tag, yapi.YVAL_MAPPING) self.assertEqual(yapi.yices_val_mapping_arity(mdl, map1), 3) self.assertEqual(yapi.yices_val_mapping_arity(mdl, map2), 3) # First mapping args1 = yapi.make_empty_yval_array(3) yval1 = yapi.yval_t() yapi.yices_val_expand_mapping(mdl, map1, args1, yval1) self.assertEqual(yval1.node_tag, yapi.YVAL_RATIONAL) self.assertEqual(yapi.yices_val_is_int32(mdl, yval1), 1) val1 = c_int32() yapi.yices_val_get_int32(mdl, yval1, val1) self.assertEqual(val1.value, 1) self.assertEqual(args1[0].node_tag, yapi.YVAL_RATIONAL) self.assertEqual(yapi.yices_val_is_int32(mdl, args1[0]), 1) m1arg1 = c_int32() yapi.yices_val_get_int32(mdl, args1[0], m1arg1) self.assertEqual(m1arg1.value, 1463) self.assertEqual(args1[1].node_tag, yapi.YVAL_BOOL) m1arg2 = c_int() yapi.yices_val_get_bool(mdl, args1[1], m1arg2) self.assertEqual(m1arg2.value, 0) m1arg3 = c_int32() yapi.yices_val_get_int32(mdl, args1[2], m1arg3) self.assertEqual(m1arg3.value, -579) # Second mapping args2 = yapi.make_empty_yval_array(3) yval2 = yapi.yval_t() yapi.yices_val_expand_mapping(mdl, map2, args2, yval2) self.assertEqual(yval2.node_tag, yapi.YVAL_RATIONAL) self.assertEqual(yapi.yices_val_is_int32(mdl, yval2), 1) val2 = c_int32() yapi.yices_val_get_int32(mdl, yval2, val2) self.assertEqual(val2.value, 0) self.assertEqual(args2[0].node_tag, yapi.YVAL_RATIONAL) self.assertEqual(yapi.yices_val_is_int32(mdl, args2[0]), 1) m2arg2 = c_int32() yapi.yices_val_get_int32(mdl, args2[0], m2arg2) self.assertEqual(m2arg2.value, 1464) self.assertEqual(args2[1].node_tag, yapi.YVAL_BOOL) m2arg2 = c_int() yapi.yices_val_get_bool(mdl, args2[1], m2arg2) self.assertEqual(m2arg2.value, 1) m2arg3 = c_int32() yapi.yices_val_get_int32(mdl, args2[2], m2arg3) self.assertEqual(m2arg3.value, -2042) fmla = yapi.yices_parse_term('(> i1 r1)') self.assertEqual(yapi.yices_formula_true_in_model(mdl, fmla), 1) a_arr = yapi.make_term_array([i1, fmla, r1]) b_arr = yapi.make_empty_term_array(3) yapi.yices_term_array_value(mdl, 3, a_arr, b_arr) self.assertEqual(b_arr[0], yapi.yices_int32(1463)) self.assertEqual(b_arr[1], yapi.yices_true()) self.assertEqual(b_arr[2], yapi.yices_int32(-579)) yapi.yices_pp_term_array_fd(1, 3, b_arr, 100, 10, 0, 0) tvec3 = yapi.term_vector_t() yapi.yices_init_term_vector(tvec3) yapi.yices_generalize_model(mdl, fmla, 1, a_arr, 0, tvec3) yapi.yices_delete_term_vector(tvec3)