Skip to content

Commit a04093e

Browse files
alex-ozdemirnafur
andauthored
More examples & transcendentals (#75)
Co-authored-by: Gereon Kremer <nafur42@gmail.com>
1 parent a35b49e commit a04093e

8 files changed

Lines changed: 208 additions & 7 deletions

File tree

cvc5_pythonic_api/cvc5_pythonic.py

Lines changed: 148 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3063,6 +3063,149 @@ def Gt(a, b):
30633063
return a > b
30643064

30653065

3066+
#########################################
3067+
#
3068+
# Transcendentals
3069+
#
3070+
#########################################
3071+
3072+
3073+
def Pi(ctx=None):
3074+
""" Create the constant pi
3075+
3076+
>>> Pi()
3077+
Pi
3078+
"""
3079+
ctx = get_ctx(ctx)
3080+
return _to_expr_ref(ctx.solver.mkTerm(Kind.PI), ctx)
3081+
3082+
3083+
def Exponential(x):
3084+
""" Create an exponential function
3085+
3086+
>>> x = Real('x')
3087+
>>> solve(Exponential(x) == 1)
3088+
[x = 0]
3089+
"""
3090+
return _nary_kind_builder(Kind.EXPONENTIAL, RealSort().cast(x))
3091+
3092+
3093+
def Sine(x):
3094+
""" Create a sine function
3095+
3096+
>>> x = Real('x')
3097+
>>> i = Int('i')
3098+
>>> prove(Sine(x) < 2)
3099+
proved
3100+
>>> prove(Sine(i) < 2)
3101+
proved
3102+
"""
3103+
return _nary_kind_builder(Kind.SINE, RealSort().cast(x))
3104+
3105+
3106+
def Cosine(x):
3107+
""" Create a cosine function
3108+
3109+
>>> x = Real('x')
3110+
>>> i = Int('i')
3111+
>>> prove(Cosine(x) < 2)
3112+
proved
3113+
>>> prove(Cosine(i) < 2)
3114+
proved
3115+
"""
3116+
return _nary_kind_builder(Kind.COSINE, RealSort().cast(x))
3117+
3118+
3119+
def Tangent(x):
3120+
""" Create a tangent function
3121+
3122+
>>> Tangent(Real('x'))
3123+
Tangent(x)
3124+
"""
3125+
return _nary_kind_builder(Kind.TANGENT, RealSort().cast(x))
3126+
3127+
3128+
def Arcsine(x):
3129+
""" Create an arcsine function
3130+
3131+
>>> Arcsine(Real('x'))
3132+
Arcsine(x)
3133+
"""
3134+
return _nary_kind_builder(Kind.ARCSINE, RealSort().cast(x))
3135+
3136+
3137+
def Arccosine(x):
3138+
""" Create an arccosine function
3139+
3140+
>>> Arccosine(Real('x'))
3141+
Arccosine(x)
3142+
"""
3143+
return _nary_kind_builder(Kind.ARCCOSINE, RealSort().cast(x))
3144+
3145+
3146+
def Arctangent(x):
3147+
""" Create an arctangent function
3148+
3149+
>>> Arctangent(Real('x'))
3150+
Arctangent(x)
3151+
"""
3152+
return _nary_kind_builder(Kind.ARCTANGENT, RealSort().cast(x))
3153+
3154+
3155+
def Secant(x):
3156+
""" Create a secant function
3157+
3158+
>>> Secant(Real('x'))
3159+
Secant(x)
3160+
"""
3161+
return _nary_kind_builder(Kind.SECANT, RealSort().cast(x))
3162+
3163+
3164+
def Cosecant(x):
3165+
""" Create a cosecant function
3166+
3167+
>>> Cosecant(Real('x'))
3168+
Cosecant(x)
3169+
"""
3170+
return _nary_kind_builder(Kind.COSECANT, RealSort().cast(x))
3171+
3172+
3173+
def Cotangent(x):
3174+
""" Create a cotangent function
3175+
3176+
>>> Cotangent(Real('x'))
3177+
Cotangent(x)
3178+
"""
3179+
return _nary_kind_builder(Kind.COTANGENT, RealSort().cast(x))
3180+
3181+
3182+
def Arcsecant(x):
3183+
""" Create an arcsecant function
3184+
3185+
>>> Arcsecant(Real('x'))
3186+
Arcsecant(x)
3187+
"""
3188+
return _nary_kind_builder(Kind.ARCSECANT, RealSort().cast(x))
3189+
3190+
3191+
def Arccosecant(x):
3192+
""" Create an arccosecant function
3193+
3194+
>>> Arccosecant(Real('x'))
3195+
Arccosecant(x)
3196+
"""
3197+
return _nary_kind_builder(Kind.ARCCOSECANT, RealSort().cast(x))
3198+
3199+
3200+
def Arccotangent(x):
3201+
""" Create an arccotangent function
3202+
3203+
>>> Arccotangent(Real('x'))
3204+
Arccotangent(x)
3205+
"""
3206+
return _nary_kind_builder(Kind.ARCCOTANGENT, RealSort().cast(x))
3207+
3208+
30663209
#########################################
30673210
#
30683211
# Bit-Vectors
@@ -6892,19 +7035,19 @@ def fpIsInf(a, ctx=None):
68927035
def fpIsZero(a, ctx=None):
68937036
"""Create a SMT floating-point isZero expression.
68947037
"""
6895-
return _mk_fp_unary_pred(Kind.FPIsZ, a, ctx)
7038+
return _mk_fp_unary_pred(Kind.FLOATINGPOINT_IS_ZERO, a, ctx)
68967039

68977040

68987041
def fpIsNormal(a, ctx=None):
68997042
"""Create a SMT floating-point isNormal expression.
69007043
"""
6901-
return _mk_fp_unary_pred(Kind.FPIsN, a, ctx)
7044+
return _mk_fp_unary_pred(Kind.FLOATINGPOINT_IS_NORMAL, a, ctx)
69027045

69037046

69047047
def fpIsSubnormal(a, ctx=None):
69057048
"""Create a SMT floating-point isSubnormal expression.
69067049
"""
6907-
return _mk_fp_unary_pred(Kind.FPIsSn, a, ctx)
7050+
return _mk_fp_unary_pred(Kind.FLOATINGPOINT_IS_SUBNORMAL, a, ctx)
69087051

69097052

69107053
def fpIsNegative(a, ctx=None):
@@ -7538,7 +7681,7 @@ def accessor(self, i, j):
75387681

75397682
class DatatypeConstructorRef(FuncDeclRef):
75407683
def __init__(self, datatype, ctx=None, r=False):
7541-
super().__init__(datatype.getConstructorTerm(), ctx, r)
7684+
super().__init__(datatype.getTerm(), ctx, r)
75427685
self.dt = datatype
75437686

75447687
def arity(self):
@@ -7611,7 +7754,7 @@ def __call__(self, *args):
76117754

76127755
class DatatypeSelectorRef(FuncDeclRef):
76137756
def __init__(self, datatype, ctx=None, r=False):
7614-
super().__init__(datatype.getSelectorTerm(), ctx, r)
7757+
super().__init__(datatype.getTerm(), ctx, r)
76157758
self.dt = datatype
76167759

76177760
def arity(self):

cvc5_pythonic_api/cvc5_pythonic_printer.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ def _assert(cond, msg):
122122
Kind.FLOATINGPOINT_IS_SUBNORMAL: "fpIsMinusnormal",
123123
Kind.FLOATINGPOINT_IS_NEG: "fpIsNegative",
124124
Kind.FLOATINGPOINT_IS_POS: "fpIsPositive",
125+
# Transcendental
126+
Kind.SINE: "Sine",
127+
Kind.COSINE: "Cosine",
128+
Kind.TANGENT: "Tangent",
129+
Kind.SECANT: "Secant",
130+
Kind.COSECANT: "Cosecant",
131+
Kind.COTANGENT: "Cotangent",
132+
Kind.ARCSINE: "Arcsine",
133+
Kind.ARCCOSINE: "Arccosine",
134+
Kind.ARCTANGENT: "Arctangent",
135+
Kind.ARCSECANT: "Arcsecant",
136+
Kind.ARCCOSECANT: "Arccosecant",
137+
Kind.ARCCOTANGENT: "Arccotangent",
138+
Kind.PI: "Pi",
139+
Kind.EXPONENTIAL: "Exponential",
125140
}
126141

127142
# List of infix operators
@@ -346,7 +361,7 @@ def _op_name(a):
346361
k = a.kind()
347362
n = _cvc5_kinds_to_str.get(k, None)
348363
if n is None:
349-
if k in [Kind.CONSTANT, Kind.CONST_FLOATINGPOINT, Kind.CONST_ROUNDINGMODE, Kind.VARIABLE, Kind.UNINTERPRETED_SORT_VALUE]:
364+
if k in [Kind.CONSTANT, Kind.CONST_FLOATINGPOINT, Kind.CONST_ROUNDINGMODE, Kind.VARIABLE, Kind.UNINTERPRETED_SORT_VALUE, Kind.PI]:
350365
return str(a.ast)
351366
if k == Kind.INTERNAL_KIND:
352367
# Hack to handle DT selectors and constructors
@@ -1088,7 +1103,7 @@ def pp_app(self, a, d, xs):
10881103
elif k == Kind.CONST_ARRAY:
10891104
return self.pp_K(a, d, xs)
10901105
# Slight hack to handle DT fns here (InternalKind case).
1091-
elif k in [Kind.CONSTANT, Kind.INTERNAL_KIND, Kind.VARIABLE, Kind.UNINTERPRETED_SORT_VALUE]:
1106+
elif k in [Kind.CONSTANT, Kind.INTERNAL_KIND, Kind.VARIABLE, Kind.UNINTERPRETED_SORT_VALUE, Kind.PI]:
10921107
return self.pp_name(a)
10931108
# elif cvc.is_pattern(a):
10941109
# return self.pp_pattern(a, d, xs)

test/pgm_outputs/example_exceptions.py.out

Whitespace-only changes.

test/pgm_outputs/example_id.py.out

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no solution

test/pgms/example_exceptions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from cvc5_pythonic_api import *
2+
3+
if __name__ == '__main__':
4+
s = Solver()
5+
s.set("produce-models", True)
6+
try:
7+
# invalid option
8+
s.set("non-existing-option", True)
9+
except:
10+
pass
11+
12+
try:
13+
# type error
14+
Int("x") + BitVec("a", 5)
15+
except:
16+
pass
17+
18+
s += BoolVal(False)
19+
s.check()
20+
try:
21+
s.model()
22+
except:
23+
pass

test/pgms/example_id.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from cvc5_pythonic_api import *
2+
3+
if __name__ == '__main__':
4+
A = Set("A", SetSort(IntSort()))
5+
B = Set("B", SetSort(IntSort()))
6+
C = Set("C", SetSort(IntSort()))
7+
8+
assert A.get_id() != B.get_id()
9+
assert C.get_id() != B.get_id()
10+
assert A.get_id() == A.get_id()
11+
assert B.get_id() == B.get_id()
12+
assert C.get_id() == C.get_id()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from cvc5_pythonic_api import *
2+
3+
if __name__ == '__main__':
4+
x, y = Reals("x y")
5+
solve(x > Pi(),
6+
x < 2 * Pi(),
7+
y ** 2 < Sine(x))

0 commit comments

Comments
 (0)