Skip to content

Commit

Permalink
Merge branch 'main' into fix-ctx-solver
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ozdemir committed Aug 8, 2023
2 parents 83feea3 + 3a2de1e commit 290717e
Show file tree
Hide file tree
Showing 23 changed files with 998 additions and 34 deletions.
839 changes: 817 additions & 22 deletions cvc5_pythonic_api/cvc5_pythonic.py

Large diffs are not rendered by default.

35 changes: 29 additions & 6 deletions cvc5_pythonic_api/cvc5_pythonic_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ def _assert(cond, msg):
Kind.SEQ_UNIT: "Unit",
Kind.SEQ_CONTAINS: "Contains",
Kind.SEQ_REPLACE: "Replace",
Kind.SEQ_EXTRACT: "Extract",
Kind.SEQ_AT: "At",
Kind.SEQ_NTH: "Nth",
Kind.SEQ_NTH: "[]",
Kind.SEQ_INDEXOF: "IndexOf",
Kind.SEQ_UPDATE: "Update",
Kind.SEQ_LENGTH: "Length",
Kind.SET_SUBSET: "IsSubset",
Kind.SET_MINUS: "SetDifference",
Expand All @@ -110,14 +112,21 @@ def _assert(cond, msg):
Kind.SET_MEMBER: "IsMember",
Kind.STRING_TO_INT: "StrToInt",
Kind.STRING_FROM_INT: "IntToStr",
# Kind.Seq_in_re: "InRe",
# Kind.Seq_to_re: "Re",
Kind.STRING_FROM_CODE: "StringFromCode",
Kind.STRING_TO_REGEXP: "StringToRegexp",
Kind.STRING_IN_REGEXP: "StringInRegexp",
Kind.STRING_TO_CODE: "StringToCode",
Kind.REGEXP_LOOP: "RegexpLoop",
Kind.REGEXP_CONCAT: "RegexpConcat",
Kind.REGEXP_OPT: "RegexpOpt",
Kind.REGEXP_PLUS: "Plus",
Kind.REGEXP_STAR: "Star",
Kind.REGEXP_UNION: "Union",
Kind.REGEXP_RANGE: "Range",
Kind.REGEXP_INTER: "Intersect",
Kind.REGEXP_COMPLEMENT: "Complement",
Kind.REGEXP_ALL: "Full",
Kind.REGEXP_NONE: "RegexpNone",
Kind.FLOATINGPOINT_IS_NAN: "fpIsNaN",
Kind.FLOATINGPOINT_IS_INF: "fpIsInf",
Kind.FLOATINGPOINT_IS_ZERO: "fpIsZero",
Expand All @@ -140,6 +149,15 @@ def _assert(cond, msg):
Kind.ARCCOTANGENT: "Arccotangent",
Kind.PI: "Pi",
Kind.EXPONENTIAL: "Exponential",
# Strings
Kind.STRING_SUBSTR: "SubString",
Kind.STRING_CHARAT: "At",
Kind.STRING_CONCAT: "+",
Kind.STRING_LENGTH: "Length",
Kind.STRING_REPLACE: "Replace",
Kind.STRING_UPDATE: "StringUpdate",
Kind.STRING_LEQ: "<=",
Kind.STRING_LT: "<",
}

# List of infix operators
Expand Down Expand Up @@ -173,6 +191,9 @@ def _assert(cond, msg):
Kind.BITVECTOR_SHL,
Kind.FINITE_FIELD_ADD,
Kind.FINITE_FIELD_MULT,
Kind.STRING_LT,
Kind.STRING_LEQ,
Kind.SEQ_NTH,
]

_cvc5_unary = [Kind.NEG, Kind.BITVECTOR_NEG, Kind.BITVECTOR_NOT, Kind.FINITE_FIELD_NEG]
Expand Down Expand Up @@ -395,6 +416,8 @@ def _op_name(a):
Kind.UNINTERPRETED_SORT_VALUE,
Kind.PI,
Kind.CONST_INTEGER,
Kind.CONST_STRING,
Kind.CONST_SEQUENCE,
]:
return str(a.ast)
if k == Kind.INTERNAL_KIND:
Expand Down Expand Up @@ -786,7 +809,7 @@ def pp_algebraic(self, a):
return to_format(a.as_decimal(self.precision))

def pp_string(self, a):
return to_format('"' + a.as_string() + '"')
return to_format(str(a.ast))

def pp_bv(self, a):
return to_format(a.as_string())
Expand Down Expand Up @@ -1127,8 +1150,8 @@ def pp_app(self, a, d, xs):
return self.pp_fp_value(a)
elif cvc.is_fp(a):
return self.pp_fp(a, d, xs)
# elif cvc.is_string_value(a):
# return self.pp_string(a)
elif cvc.is_string_value(a):
return self.pp_string(a)
elif cvc.is_const(a):
return self.pp_const(a)
else:
Expand Down
12 changes: 12 additions & 0 deletions test/pgm_outputs/example_re.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
True
[a = "a"]
no solution
[a = ""]
[a = ""]
[a = "a"]
[a = "a"]
[a = "A"]
[a = "a"]
[a = ""]
no solution
[a = ""]
2 changes: 2 additions & 0 deletions test/pgm_outputs/example_re_concat.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[s = "ab"]
[s = "a"]
13 changes: 13 additions & 0 deletions test/pgm_outputs/example_sequence.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
False
[s = (seq.unit 1)(), t = (seq.unit 2)()]
[s = (seq.unit (- 1))(), t = (seq.unit (- 1))()]
[s = (as seq.empty (Seq Int))()]
[s = (as seq.empty (Seq Int))()]
[s = (seq.++ (seq.unit 1) (seq.unit 2))()]
[s = (as seq.empty (Seq Int))()]
[s = (seq.++ (seq.unit 1) (seq.unit 2))()]
[s = (seq.++ (seq.unit 1) (seq.unit 1))(), t = (seq.++ (seq.unit 1) (seq.unit 1))()]
[s = (as seq.empty (Seq Int))(), t = (as seq.empty (Seq Int))(), u = (as seq.empty (Seq Int))()]
[s = (seq.++ (seq.unit (- 2)) (seq.unit (- 1)))(), t = (seq.unit (- 1))()]
[s = (seq.++ (seq.unit (- 3)) (seq.unit (- 2)) (seq.unit 4))(), t = (seq.unit 4)()]
no solution
3 changes: 3 additions & 0 deletions test/pgm_outputs/example_sequence_update.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
True
[s = (seq.++ (seq.unit 1) (seq.unit 2))(), t = (as seq.empty (Seq Int))()]
[a = "heAAA"]
3 changes: 3 additions & 0 deletions test/pgm_outputs/example_string.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
True
True
[B = "", trying = ""]
2 changes: 2 additions & 0 deletions test/pgm_outputs/example_string_ar.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[a = "aAAAA"]
[a = "?"]
3 changes: 3 additions & 0 deletions test/pgm_outputs/example_string_comparison.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[a = "", b = "\u{0}", c = "\u{1}"]
[a = "A", b = "", c = ""]
[a = "", b = "", c = ""]
3 changes: 3 additions & 0 deletions test/pgm_outputs/example_string_concat.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[a = "", b = ""]
[a = "hello", b = ""]
no solution
7 changes: 7 additions & 0 deletions test/pgm_outputs/example_string_functions.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[b = "hello world"]
[b = "hello world"]
[b = "hello world"]
no solution
no solution
[b = "rello world"]
[b = ""]
4 changes: 4 additions & 0 deletions test/pgm_outputs/example_string_int.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[a = 0, b = -1]
[a = 1, b = 0]
[c = "0", d = ""]
[c = "\u{0}", d = ""]
21 changes: 15 additions & 6 deletions test/pgms/example_finite_field.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from cvc5_pythonic_api import *

if __name__ == '__main__':
try:

a, b = FiniteFieldElems('a b', 5)

a, b = FiniteFieldElems('a b', 5)
# SAT
solve(a * b == 1, a == 2)

# SAT
solve(a * b == 1, a == 2)

# UNSAT
solve(a * b == 1, a == 2, b == 2)
# UNSAT
solve(a * b == 1, a == 2, b == 2)
except RuntimeError as e:
# We want the test to pass in case cocoa is not installed
if "--cocoa" not in str(e):
# If the error is not related to cocoa then fail
raise e
else:
# If cocoa is not installed then mock correct result
print("[a = 2, b = -2]\nno solution")
16 changes: 16 additions & 0 deletions test/pgms/example_re.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from cvc5_pythonic_api import *
r1 = Re('a')
r2 = Re('b')
print(is_re(r1))
a = String('a')
solve(InRe(a,Union(r1,r2)))
solve(InRe(a,Intersect(r1,r2)))
solve(InRe(a,Complement(r1)))
solve(InRe(a,Star(Union(r1,r2))))
solve(InRe(a,Range('a','b')))
solve(InRe(a,Diff(Star(r1),Star(r2))))
solve(InRe(a,AllChar()))
solve(InRe(a,Plus(r1)))
solve(InRe(a,Option(r1)))
solve(InRe(a,Empty(ReSort())))
solve(InRe(a,Full()))
6 changes: 6 additions & 0 deletions test/pgms/example_re_concat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cvc5_pythonic_api import *
s = String('s')
r1 = Re('a')
r2 = Re('b')
solve(InRe(s,Concat(r1,r2)))
solve(InRe(s,Concat(r1,Star(r2))))
16 changes: 16 additions & 0 deletions test/pgms/example_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from cvc5_pythonic_api import *
s, t, u = Consts('s t u', SeqSort(IntSort()))
print(s.is_string())
solve(Concat(s, Unit(IntVal(2))) == Concat(Unit(IntVal(1)), t))
solve(PrefixOf(s.at(0),t),Length(s)>0)
solve(s[0]> 5)
solve(PrefixOf(s,Concat(Unit(IntVal(1)),Unit(IntVal(2)))))
solve(PrefixOf(Concat(Unit(IntVal(1)),Unit(IntVal(2))),s))
solve(SuffixOf(s,Concat(Unit(IntVal(1)),Unit(IntVal(2)))))
solve(SuffixOf(Concat(Unit(IntVal(1)),Unit(IntVal(2))),s))
solve(Contains(s,t),Length(t)>1)
solve(Replace(s,t,u)==Replace(s,u,t))
solve(IndexOf(s,t) == 1 )
solve(IndexOf(s,t,1) > 1 )
e = Empty(SeqSort(IntSort()))
solve(PrefixOf(s,e))
6 changes: 6 additions & 0 deletions test/pgms/example_sequence_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cvc5_pythonic_api import *
s,t = Consts('s t',SeqSort(IntSort()))
print(is_seq(s))
solve(SeqUpdate(s,t,0) == Concat(Unit(IntVal(1)),Unit(IntVal(2))))
a = String('a')
solve(SeqUpdate(a,StringVal('llo'),2) == 'hello')
6 changes: 6 additions & 0 deletions test/pgms/example_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cvc5_pythonic_api import *
n = String("trying")
b = String("B")
print(b.sort().is_string())
print(is_string(b))
solve(n==b)
4 changes: 4 additions & 0 deletions test/pgms/example_string_ar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from cvc5_pythonic_api import *
a = String('a')
solve(Length(a) == 5,a[0]==StringVal('a'))
solve(a.at(0)==StringVal('?'))
6 changes: 6 additions & 0 deletions test/pgms/example_string_comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cvc5_pythonic_api import *
a, b, c = Strings('a b c')
solve(a<b,b<c)
solve(a>b,b<=c)
solve(a >= c, a<=b)

7 changes: 7 additions & 0 deletions test/pgms/example_string_concat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from cvc5_pythonic_api import *
a = String('a')
b = String('b')
const = StringVal('hello')
solve(a == a+b)
solve(a == Concat(a,b),a == const)
solve(a == a+b, b == b+a, Length(a) > 0, Length(b) > 0 )
11 changes: 11 additions & 0 deletions test/pgms/example_string_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cvc5_pythonic_api import *
a = StringVal('hello world')
b = String('b')
solve(Contains(b,a))
solve(Contains(a,b),PrefixOf(a,b))
solve(Contains(a,b),SuffixOf(a,b))
solve(PrefixOf(a,b),SubString(b,0,2)==StringVal('HE'))
solve(IndexOf(b,a) == 3,b>a,IndexOf(b,a,2)==8)
solve(Replace(a,'h','r')==b)
e=Empty(StringSort())
solve(PrefixOf(b,e))
7 changes: 7 additions & 0 deletions test/pgms/example_string_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from cvc5_pythonic_api import *
a, b = Ints('a b')
solve(IntToStr(a)>IntToStr(b))
solve(StrFromCode(a) > StrFromCode(b))
c,d = Strings('c d')
solve(StrToInt(c)>StrToInt(d))
solve(StrToCode(c) > StrToCode(d))

0 comments on commit 290717e

Please sign in to comment.