def test_scalar_models(self): scalar_t = yapi.yices_new_scalar_type(10) sc1 = define_const('sc1', scalar_t) sc2 = define_const('sc2', scalar_t) sc3 = define_const('sc3', scalar_t) assert_formula('(/= sc1 sc2)', self.ctx) assert_formula('(/= sc1 sc3)', self.ctx) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), yapi.STATUS_SAT) mdl = yapi.yices_get_model(self.ctx, 1) val1 = c_int32() val2 = c_int32() val3 = c_int32() yapi.yices_get_scalar_value(mdl, sc1, val1) yapi.yices_get_scalar_value(mdl, sc2, val2) yapi.yices_get_scalar_value(mdl, sc3, val3) self.assertEqual(val1.value, 9) self.assertEqual(val2.value, 8) self.assertEqual(val3.value, 8) yv1 = yapi.yval_t() ty1 = c_int32() self.assertEqual(yapi.yices_term_is_scalar(sc1), 1) sc1val = yapi.yices_get_value_as_term(mdl, sc1) self.assertEqual(yapi.yices_term_is_scalar(sc1val), 1) self.assertEqual(yapi.yices_get_value(mdl, sc1, yv1), 0) # yapi.YVAL_SCALAR self.assertEqual(yv1.node_tag, yapi.YVAL_SCALAR) yapi.yices_val_get_scalar(mdl, yv1, val1, ty1) self.assertEqual(val1.value, 9)
def get_value_from_function_yval(self, yval): function_size = yapi.yices_val_function_arity(self.model, yval) if function_size <= 0: return None ydefault = yapi.yval_t() ymapping = yapi.yval_vector_t() yapi.yices_init_yval_vector(ymapping) errcode = yapi.yices_val_expand_function(self.model, yval, ydefault, ymapping) if errcode == -1: yapi.yices_delete_yval_vector(ymapping) raise YicesException('yices_val_expand_function') default = self.get_value_from_yval(ydefault) mapping = [ self.get_value_from_yval(ymapping.data[i]) for i in range(0, ymapping.size) ] dict_map = {} for (src, tgt) in mapping: dict_map[src] = tgt yapi.yices_delete_yval_vector(ymapping) def retfun(src): if src in dict_map: return dict_map[src] return default return retfun
def get_value_from_mapping_yval(self, yval): mapping_size = yapi.yices_val_mapping_arity(self.model, yval) if mapping_size <= 0: return None ytgt = yapi.yval_t() ysrc = yapi.make_empty_yval_array(mapping_size) errcode = yapi.yices_val_expand_mapping(self.model, yval, ysrc, ytgt) if errcode == -1: raise YicesException('yices_val_expand_mapping') src = [self.get_value_from_yval(ysrc[i]) for i in range(0, mapping_size) ] tgt = self.get_value_from_yval(ytgt) return (tuple(src), tgt)
def test_algebraic_numbers(self): if not yapi.yices_has_mcsat(): return # Need a different context cfg = yapi.yices_new_config() yapi.yices_default_config_for_logic(cfg, "QF_NRA") yapi.yices_set_config(cfg, "mode", "one-shot") ctx = yapi.yices_new_context(cfg) x0 = define_const('x', self.real_t) assert_formula('(= (* x x) 2)', ctx) self.assertEqual(yapi.yices_check_context(ctx, None), yapi.STATUS_SAT) mdl = yapi.yices_get_model(ctx, 1) mdlstr = yapi.yices_model_to_string(mdl, 80, 100, 0) self.assertEqual(mdlstr, '(= x -1.414214)') alg1 = yapi.lp_algebraic_number_t() yapi.yices_get_algebraic_number_value(mdl, x0, alg1) yv1 = yapi.yval_t() yapi.yices_get_value(mdl, x0, yv1) alg2 = yapi.lp_algebraic_number_t() yapi.yices_val_get_algebraic_number(mdl, yv1, alg2)
def test_bv_models(self): bv_t = yapi.yices_bv_type(3) bv1 = define_const('bv1', bv_t) bv2 = define_const('bv2', bv_t) bv3 = define_const('bv3', bv_t) fmla1 = yapi.yices_parse_term('(= bv1 (bv-add bv2 bv3))') fmla2 = yapi.yices_parse_term('(bv-gt bv2 0b000)') fmla3 = yapi.yices_parse_term('(bv-gt bv3 0b000)') yapi.yices_assert_formula(self.ctx, fmla1) yapi.yices_assert_formulas(self.ctx, 3, yapi.make_term_array([fmla1, fmla2, fmla3])) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), 3) mdl1 = yapi.yices_get_model(self.ctx, 1) val1 = yapi.make_empty_int32_array(3) val2 = yapi.make_empty_int32_array(3) val3 = yapi.make_empty_int32_array(3) yapi.yices_get_bv_value(mdl1, bv1, val1) self.assertEqual(val1[0], 0) self.assertEqual(val1[1], 0) self.assertEqual(val1[2], 0) yapi.yices_get_bv_value(mdl1, bv2, val2) self.assertEqual(val2[0], 0) self.assertEqual(val2[1], 0) self.assertEqual(val2[2], 1) yapi.yices_get_bv_value(mdl1, bv3, val3) self.assertEqual(val3[0], 0) self.assertEqual(val3[1], 0) self.assertEqual(val3[2], 1) yv1 = yapi.yval_t() yapi.yices_get_value(mdl1, bv2, yv1) self.assertEqual(yapi.yices_val_bitsize(mdl1, yv1), 3) self.assertEqual(yv1.node_tag, yapi.YVAL_BV) yapi.yices_val_get_bv(mdl1, yv1, val1) self.assertEqual(yapi.yices_val_bitsize(mdl1, yv1), 3) self.assertEqual(val1[0], 0) self.assertEqual(val1[1], 0) self.assertEqual(val1[2], 1) yapi.yices_free_model(mdl1)
def test_tuple_models(self): tup_t = yapi.yices_tuple_type3(self.bool_t, self.real_t, self.int_t) t1 = define_const('t1', tup_t) assert_formula( '(ite (select t1 1) (< (select t1 2) (select t1 3)) (> (select t1 2) (select t1 3)))', self.ctx) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), 3) mdl = yapi.yices_get_model(self.ctx, 1) mdlstr = yapi.yices_model_to_string(mdl, 80, 100, 0) self.assertEqual(mdlstr, '(= t1 (mk-tuple false 1 0))') yv1 = yapi.yval_t() yapi.yices_get_value(mdl, t1, yv1) self.assertEqual(yv1.node_tag, yapi.YVAL_TUPLE) self.assertEqual(yapi.yices_val_tuple_arity(mdl, yv1), 3) yvarr = yapi.make_empty_yval_array(3) yapi.yices_val_expand_tuple(mdl, yv1, yvarr) self.assertEqual(yvarr[0].node_tag, yapi.YVAL_BOOL) bval1 = c_int32() ival1 = c_int32() yapi.yices_val_get_bool(mdl, yvarr[0], bval1) self.assertEqual(bval1.value, 0) yapi.yices_val_get_int32(mdl, yvarr[1], ival1) self.assertEqual(ival1.value, 1)
def test_bool_models(self): b1 = define_const('b1', self.bool_t) b2 = define_const('b2', self.bool_t) b3 = define_const('b3', self.bool_t) b_fml1 = yapi.yices_parse_term('(or b1 b2 b3)') yapi.yices_assert_formula(self.ctx, b_fml1) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), yapi.STATUS_SAT) b_mdl1 = yapi.yices_get_model(self.ctx, 1) self.assertNotEqual(b_mdl1, None) # init to -1 to make sure they get updated bval1 = c_int32() bval2 = c_int32() bval3 = c_int32() yapi.yices_get_bool_value(b_mdl1, b1, bval1) yapi.yices_get_bool_value(b_mdl1, b2, bval2) yapi.yices_get_bool_value(b_mdl1, b3, bval3) self.assertEqual(bval1.value, 0) self.assertEqual(bval2.value, 0) self.assertEqual(bval3.value, 1) b_fmla2 = yapi.yices_parse_term('(not b3)') yapi.yices_assert_formula(self.ctx, b_fmla2) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), yapi.STATUS_SAT) b_mdl1 = yapi.yices_get_model(self.ctx, 1) self.assertNotEqual(b_mdl1, None) yapi.yices_get_bool_value(b_mdl1, b1, bval1) yapi.yices_get_bool_value(b_mdl1, b2, bval2) yapi.yices_get_bool_value(b_mdl1, b3, bval3) self.assertEqual(bval1.value, 0) self.assertEqual(bval2.value, 1) self.assertEqual(bval3.value, 0) yv1 = yapi.yval_t() yapi.yices_get_value(b_mdl1, b1, yv1) self.assertEqual(yv1.node_tag, yapi.YVAL_BOOL) yapi.yices_val_get_bool(b_mdl1, yv1, bval1) self.assertEqual(bval1.value, 0)
def get_value(self, term): yval = yapi.yval_t() errcode = yapi.yices_get_value(self.model, term, yval) if errcode == -1: raise YicesException('yices_get_value') return self.get_value_from_yval(yval)
def test_yval_numeric_models(self): i1 = define_const('i1', self.int_t) define_const('i2', self.int_t) assert_formula('(> i1 3)', self.ctx) assert_formula('(< i2 i1)', self.ctx) self.assertEqual(yapi.yices_check_context(self.ctx, self.param), yapi.STATUS_SAT) mdl = yapi.yices_get_model(self.ctx, 1) yv1 = yapi.yval_t() yapi.yices_get_value(mdl, i1, yv1) self.assertEqual(yapi.yices_val_is_int32(mdl, yv1), 1) self.assertEqual(yapi.yices_val_is_int64(mdl, yv1), 1) self.assertEqual(yapi.yices_val_is_rational32(mdl, yv1), 1) self.assertEqual(yapi.yices_val_is_rational64(mdl, yv1), 1) self.assertEqual(yapi.yices_val_is_integer(mdl, yv1), 1) # The next four just return 0 since yval is not a bv, tuple, mapping, or function self.assertEqual(yapi.yices_val_bitsize(mdl, yv1), 0) # Note that the next three aren't real tests, since 0 is returned if the tag is wrong self.assertEqual(yapi.yices_val_tuple_arity(mdl, yv1), 0) self.assertEqual(yapi.yices_val_mapping_arity(mdl, yv1), 0) self.assertEqual(yapi.yices_val_function_arity(mdl, yv1), 0) bval1 = c_int32() #iam: 9/19/2018 with self.assertRaisesRegexp(YicesAPIException, 'invalid operation on yval'): #iam: 9/19/2018 yapi.yices_val_get_bool(mdl, yv1, bval1) errcode = yapi.yices_val_get_bool(mdl, yv1, bval1) error_string = yapi.yices_error_string() self.assertEqual(errcode, -1) self.assertEqual(error_string, 'invalid operation on yval') i32val1 = c_int32() yapi.yices_val_get_int32(mdl, yv1, i32val1) self.assertEqual(i32val1.value, 4) i64val1 = c_int64() yapi.yices_val_get_int64(mdl, yv1, i64val1) self.assertEqual(i64val1.value, 4) r32num1 = c_int32() r32den1 = c_uint32() yapi.yices_val_get_rational32(mdl, yv1, r32num1, r32den1) self.assertEqual(r32num1.value, 4) self.assertEqual(r32den1.value, 1) r64num1 = c_int64() r64den1 = c_uint64() yapi.yices_val_get_rational64(mdl, yv1, r64num1, r64den1) self.assertEqual(r64num1.value, 4) self.assertEqual(r64den1.value, 1) rdoub1 = c_double() yapi.yices_val_get_double(mdl, yv1, rdoub1) self.assertEqual(rdoub1.value, 4.0) gmpz1 = yapi.yices_new_mpz() yapi.yices_val_get_mpz(mdl, yv1, gmpz1) mpz1 = yapi.yices_mpz(gmpz1) self.assertEqual(yapi.yices_term_to_string(mpz1, 200, 10, 0), '4') gmpq1 = yapi.yices_new_mpq() yapi.yices_val_get_mpq(mdl, yv1, gmpq1) mpq1 = yapi.yices_mpq(gmpq1) self.assertEqual(yapi.yices_term_to_string(mpq1, 200, 10, 0), '4') #iam: 9/19/2018 with self.assertRaisesRegexp(YicesAPIException, 'invalid operation on yval'): #iam: 9/19/2018 yapi.yices_val_get_bv(mdl, yv1, bval1) errcode = yapi.yices_val_get_bv(mdl, yv1, bval1) error_string = yapi.yices_error_string() self.assertEqual(errcode, -1) self.assertEqual(error_string, 'invalid operation on yval')
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)