-
Notifications
You must be signed in to change notification settings - Fork 5
/
yices_api.py
5727 lines (4872 loc) · 200 KB
/
yices_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
The Yices2 Python API.
Basically implements all the external symbols of Yices2 - see yices.h
iam: 9/26/2017 I decided that it was better to preserve the names
between the API and the python bindings, than any other consideration.
This will be annoying if one uses this idiom:
import yices
but not if one uses this idiom:
from yices import *
there is also the middle ground of
import yices as y
iam: need to isolate and load the gmp stuff into a separate language binding.
bd: take care of pointer() vs. byref(). Be consistent about it.
update the docstrings: some functions return bitvector values
in an array of integers.
iam: we should beef up the Yices exception so that it contains a copy of the
error report.
iam: 9/19/2018 this module has been renamed to yices_api
"""
from __future__ import with_statement
from __future__ import print_function
import os
import sys
from functools import wraps
from ctypes import (
Array,
byref,
CDLL,
cast,
c_char_p,
c_double,
c_int,
c_uint,
c_int32,
c_uint32,
c_int64,
c_uint64,
c_ulong,
c_size_t,
c_void_p,
pointer,
POINTER,
Structure
)
from ctypes.util import find_library
def yices_python_info_main():
"""The only console entry point; currently just used for information."""
loadYices()
sys.stdout.write('Python Yices Bindings. Version {0}\n'.format(yices_python_version))
sys.stdout.write('Yices library loaded from {0}\n'.format(libyicespath))
msg = 'Version: {0}\nArchitecture: {1}\nBuild mode: {2}\nBuild date: {3}\nMCSat support: {4}\nThread safe: {5}\n'
mcsat = 'yes' if yices_has_mcsat() else 'no'
thread_safe = 'yes' if yices_is_thread_safe() else 'no'
sys.stdout.write(msg.format(yices_version, yices_build_arch, yices_build_mode, yices_build_date, mcsat, thread_safe))
################################################################################################
# Feeping Creaturism: #
# #
# this is the all important version number used by pip. #
# #
# #
################################################################################################
# #
# Version History: #
# #
# pip - lib - release date - notes #
# #
# 1.0.0 - 2.5.3 - 9/11/2017 - birth #
# 1.0.1 - 2.5.3 - 9/27/2017 - uniform API version #
# 1.0.2 - 2.5.3 - 9/27/2017 - library version check #
# 1.0.3 - 2.5.3 - 9/28/2017 - STATUS_SAT et al + Linux install fix. #
# 1.0.4 - 2.5.3 - 9/28/2017 - LD_LIBRARY_PATH hackery. #
# 1.0.5 - 2.5.3 - 9/28/2017 - LD_LIBRARY_PATH hackery, II. #
# 1.0.6 - 2.5.3 - 9/28/2017 - LD_LIBRARY_PATH hackery, III. #
# 1.0.7 - 2.5.4 - 9/29/2017 - patch level version bump for PPA goodness #
# 1.0.8 - 2.5.4 - 10/4/2017 - improving the user experience (less SIGSEGVs) #
# 1.1.0 - 2.6.0 - 10/8/2018 - major changes (addition of pythonesque API) #
# 1.1.1 - 2.6.0 - 10/9/2018 - tweaks and finish the pythonesque API #
# 1.1.2 - 2.6.0 - 11/26/2018 - ctype string conversions for python 3 #
# 1.1.3 - 2.6.2 - 05/20/2020 - new API routines (not sure where 2.6.1 was?) #
# 1.1.4 - 2.6.2 - 07/10/2020 - Profiling stuff #
# 1.1.5 - 2.6.4 - 12/06/2021 - new 2.6.4 API routines #
# 1.1.6 - 2.6.5 - 12/16/2024 - new 2.6.5 API routines #
################################################################################################
#
# when the dust settles we can synch this with the library, but
# while the bindings are moving so fast we should keep them separate.
#
#
yices_python_version = '1.1.6'
#
# 1.0.1 needs yices_has_mcsat
# 1.0.1-7 needs the _fd api additions that appear in 2.5.4
# 1.1.0 hooks into the new unsat core stuff
yices_recommended_version = '2.6.5'
#iam: 10/4/2017 try to make the user experience a little more pythony.
#BD suggests doing this in the loadYices routine; he might be right
#but this is more nannyish because the dolt could still call yices_exit
#then go on to try and do stuff.
__yices_library_inited__ = False
class YicesAPIException(Exception):
"""Base class for exceptions from Yices API."""
#iam: 9/29/2018 turn this on to get entry and exit log messages on stderr.
YICES_API_TRACE = False
#iam: 9/19/2018 only throw an exception if the library is not inited.
def catch_error(errval):
"""catches any error."""
def decorator(yices_fun):
"""yices api function decorator."""
@wraps(yices_fun)
def wrapper(*args, **kwargs):
"""yices api function wrapper."""
errstr = "You must initialize by calling yices_init()"
if YICES_API_TRACE:
sys.stderr.write('\nEntering {0}.\n'.format(yices_fun.__name__))
result = yices_fun(*args, **kwargs) if __yices_library_inited__ else None
if YICES_API_TRACE:
sys.stderr.write('\nExiting {0} with {1} (errval is {2}).\n'.format(yices_fun.__name__, result, errval))
if not __yices_library_inited__:
raise YicesAPIException(errstr)
#iam: 9/19/2018 if result == errval and yices_error_code() != 0L:
#iam: 9/19/2018 errstr = yices_error_string()
#iam: 9/19/2018 yices_clear_error()
#iam: 9/19/2018 raise YicesAPIException(errstr)
return result
return wrapper
return decorator
def catch_uninitialized():
"""catches a call to an uninitialized yices library (catch_error also does this)."""
def decorator(yices_fun):
"""yices api function decorator."""
@wraps(yices_fun)
def wrapper(*args, **kwargs):
"""yices api function wrapper."""
errstr = "You must initialize by calling yices_init()"
result = yices_fun(*args, **kwargs) if __yices_library_inited__ else None
if not __yices_library_inited__:
raise YicesAPIException(errstr)
return result
return wrapper
return decorator
#########################################
# #
# Start of LOADING ZONE #
# #
#########################################
def guess_library_name(package_name):
"""attempts to guess the name of the yices2 library."""
lib_basename = 'lib' + package_name
extension = '.so'
if sys.platform == 'win32':
extension = '.dll'
elif sys.platform == 'cygwin':
lib_basename = 'cyg' + package_name
extension = '.dll'
elif sys.platform == 'darwin':
extension = '.dylib'
return '{0}{1}'.format(lib_basename, extension)
#
# N.B. find_library is not super reliable
#
libyicespath = find_library("yices")
libyices = None
if libyicespath is None:
libyicespath = guess_library_name('yices')
def _loadYicesFromPath(path, library):
global libyices
try:
where = os.path.join(path, library) if path is not None else library
libyices = CDLL(where)
return True
# iam: if someone can nail down what exceptions CDLL can raise we could be more specific
# but the documentation is pretty vague.
except Exception as exception: # pylint: disable=broad-except
sys.stderr.write('\nCDLL({0}) raised {1}.\n'.format(where, exception))
return False
def loadYices():
"""attempts to load the yices library, relying on CDLL, and using /usr/local/lib as a backup plan."""
global libyicespath
global libyices
error_msg = "Yices dynamic library not found."
if _loadYicesFromPath(None, libyicespath):
return
if _loadYicesFromPath('/usr/local/lib', libyicespath):
return
# else we failed
raise YicesAPIException(error_msg)
loadYices()
###########################
# Utilities
###########################
def str2bytes(s):
"""converts its argument into a bytes object using the default utf-8 encoding, or None if given None."""
return str(s).encode() if s is not None else None
def bytes2str(b):
"""converts the bytes into a string using utf-8, or None if given None."""
return b.decode() if b is not None else None
###########################
# VERSION AND RELATIVES #
###########################
# const char *yices_version
yices_version = c_char_p.in_dll(libyices, "yices_version").value
yices_version = yices_version.decode("utf-8")
"""libyices version as in '2.5.3'"""
# const char *yices_build_arch
yices_build_arch = c_char_p.in_dll(libyices, "yices_build_arch").value
yices_build_arch = yices_build_arch.decode("utf-8")
"""libyices build architecture as in 'x86_64-pc-linux-gnu'"""
# const char *yices_build_mode
yices_build_mode = c_char_p.in_dll(libyices, "yices_build_mode").value
yices_build_mode = yices_build_mode.decode("utf-8")
"""libyices build mode (typically either 'release' or 'debug'"""
# const char *yices_build_date
yices_build_date = c_char_p.in_dll(libyices, "yices_build_date").value
yices_build_date = yices_build_date.decode("utf-8")
"""libyices build date as in '2017-09-08'"""
# int32_t yices_has_mcsat(void)
libyices.yices_has_mcsat.restype = c_int32
def yices_has_mcsat():
"""Returns 1 if the yices library has mcsat support, 0 otherwise."""
return libyices.yices_has_mcsat()
# new in 2.6.2
# int32_t yices_is_thread_safe(void)
libyices.yices_has_mcsat.restype = c_int32
def yices_is_thread_safe():
"""Returns 1 if the yices library has been compiled for thread safety, 0 otherwise."""
return libyices.yices_is_thread_safe()
def checkYices():
"""Checks that the library is not too stale to work with these bindings."""
def _versionCheck():
(lv_major, lv_minor, lv_revision) = [int(x) for x in yices_version.split('.')]
(rv_major, rv_minor, rv_revision) = [int(x) for x in yices_recommended_version.split('.')]
if lv_major < rv_major:
return False
if lv_major == rv_major and lv_minor < rv_minor:
return False
if lv_major == rv_major and lv_minor == rv_minor and lv_revision < rv_revision:
return False
return True
complaint = """
This Python package requires Yices C library version {0} or better.
Your version is {1}.
Please upgrade: http://yices.csl.sri.com/.
"""
if not _versionCheck():
raise YicesAPIException(complaint.format(yices_recommended_version, yices_version))
checkYices()
#########################################
# #
# End of LOADING ZONE #
# #
#########################################
#we are lazy about attempting to load gmp. The user must try
#to call a routine that needs gmp, otherwise we do not load it.
libgmppath = find_library('gmp')
if libgmppath is None:
libgmppath = guess_library_name('gmp')
libgmp = None
libgmpFailed = None
def hasGMP():
"""Returns True in the GMP library has been loaded and is ready to use, False otherwise."""
global libgmpFailed, libgmp, libgmppath
if libgmpFailed is True:
return False
if libgmp is not None:
return True
if libgmppath is None:
libgmpFailed = True
return False
libgmp = CDLL(libgmppath)
if libgmp is not None:
sys.stderr.write('\nLoading gmp library from {0}.\n'.format(libgmppath))
libgmpFailed = False
return True
libgmpFailed = True
return False
# From yices_limits.h
# int32_t max (2^31 - 1)
MAX_INT32_SIZE = 2147483647
#########################################
# #
# Yices2's fundamental types. #
# From yices_types.h #
# #
#########################################
error_code_t = c_uint32 # an enum type, in yices_types.h
term_t = c_int32
type_t = c_int32
term_constructor_t = c_uint # an enum type, in yices_types.h
ctx_config_t = c_void_p # an opaque type, in yices_types.h
context_t = c_void_p # an opaque type, in yices_types.h
smt_status_t = c_uint # an enum type, in yices_types.h
param_t = c_void_p # an opaque type, in yices_types.h
model_t = c_void_p # an opaque type, in yices_types.h
yval_tag_t = c_uint
yices_gen_mode_t = c_void_p
class error_report_t(Structure):
"""The cause of an API error is stored in an error_report structure."""
_fields_ = [("code", error_code_t), # 8
("line", c_uint32), # 4
("column", c_uint32), # 4
("term1", term_t), # 4
("type1", type_t), # 4
("term2", term_t), # 4
("type2", type_t), # 4
("badval", c_int64)] # 8
class yval_t(Structure):
"""The type of a node descriptor."""
_fields_ = [("node_id", c_int32),
("node_tag", yval_tag_t)]
class term_vector_t(Structure):
"""A resizable array of terms."""
_fields_ = [("capacity", c_uint32),
("size", c_uint32),
("data", POINTER(term_t))]
class type_vector_t(Structure):
"""A resizable array of types."""
_fields_ = [("capacity", c_uint32),
("size", c_uint32),
("data", POINTER(type_t))]
class yval_vector_t(Structure):
"""A resizable array of node descriptors."""
_fields_ = [("capacity", c_uint32),
("size", c_uint32),
("data", POINTER(yval_t))]
class yval_array(Array):
"""An array of node descriptors."""
_type_ = yval_t
_length_ = 2
# new in 2.6.4
class interpolation_context_t(Structure):
"""Used in the API for checking satisfiability and computing an interpolant."""
_fields_ = [("ctx_A", context_t),
("ctx_B", context_t),
("interpolant", term_t),
("model", model_t)]
# gmp types
class mpz_t(Structure):
"""Replica of the GMP mpz_t struct, it must be kept upto date with gmp.h."""
_fields_ = [("_mp_alloc", c_int),
("_mp_size", c_int),
("_mp_d", POINTER(c_void_p))]
class mpq_t(Structure):
"""Replica of the GMP mpq_t struct, it must be kept upto date with gmp.h."""
_fields_ = [("_mp_num", mpz_t),
("_mp_den", mpz_t)]
# From polylib poly.h
# class lp_upolynomial_t(Structure):
# _fields_ = [("K", POINTER(lp_int_ring_t)),
# ("size", size_t),
# ("monomials", POINTER(ulp_monomial_t))]
lp_integer_t = mpz_t
class lp_dyadic_rational_t(Structure):
"""Replica of the Libpoly lp_dyadic_rational_t struct, it must be kept upto date with polynomial.h."""
_fields_ = [("a", lp_integer_t),
("n", c_ulong)]
class lp_dyadic_interval_t(Structure):
"""Replica of the Libpoly lp_dyadic_interval_t struct, it must be kept upto date with polynomial.h."""
_fields_ = [("a_open", c_size_t),
("b_open", c_size_t),
("is_point", c_size_t),
("a", lp_dyadic_rational_t),
("b", lp_dyadic_rational_t)]
class lp_algebraic_number_t(Structure):
"""Replica of the Libpoly lp_algebraic_number_t struct, it must be kept upto date with polynomial.h."""
_fields_ = [("f", c_void_p), #("f", POINTER(lp_upolynomial_t)),
("I", lp_dyadic_interval_t),
("sgn_at_a", c_int),
("sgn_at_b", c_int)]
#from yices_types.h:
#yval_tag_t: yval node_tag values
YVAL_UNKNOWN = 0
YVAL_BOOL = 1
YVAL_RATIONAL = 2
YVAL_ALGEBRAIC = 3
YVAL_BV = 4
YVAL_SCALAR = 5
YVAL_TUPLE = 6
YVAL_FUNCTION = 7
YVAL_MAPPING = 8
#smt_status_t
STATUS_IDLE = 0
STATUS_SEARCHING = 1
STATUS_UNKNOWN = 2
STATUS_SAT = 3
STATUS_UNSAT = 4
STATUS_INTERRUPTED = 5
STATUS_ERROR = 6
#term_constructor_t
YICES_CONSTRUCTOR_ERROR = -1
YICES_BOOL_CONSTANT = 0
YICES_ARITH_CONSTANT = 1
YICES_BV_CONSTANT = 2
YICES_SCALAR_CONSTANT = 3
YICES_VARIABLE = 4
YICES_UNINTERPRETED_TERM = 5
YICES_ITE_TERM = 6
YICES_APP_TERM = 7
YICES_UPDATE_TERM = 8
YICES_TUPLE_TERM = 9
YICES_EQ_TERM = 10
YICES_DISTINCT_TERM = 11
YICES_FORALL_TERM = 12
YICES_LAMBDA_TERM = 13
YICES_NOT_TERM = 14
YICES_OR_TERM = 15
YICES_XOR_TERM = 16
YICES_BV_ARRAY = 17
YICES_BV_DIV = 18
YICES_BV_REM = 19
YICES_BV_SDIV = 20
YICES_BV_SREM = 21
YICES_BV_SMOD = 22
YICES_BV_SHL = 23
YICES_BV_LSHR = 24
YICES_BV_ASHR = 25
YICES_BV_GE_ATOM = 26
YICES_BV_SGE_ATOM = 27
YICES_ARITH_GE_ATOM = 28
YICES_ARITH_ROOT_ATOM = 29
YICES_ABS = 30
YICES_CEIL = 31
YICES_FLOOR = 32
YICES_RDIV = 33
YICES_IDIV = 34
YICES_IMOD = 35
YICES_IS_INT_ATOM = 36
YICES_DIVIDES_ATOM = 37
YICES_SELECT_TERM = 38
YICES_BIT_TERM = 39
YICES_BV_SUM = 40
YICES_ARITH_SUM = 41
YICES_POWER_PRODUCT = 42
#yices_gen_mode_t
YICES_GEN_DEFAULT = 0
YICES_GEN_BY_SUBST = 1
YICES_GEN_BY_PROJ = 2
# From yices.h
########################################
# GLOBAL INITIALIZATION AND CLEANUP #
########################################
# void yices_init(void)
libyices.yices_init.restype = None
def yices_init():
"""This function must be called before anything else to initialize internal data structures."""
global __yices_library_inited__
__yices_library_inited__ = True
libyices.yices_init()
# iam: 10/2/2018 N.B. Neither this nor yices_exit() get wrapped because either before or after execution
# __yices_library_inited__ can be False
def yices_is_inited():
"""This function True if the yices library has been initied, False otherwise."""
global __yices_library_inited__
return bool(__yices_library_inited__)
# void yices_exit(void)
libyices.yices_exit.restype = None
def yices_exit():
"""Delete all internal data structures and objects - this must be called to avoid memory leaks."""
global __yices_library_inited__
if __yices_library_inited__:
libyices.yices_exit()
__yices_library_inited__ = False
# void yices_reset(void)
libyices.yices_reset.restype = None
@catch_uninitialized()
def yices_reset():
"""A full reset of all internal data structures (terms, types, symbol tables, contexts, models, ...)."""
libyices.yices_reset()
# void yices_free_string(char*)
# No API for this - the functions which return a C string (e.g., yices_error_string)
# all call yices_free_string as soon as they cast it to a Python string
# void yices_set_out_of_mem_callback(void (*callback)(void))
# libyices.yices_set_out_of_mem_callback.restype = None
# CBFUNC = CFUNCTYPE(None)
# libyices.yices_set_out_of_mem_callback.argtypes = [c_void_p]
#######################
# ERROR REPORTING #
#######################
# error_code_t yices_error_code(void)
libyices.yices_error_code.restype = error_code_t
@catch_uninitialized()
def yices_error_code():
"""Get the last error code."""
return libyices.yices_error_code()
# error_report_t *yices_error_report(void)
libyices.yices_error_report.restype = POINTER(error_report_t)
@catch_uninitialized()
def yices_error_report():
"""Get the last error report."""
return libyices.yices_error_report().contents
# void yices_clear_error(void)
libyices.yices_clear_error.restype = None
@catch_uninitialized()
def yices_clear_error():
"""Clear the error report."""
libyices.yices_clear_error()
# int32_t yices_print_error_fd(int fd)
libyices.yices_print_error_fd.restype = c_int32
libyices.yices_print_error_fd.argtypes = [c_int]
@catch_uninitialized()
def yices_print_error_fd(fd):
"""Print an error message on file descriptor fd."""
return libyices.yices_print_error_fd(fd)
# char *yices_error_string(void)
# NOTE: restype is c_void_p in order not to trigger the automatic cast, which loses the pointer
libyices.yices_error_string.restype = c_void_p
libyices.yices_free_string.argtypes = [c_void_p]
@catch_uninitialized()
def yices_error_string():
"""Build a string from the current error code + error report structure."""
cstrptr = libyices.yices_error_string()
errstr = bytes2str(cast(cstrptr, c_char_p).value)
libyices.yices_free_string(cstrptr)
return errstr
#################################
# PYTHON <-> C UTILITIES #
#################################
# Utilities for constructing:
#
# - C arrays of a certain type from python arrays
# - C arrays that are empty (i.e. zeroed) of a certain type and given length
#
# types include: term_t type_t yval_t c_int32 c_int64.
#
# The idea being to hide as much of ctype weirdness as possible. This was a demand driven list.
# feel free to add more, or request that I (iam) add more.
#
def make_term_array(pyarray):
"""Makes a C term array object from a python array object"""
retval = None
if pyarray is not None:
#weird python and ctype magic
retval = (term_t * len(pyarray))(*pyarray)
return retval
def make_empty_term_array(n):
"""Makes an empty C term array object of length n"""
retval = (term_t * n)()
return retval
def make_type_array(pyarray):
"""Makes a C term array object from a python array object"""
retval = None
if pyarray is not None:
#weird python and ctype magic
retval = (type_t * len(pyarray))(*pyarray)
return retval
def make_empty_type_array(n):
"""Makes an empty C type array object of length n"""
retval = (type_t * n)()
return retval
def make_int32_array(pyarray):
"""Makes a C int32 array object from a python array object"""
retval = None
if pyarray is not None:
#weird python and ctype magic
retval = (c_int32 * len(pyarray))(*pyarray)
return retval
def make_empty_int32_array(n):
"""Makes an empty C int32 array object of length n"""
retval = (c_int32 * n)()
return retval
def make_int64_array(pyarray):
"""Makes a C int64 array object from a python array object"""
retval = None
if pyarray is not None:
#weird python and ctype magic
retval = (c_int64 * len(pyarray))(*pyarray)
return retval
def make_empty_int64_array(n):
"""Makes an empty C int64 array object of length n"""
retval = (c_int64 * n)()
return retval
def make_empty_yval_array(n):
"""Makes an empty C yval array object of length n"""
retval = (yval_t * n)()
return retval
#################################
# VECTORS OF TERMS AND TYPES #
#################################
# void yices_init_term_vector(term_vector_t *v)
libyices.yices_init_term_vector.restype = None
libyices.yices_init_term_vector.argtypes = [POINTER(term_vector_t)]
@catch_uninitialized()
def yices_init_term_vector(v):
"""Before calling any function that fills in a term_vector the vector object must be initialized via init_term_vector."""
libyices.yices_init_term_vector(pointer(v))
# void yices_init_type_vector(type_vector_t *v)
libyices.yices_init_type_vector.restype = None
libyices.yices_init_type_vector.argtypes = [POINTER(type_vector_t)]
@catch_uninitialized()
def yices_init_type_vector(v):
"""Before calling any function that fills in a type_vector the vector object must be initialized via init_type_vector."""
libyices.yices_init_type_vector(pointer(v))
# void yices_delete_term_vector(term_vector_t *v)
libyices.yices_delete_term_vector.restype = None
libyices.yices_delete_term_vector.argtypes = [POINTER(term_vector_t)]
@catch_uninitialized()
def yices_delete_term_vector(v):
"""To prevent memory leaks, a term_vector must be deleted when no longer needed."""
libyices.yices_delete_term_vector(pointer(v))
# void yices_delete_type_vector(type_vector_t *v)
libyices.yices_delete_type_vector.restype = None
libyices.yices_delete_type_vector.argtypes = [POINTER(type_vector_t)]
@catch_uninitialized()
def yices_delete_type_vector(v):
"""To prevent memory leaks, a type_vector must be deleted when no longer needed."""
libyices.yices_delete_type_vector(pointer(v))
# void yices_reset_term_vector(term_vector_t *v)
libyices.yices_reset_term_vector.restype = None
libyices.yices_reset_term_vector.argtypes = [POINTER(term_vector_t)]
@catch_uninitialized()
def yices_reset_term_vector(v):
"""Reset: empty the vector (reset size to 0)."""
libyices.yices_reset_term_vector(pointer(v))
# void yices_reset_type_vector(type_vector_t *v)
libyices.yices_reset_type_vector.restype = None
libyices.yices_reset_type_vector.argtypes = [POINTER(type_vector_t)]
@catch_uninitialized()
def yices_reset_type_vector(v):
"""Reset: empty the vector (reset size to 0)."""
libyices.yices_reset_type_vector(pointer(v))
#######################
# TYPE CONSTRUCTORS #
#######################
# type_t yices_bool_type(void)
libyices.yices_bool_type.restype = type_t
@catch_uninitialized()
def yices_bool_type():
"""Returns the built-in bool type."""
return libyices.yices_bool_type()
# type_t yices_int_type(void)
libyices.yices_int_type.restype = type_t
@catch_uninitialized()
def yices_int_type():
"""Returns the built-in int type."""
return libyices.yices_int_type()
# type_t yices_real_type(void)
libyices.yices_real_type.restype = type_t
@catch_uninitialized()
def yices_real_type():
"""Returns the built-in real type."""
return libyices.yices_real_type()
# type_t yices_bv_type(uint32_t size)
libyices.yices_bv_type.restype = type_t
libyices.yices_bv_type.argtypes = [c_uint32]
@catch_error(-1)
def yices_bv_type(size):
"""Returns the type of bitvectors of given size (number of bits), size > 0."""
# let yices deal with int32_t excesses
if size > MAX_INT32_SIZE:
size = MAX_INT32_SIZE
return libyices.yices_bv_type(size)
# type_t yices_new_scalar_type(uint32_t card)
libyices.yices_new_scalar_type.restype = type_t
libyices.yices_new_scalar_type.argtypes = [c_uint32]
@catch_error(-1)
def yices_new_scalar_type(card):
"""New scalar type of given cardinality, card > 0."""
return libyices.yices_new_scalar_type(card)
# type_t yices_new_uninterpreted_type(void)
libyices.yices_new_uninterpreted_type.restype = type_t
@catch_uninitialized()
def yices_new_uninterpreted_type():
"""New uninterpreted type, no error report."""
return libyices.yices_new_uninterpreted_type()
# type_t yices_tuple_type(uint32_t n, const type_t tau[])
libyices.yices_tuple_type.restype = type_t
libyices.yices_tuple_type.argtypes = [c_uint32, POINTER(type_t)]
@catch_error(-1)
def yices_tuple_type(n, tau):
"""Tuple type tau[0] x ... x tau[n-1], requires n > 0 and tau[0] ... tau[n-1] to be well defined types."""
return libyices.yices_tuple_type(n, tau)
# type_t yices_tuple_type1(type_t tau1)
libyices.yices_tuple_type1.restype = type_t
libyices.yices_tuple_type1.argtypes = [type_t]
@catch_error(-1)
def yices_tuple_type1(tau):
"""For convenience: returns a unary tuple type, tau must be a valid type."""
return libyices.yices_tuple_type1(tau)
# type_t yices_tuple_type2(type_t tau1, type_t tau2)
libyices.yices_tuple_type2.restype = type_t
libyices.yices_tuple_type2.argtypes = [type_t, type_t]
@catch_error(-1)
def yices_tuple_type2(tau1, tau2):
"""For convenience: returns a binary tuple type, tau1, tau2 must be a valid types."""
return libyices.yices_tuple_type2(tau1, tau2)
# type_t yices_tuple_type3(type_t tau1, type_t tau2, type_t tau3)
libyices.yices_tuple_type3.restype = type_t
libyices.yices_tuple_type3.argtypes = [type_t, type_t, type_t]
@catch_error(-1)
def yices_tuple_type3(tau1, tau2, tau3):
"""For convenience: returns a ternary tuple type, tau1, tau2, tau3 must be a valid types."""
return libyices.yices_tuple_type3(tau1, tau2, tau3)
# type_t yices_function_type(uint32_t n, const type_t dom[], type_t range)
libyices.yices_function_type.restype = type_t
libyices.yices_function_type.argtypes = [c_uint32, POINTER(type_t), type_t]
@catch_error(-1)
def yices_function_type(n, dom, ran):
"""Function type: dom[0] ... dom[n-1] -> ran, requires n>0, and dom[0] ... dom[n-1] and ran to be well defined."""
return libyices.yices_function_type(n, dom, ran)
# type_t yices_function_type1(type_t tau1, type_t range)
libyices.yices_function_type1.restype = type_t
libyices.yices_function_type1.argtypes = [type_t, type_t]
@catch_error(-1)
def yices_function_type1(tau1, ran):
"""For convenience: returns the function type tau1 -> ran, tau1, ran must be a valid types."""
return libyices.yices_function_type1(tau1, ran)
# type_t yices_function_type2(type_t tau1, type_t tau2, type_t range)
libyices.yices_function_type2.restype = type_t
libyices.yices_function_type2.argtypes = [type_t, type_t, type_t]
@catch_error(-1)
def yices_function_type2(tau1, tau2, ran):
"""For convenience: returns the function type tau1, tau2 -> ran, tau1, tau2, ran must be a valid types."""
return libyices.yices_function_type2(tau1, tau2, ran)
# type_t yices_function_type3(type_t tau1, type_t tau2, type_t tau3, type_t range)
libyices.yices_function_type3.restype = type_t
libyices.yices_function_type3.argtypes = [type_t, type_t, type_t, type_t]
@catch_error(-1)
def yices_function_type3(tau1, tau2, tau3, ran):
"""For convenience: returns the function type tau1, tau2, tau3 -> ran, tau1, tau2, tau3, ran must be a valid types."""
return libyices.yices_function_type3(tau1, tau2, tau3, ran)
#########################
# TYPE EXPLORATION #
#########################
# int32_t yices_type_is_bool(type_t tau)
libyices.yices_type_is_bool.restype = c_int32
libyices.yices_type_is_bool.argtypes = [type_t]
@catch_error(0)
def yices_type_is_bool(tau):
"""Returns 1 if tau is the built-in bool type, 0 otherwise."""
return libyices.yices_type_is_bool(tau)
# int32_t yices_type_is_int(type_t tau)
libyices.yices_type_is_int.restype = c_int32
libyices.yices_type_is_int.argtypes = [type_t]
@catch_error(0)
def yices_type_is_int(tau):
"""Returns 1 if tau is the built-in int type, 0 otherwise."""
return libyices.yices_type_is_int(tau)
# int32_t yices_type_is_real(type_t tau)
libyices.yices_type_is_real.restype = c_int32
libyices.yices_type_is_real.argtypes = [type_t]
@catch_error(0)
def yices_type_is_real(tau):
"""Returns 1 if tau is the built-in real type, 0 otherwise."""
return libyices.yices_type_is_real(tau)
# int32_t yices_type_is_arithmetic(type_t tau)
libyices.yices_type_is_arithmetic.restype = c_int32
libyices.yices_type_is_arithmetic.argtypes = [type_t]
@catch_error(0)
def yices_type_is_arithmetic(tau):
"""Returns 1 if tau is either the int or real type, 0 otherwise."""
return libyices.yices_type_is_arithmetic(tau)
# int32_t yices_type_is_bitvector(type_t tau)
libyices.yices_type_is_bitvector.restype = c_int32
libyices.yices_type_is_bitvector.argtypes = [type_t]
@catch_error(0)
def yices_type_is_bitvector(tau):
"""Returns 1 if tau is a bitvector type, 0 otherwise."""
return libyices.yices_type_is_bitvector(tau)
# int32_t yices_type_is_tuple(type_t tau)
libyices.yices_type_is_tuple.restype = c_int32
libyices.yices_type_is_tuple.argtypes = [type_t]
@catch_error(0)
def yices_type_is_tuple(tau):
"""Returns 1 if tau is a tuple type, 0 otherwise."""
return libyices.yices_type_is_tuple(tau)
# int32_t yices_type_is_function(type_t tau)
libyices.yices_type_is_function.restype = c_int32
libyices.yices_type_is_function.argtypes = [type_t]
@catch_error(0)
def yices_type_is_function(tau):
"""Returns 1 if tau is a function type, 0 otherwise."""
return libyices.yices_type_is_function(tau)
# int32_t yices_type_is_scalar(type_t tau)
libyices.yices_type_is_scalar.restype = c_int32
libyices.yices_type_is_scalar.argtypes = [type_t]
@catch_error(0)
def yices_type_is_scalar(tau):
"""Returns 1 if tau is a scalar type, 0 otherwise."""
return libyices.yices_type_is_scalar(tau)
# int32_t yices_type_is_uninterpreted(type_t tau)
libyices.yices_type_is_uninterpreted.restype = c_int32
libyices.yices_type_is_uninterpreted.argtypes = [type_t]
@catch_error(0)
def yices_type_is_uninterpreted(tau):
"""Returns 1 if tau is a uninterpreted type, 0 otherwise."""
return libyices.yices_type_is_uninterpreted(tau)
# int32_t yices_test_subtype(type_t tau, type_t sigma)
libyices.yices_test_subtype.restype = c_int32
libyices.yices_test_subtype.argtypes = [type_t, type_t]
@catch_error(0)
def yices_test_subtype(tau, sigma):
"""Returns 1 if tau is a subtype of sigma, 0 otherwise."""
return libyices.yices_test_subtype(tau, sigma)
# int32_t yices_compatible_types(type_t tau, type_t sigma)
libyices.yices_compatible_types.restype = c_int32
libyices.yices_compatible_types.argtypes = [type_t, type_t]
@catch_error(0)
def yices_compatible_types(tau, sigma):
"""Returns 1 if tau is and sigma are compatible types, 0 otherwise."""
return libyices.yices_compatible_types(tau, sigma)
# uint32_t yices_bvtype_size(type_t tau)
libyices.yices_bvtype_size.restype = c_uint32
libyices.yices_bvtype_size.argtypes = [type_t]
@catch_error(0)
def yices_bvtype_size(tau):
"""Returns the number of bits for type tau, or 0 if there's and error."""
return libyices.yices_bvtype_size(tau)
# uint32_t yices_scalar_type_card(type_t tau)
libyices.yices_scalar_type_card.restype = c_uint32
libyices.yices_scalar_type_card.argtypes = [type_t]
@catch_error(0)
def yices_scalar_type_card(tau):
"""Returns the cardinality of teh scalar tau, or 0 if there's and error."""
return libyices.yices_scalar_type_card(tau)
# int32_t yices_type_num_children(type_t tau)
libyices.yices_type_num_children.restype = c_int32
libyices.yices_type_num_children.argtypes = [type_t]
@catch_error(-1)
def yices_type_num_children(tau):
"""Returns the number of children of type tau.
- if tau is a tuple type (tuple tau_1 ... tau_n), returns n
- if tau is a function type (-> tau_1 ... tau_n sigma), returns n+1