Skip to content

Commit 6f0bdf1

Browse files
committed
Nearing a good way point.
1 parent d2bf57d commit 6f0bdf1

File tree

6 files changed

+130
-14
lines changed

6 files changed

+130
-14
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
.cache
1+
build
2+
dist
3+
yices.egg-info
4+
.pytest_cache
5+
**/*.cache
6+
**/*.pyc

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ install:
4242
pip install
4343

4444
clean:
45-
rm -rf *.pyc *~ __pycache__ */*.pyc */*~ */__pycache__ */*/*.pyc */*/*~ */*/__pycache__
45+
rm -rf *.pyc *~ __pycache__ */*.pyc */*~ */__pycache__ */*/*.pyc */*/*~ */*/__pycache__ examples/mcsat/mcsat
4646

4747

4848

TODO.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

2-
[] 1. Move exceptions out of yices_api and into yices. yices_api should not throw exceptions.
2+
[X] 1. Move exceptions out of yices_api and into yices. yices_api should not throw exceptions.
33
yices should throw them instead.
44

55
[] 2. doc strings in yices to reflect this. do the yices_api doc strings need TLC?
66

77
[] 3. Solve the gmp and libpoly conundrums, maybe ask Dejan?
88

9-
[] 4. Add tests for the yices package.
9+
[X] 4. Add tests for the yices package.
1010

11-
[] 5. Fix the `lint_all` complaints.
11+
[X] 5. Fix the `lint_all` complaints.
1212

1313
[] 6. Investigate the new type annotation mechanism.
1414

examples/functions.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#! /usr/bin/env python
2+
3+
4+
from yices import *
5+
6+
7+
def define_const(name, ytype, defn=None):
8+
'''Tries to emulate yices define_term
9+
(see eval_define_term in yices2/src/parser_utils/term_stack2)
10+
'''
11+
if defn is None:
12+
term = Terms.new_uninterpreted_term(ytype)
13+
Terms.set_name(term, name)
14+
return term
15+
# Have a defn
16+
if isinstance(defn, basestring):
17+
term = Terms.parse_term(defn)
18+
else:
19+
term = defn
20+
term_type = Terms.type_of_term(term)
21+
if not Types.is_subtype(term_type, ytype):
22+
raise YicesException(msg='incompatible sort in definition')
23+
Terms.set_name(term, name)
24+
return term
25+
26+
def assert_formula(formula, ctx):
27+
if isinstance(formula, basestring):
28+
formula = Terms.parse_term(formula)
29+
ctx.assert_formula(formula)
30+
31+
32+
33+
cfg = Config()
34+
ctx = Context(cfg)
35+
param = Parameters()
36+
param.default_params_for_context(ctx)
37+
global bool_t, int_t, real_t
38+
bool_t = Types.bool_type()
39+
int_t = Types.int_type()
40+
real_t = Types.real_type()
41+
42+
43+
funtype = Types.new_function_type([int_t, bool_t, real_t], real_t)
44+
ftystr = Types.to_string(funtype, 100, 80, 0)
45+
Types.print_to_fd(1, funtype, 100, 80, 0)
46+
#assertEqual(ftystr, '(-> int bool real real)')
47+
fun1 = define_const('fun1', funtype)
48+
b1 = define_const('b1', bool_t)
49+
i1 = define_const('i1', int_t)
50+
r1 = define_const('r1', real_t)
51+
assert_formula('(> (fun1 i1 b1 r1) (fun1 (+ i1 1) (not b1) (- r1 i1)))', ctx)
52+
print(ctx.check_context(param) == Status.SAT)
53+
mdl = Model.from_context(ctx, 1)
54+
mdlstr = mdl.to_string(80, 100, 0)
55+
print(mdlstr)
56+
print('(= 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))')
57+
fun1val = mdl.get_value(fun1)
58+
print(fun1val((1463, False, -579)))
59+
print(fun1val((1464, True, -2042)))
60+
print(fun1val((1462, True, -2041)))
61+
62+
63+
64+
cfg.dispose()
65+
ctx.dispose()
66+
param.dispose()
67+
Yices.exit()

yices/Terms.py

+50-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ctypes
12
import yices_api as yapi
23

34
from .YicesException import YicesException
@@ -994,15 +995,57 @@ def proj_arg(term):
994995
return retval
995996

996997

997-
#FIXME finish these
998-
#yices_bool_const_value(term)
999-
#yices_bv_const_value(term)
1000-
#yices_scalar_const_value(term)
998+
@staticmethod
999+
def bool_const_value(term):
1000+
value = ctypes.c_int32()
1001+
errcode = yapi.yices_bool_const_value(term, value)
1002+
if errcode == 0:
1003+
return value.value
1004+
raise YicesException('yices_bool_const_value')
1005+
1006+
@staticmethod
1007+
def bv_const_value(term):
1008+
bitsize = Terms.bitsize(term)
1009+
bvarray = yapi.make_empty_int32_array(bitsize)
1010+
errcode = yapi.yices_bv_const_value(term, bvarray)
1011+
if errcode == 0:
1012+
return [ bvarray[i] for i in range(0, bitsize) ]
1013+
raise YicesException('yices_bool_const_value')
1014+
1015+
@staticmethod
1016+
def scalar_const_value(term):
1017+
value = ctypes.c_int32()
1018+
errcode = yapi.yices_scalar_const_value(term, value)
1019+
if errcode == 0:
1020+
return value.value
1021+
raise YicesException('yices_scalar_const_value')
1022+
1023+
@staticmethod
1024+
def bvsum_component(term, i):
1025+
bitsize = Terms.bitsize(term)
1026+
if i >= bitsize:
1027+
raise YicesException('bvsum_component', 'index {0} too big >= bitsize {1}'.format(i, bitsize))
1028+
bvarray = yapi.make_empty_int32_array(bitsize)
1029+
termv = ctypes.c_int32()
1030+
errcode = yapi.yices_bvsum_component(term, i, bvarray, termv)
1031+
if errcode == 0:
1032+
return ([ bvarray[i] for i in range(0, bitsize) ], termv.value)
1033+
raise YicesException('yices_bvsum_component')
1034+
1035+
@staticmethod
1036+
def product_component(term, i):
1037+
expv = ctypes.c_uint32()
1038+
termv = ctypes.c_int32()
1039+
errcode = yapi.yices_bvsum_component(term, i, termv, expv)
1040+
if errcode == 0:
1041+
return (termv.value, expv.value)
1042+
raise YicesException('yices_bvsum_component')
1043+
1044+
1045+
1046+
# TBD: gmp issues
10011047
#yices_rational_const_value
10021048
#yices_sum_component
1003-
#yices_bvsum_component(term)
1004-
#yices_product_component(term)
1005-
10061049

10071050
# names
10081051

yices_api.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ def yices_python_info_main():
9595
# 1.0.6 - 2.5.3 - 9/28/2017 - LD_LIBRARY_PATH hackery, III. #
9696
# 1.0.7 - 2.5.4 - 9/29/2017 - patch level version bump for PPA goodness #
9797
# 1.0.8 - 2.5.4 - 10/4/2017 - improving the user experience (less SIGSEGVs) #
98-
# 1.1.0 - 2.6.0 - 10/8/2018 - major changes (addition of pythonesque api) #
98+
# 1.1.0 - 2.6.0 - 10/8/2018 - major changes (addition of pythonesque API) #
99+
# 1.1.1 - 2.6.0 - 10/9/2018 - tweaks and finish the pythonesque API #
99100
################################################################################################
100101

101102
#
102103
# when the dust settles we can synch this with the library, but
103104
# while the bindings are moving so fast we should keep them separate.
104105
#
105106
#
106-
yices_python_version = '1.1.0'
107+
yices_python_version = '1.1.1'
107108

108109
#
109110
# 1.0.1 needs yices_has_mcsat

0 commit comments

Comments
 (0)