z3 0.0.20221020 → 0.0.20230107
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/z3/expr/arith_expr.rb +20 -0
- data/lib/z3/expr/bitvec_expr.rb +20 -0
- data/lib/z3/expr/float_expr.rb +4 -0
- data/spec/bitvec_expr_spec.rb +33 -0
- data/spec/float_expr_spec.rb +34 -0
- data/spec/int_expr_spec.rb +30 -0
- data/spec/integration/examples/knights_puzzle-5.txt +90 -0
- data/spec/real_expr_spec.rb +30 -0
- data/spec/spec_helper.rb +19 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5369c721ea50477790661ac8975e9c66104028450d4a73a2916eac046633380
|
4
|
+
data.tar.gz: 713374dddd23a82e7a22ec32e60964288182306201ef6e3e212e17b8d25c7a06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22d87abcdc1c5b8570bb0c52db0a0086185336bba84092666b010de0504cfa15e9973073d0ca1fa0ac5cb2eb448b2e9ae6da1ba5f7f807255fbbb91e573c42cf
|
7
|
+
data.tar.gz: cc3527b80fd35db96b1dd6cf2fde3511f97cc94316f76c507e8c7ded6f6fef1b122fdc477a043870a15acd7bf76a87e6884f3e6d502998c762315b3732d14023
|
data/lib/z3/expr/arith_expr.rb
CHANGED
@@ -40,6 +40,26 @@ module Z3
|
|
40
40
|
sort.new(LowLevel.mk_unary_minus(self))
|
41
41
|
end
|
42
42
|
|
43
|
+
def zero?
|
44
|
+
self == 0
|
45
|
+
end
|
46
|
+
|
47
|
+
def nonzero?
|
48
|
+
self != 0
|
49
|
+
end
|
50
|
+
|
51
|
+
def positive?
|
52
|
+
self > 0
|
53
|
+
end
|
54
|
+
|
55
|
+
def negative?
|
56
|
+
self < 0
|
57
|
+
end
|
58
|
+
|
59
|
+
def abs
|
60
|
+
(self < 0).ite(-self, self)
|
61
|
+
end
|
62
|
+
|
43
63
|
# Recast so 1 + x:Float
|
44
64
|
# is: (+ 1.0 x)
|
45
65
|
# not: (+ (to_real 1) x)
|
data/lib/z3/expr/bitvec_expr.rb
CHANGED
@@ -246,6 +246,26 @@ module Z3
|
|
246
246
|
BitvecExpr.UnsignedLe(self, other)
|
247
247
|
end
|
248
248
|
|
249
|
+
def zero?
|
250
|
+
self == 0
|
251
|
+
end
|
252
|
+
|
253
|
+
def nonzero?
|
254
|
+
self != 0
|
255
|
+
end
|
256
|
+
|
257
|
+
def positive?
|
258
|
+
self.signed_gt 0
|
259
|
+
end
|
260
|
+
|
261
|
+
def negative?
|
262
|
+
self.signed_lt 0
|
263
|
+
end
|
264
|
+
|
265
|
+
def abs
|
266
|
+
self.negative?.ite(-self, self)
|
267
|
+
end
|
268
|
+
|
249
269
|
def coerce(other)
|
250
270
|
other_sort = Expr.sort_for_const(other)
|
251
271
|
max_sort = [sort, other_sort].max
|
data/lib/z3/expr/float_expr.rb
CHANGED
data/spec/bitvec_expr_spec.rb
CHANGED
@@ -231,5 +231,38 @@ module Z3
|
|
231
231
|
expect([a == 0b0101_0110, e == 0b1101, d == a.concat(e)]).to have_solution(d => 0b0101_0110_1101)
|
232
232
|
expect([a == 0b0101_0110, e == 0b1101, d == e.concat(a)]).to have_solution(d => 0b1101_0101_0110)
|
233
233
|
end
|
234
|
+
|
235
|
+
it "zero?" do
|
236
|
+
expect([a == 0, x == a.zero?]).to have_solution(x => true)
|
237
|
+
expect([a == 100, x == a.zero?]).to have_solution(x => false)
|
238
|
+
expect([a == 200, x == a.zero?]).to have_solution(x => false)
|
239
|
+
end
|
240
|
+
|
241
|
+
it "nonzero?" do
|
242
|
+
expect([a == 0, x == a.nonzero?]).to have_solution(x => false)
|
243
|
+
expect([a == 100, x == a.nonzero?]).to have_solution(x => true)
|
244
|
+
expect([a == 200, x == a.nonzero?]).to have_solution(x => true)
|
245
|
+
end
|
246
|
+
|
247
|
+
# Inherently signed
|
248
|
+
it "positive?" do
|
249
|
+
expect([a == 0, x == a.positive?]).to have_solution(x => false)
|
250
|
+
expect([a == 100, x == a.positive?]).to have_solution(x => true)
|
251
|
+
expect([a == 200, x == a.positive?]).to have_solution(x => false)
|
252
|
+
end
|
253
|
+
|
254
|
+
# Inherently signed
|
255
|
+
it "negative?" do
|
256
|
+
expect([a == 0, x == a.negative?]).to have_solution(x => false)
|
257
|
+
expect([a == 100, x == a.negative?]).to have_solution(x => false)
|
258
|
+
expect([a == 200, x == a.negative?]).to have_solution(x => true)
|
259
|
+
end
|
260
|
+
|
261
|
+
# Inherently signed
|
262
|
+
it "abs" do
|
263
|
+
expect([a == 0, b == a.abs]).to have_solution(b => 0)
|
264
|
+
expect([a == 100, b == a.abs]).to have_solution(b => 100)
|
265
|
+
expect([a == 200, b == a.abs]).to have_solution(b => 56)
|
266
|
+
end
|
234
267
|
end
|
235
268
|
end
|
data/spec/float_expr_spec.rb
CHANGED
@@ -129,6 +129,17 @@ module Z3
|
|
129
129
|
expect([x == nan.zero?]).to have_solution(x => false)
|
130
130
|
end
|
131
131
|
|
132
|
+
# Same as positive or negative
|
133
|
+
# +0, -0, and NaN are all false
|
134
|
+
it "nonzero?" do
|
135
|
+
expect([x == positive_zero.nonzero?]).to have_solution(x => false)
|
136
|
+
expect([x == negative_zero.nonzero?]).to have_solution(x => false)
|
137
|
+
expect([x == positive_infinity.nonzero?]).to have_solution(x => true)
|
138
|
+
expect([x == negative_infinity.nonzero?]).to have_solution(x => true)
|
139
|
+
expect([x == float_double.from_const(1.5).nonzero?]).to have_solution(x => true)
|
140
|
+
expect([x == nan.nonzero?]).to have_solution(x => false)
|
141
|
+
end
|
142
|
+
|
132
143
|
it "infinite?" do
|
133
144
|
expect([x == positive_zero.infinite?]).to have_solution(x => false)
|
134
145
|
expect([x == positive_infinity.infinite?]).to have_solution(x => true)
|
@@ -179,5 +190,28 @@ module Z3
|
|
179
190
|
expect([x == float_double.from_const(-1.5).negative?]).to have_solution(x => true)
|
180
191
|
expect([x == nan.negative?]).to have_solution(x => false)
|
181
192
|
end
|
193
|
+
|
194
|
+
# We can't simply do have_solution(b => positive_zero)
|
195
|
+
# as positive_zero == negative_zero
|
196
|
+
it "abs" do
|
197
|
+
expect([a == positive_zero, b == a.abs]).to have_solution(a.abs => positive_zero)
|
198
|
+
expect([a == negative_zero, b == a.abs]).to have_solution(a.abs => positive_zero)
|
199
|
+
expect([a == positive_infinity, b == a.abs]).to have_solution(b => positive_infinity)
|
200
|
+
expect([a == negative_infinity, b == a.abs]).to have_solution(b => positive_infinity)
|
201
|
+
expect([a == float_double.from_const(1.5), b == a.abs]).to have_solution(b => float_double.from_const(1.5))
|
202
|
+
expect([a == float_double.from_const(-1.5), b == a.abs]).to have_solution(b => float_double.from_const(1.5))
|
203
|
+
expect([a == nan, b == a.abs]).to have_no_solution
|
204
|
+
end
|
205
|
+
|
206
|
+
# This means you need to be extra careful when using Z3::Float
|
207
|
+
# as == means something else on them than mathematical ==
|
208
|
+
#
|
209
|
+
# Also have_solutions helper doesn't work as it relies on ==
|
210
|
+
it "zeroes" do
|
211
|
+
expect([a == positive_zero, b == a, b.positive?]).to have_solution(b => positive_zero)
|
212
|
+
expect([a == negative_zero, b == a, b.negative?]).to have_solution(b => negative_zero)
|
213
|
+
expect([a == positive_zero, b == a, b.positive?]).to have_solution(b => positive_zero)
|
214
|
+
expect([a == negative_zero, b == a, b.negative?]).to have_solution(b => negative_zero)
|
215
|
+
end
|
182
216
|
end
|
183
217
|
end
|
data/spec/int_expr_spec.rb
CHANGED
@@ -81,6 +81,36 @@ module Z3
|
|
81
81
|
expect([a == 3, b == -a]).to have_solution(b => -3)
|
82
82
|
end
|
83
83
|
|
84
|
+
it "zero?" do
|
85
|
+
expect([a == 0, x == a.zero?]).to have_solution(x => true)
|
86
|
+
expect([a == 100, x == a.zero?]).to have_solution(x => false)
|
87
|
+
expect([a == -200, x == a.zero?]).to have_solution(x => false)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "nonzero?" do
|
91
|
+
expect([a == 0, x == a.nonzero?]).to have_solution(x => false)
|
92
|
+
expect([a == 100, x == a.nonzero?]).to have_solution(x => true)
|
93
|
+
expect([a == -200, x == a.nonzero?]).to have_solution(x => true)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "positive?" do
|
97
|
+
expect([a == 0, x == a.positive?]).to have_solution(x => false)
|
98
|
+
expect([a == 100, x == a.positive?]).to have_solution(x => true)
|
99
|
+
expect([a == -200, x == a.positive?]).to have_solution(x => false)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "negative?" do
|
103
|
+
expect([a == 0, x == a.negative?]).to have_solution(x => false)
|
104
|
+
expect([a == 100, x == a.negative?]).to have_solution(x => false)
|
105
|
+
expect([a == -200, x == a.negative?]).to have_solution(x => true)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "abs" do
|
109
|
+
expect([a == 3, b == 2, c == (a - b).abs]).to have_solution(c => 1)
|
110
|
+
expect([a == 2, b == 3, c == (a - b).abs]).to have_solution(c => 1)
|
111
|
+
expect([a == 2, b == 2, c == (a - b).abs]).to have_solution(c => 0)
|
112
|
+
end
|
113
|
+
|
84
114
|
it "simplify" do
|
85
115
|
a = Z3.Const(5)
|
86
116
|
b = Z3.Const(3)
|
@@ -0,0 +1,90 @@
|
|
1
|
+
Solved
|
2
|
+
State 0:
|
3
|
+
bbb.
|
4
|
+
xbxw
|
5
|
+
..ww
|
6
|
+
x.xw
|
7
|
+
b: 0,0 -> 1,2
|
8
|
+
|
9
|
+
State 1:
|
10
|
+
.bb.
|
11
|
+
xbxw
|
12
|
+
.bww
|
13
|
+
x.xw
|
14
|
+
w: 2,2 -> 3,0
|
15
|
+
|
16
|
+
State 2:
|
17
|
+
.bbw
|
18
|
+
xbxw
|
19
|
+
.b.w
|
20
|
+
x.xw
|
21
|
+
b: 1,0 -> 2,2
|
22
|
+
|
23
|
+
State 3:
|
24
|
+
..bw
|
25
|
+
xbxw
|
26
|
+
.bbw
|
27
|
+
x.xw
|
28
|
+
w: 3,1 -> 1,0
|
29
|
+
|
30
|
+
State 4:
|
31
|
+
.wbw
|
32
|
+
xbx.
|
33
|
+
.bbw
|
34
|
+
x.xw
|
35
|
+
b: 1,2 -> 3,1
|
36
|
+
|
37
|
+
State 5:
|
38
|
+
.wbw
|
39
|
+
xbxb
|
40
|
+
..bw
|
41
|
+
x.xw
|
42
|
+
w: 3,3 -> 1,2
|
43
|
+
|
44
|
+
State 6:
|
45
|
+
.wbw
|
46
|
+
xbxb
|
47
|
+
.wbw
|
48
|
+
x.x.
|
49
|
+
w: 1,2 -> 0,0
|
50
|
+
|
51
|
+
State 7:
|
52
|
+
wwbw
|
53
|
+
xbxb
|
54
|
+
..bw
|
55
|
+
x.x.
|
56
|
+
b: 2,0 -> 1,2
|
57
|
+
|
58
|
+
State 8:
|
59
|
+
ww.w
|
60
|
+
xbxb
|
61
|
+
.bbw
|
62
|
+
x.x.
|
63
|
+
b: 1,2 -> 3,3
|
64
|
+
|
65
|
+
State 9:
|
66
|
+
ww.w
|
67
|
+
xbxb
|
68
|
+
..bw
|
69
|
+
x.xb
|
70
|
+
w: 3,2 -> 2,0
|
71
|
+
|
72
|
+
State 10:
|
73
|
+
wwww
|
74
|
+
xbxb
|
75
|
+
..b.
|
76
|
+
x.xb
|
77
|
+
b: 1,1 -> 3,2
|
78
|
+
|
79
|
+
State 11:
|
80
|
+
wwww
|
81
|
+
x.xb
|
82
|
+
..bb
|
83
|
+
x.xb
|
84
|
+
w: 3,0 -> 1,1
|
85
|
+
|
86
|
+
State 12:
|
87
|
+
www.
|
88
|
+
xwxb
|
89
|
+
..bb
|
90
|
+
x.xb
|
data/spec/real_expr_spec.rb
CHANGED
@@ -64,6 +64,36 @@ module Z3
|
|
64
64
|
expect([a == 81, b == 0.25, c == (a ** b)]).to have_solution(c => 3)
|
65
65
|
end
|
66
66
|
|
67
|
+
it "zero?" do
|
68
|
+
expect([a == 0, x == a.zero?]).to have_solution(x => true)
|
69
|
+
expect([a == 100, x == a.zero?]).to have_solution(x => false)
|
70
|
+
expect([a == -200, x == a.zero?]).to have_solution(x => false)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "nonzero?" do
|
74
|
+
expect([a == 0, x == a.nonzero?]).to have_solution(x => false)
|
75
|
+
expect([a == 100, x == a.nonzero?]).to have_solution(x => true)
|
76
|
+
expect([a == -200, x == a.nonzero?]).to have_solution(x => true)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "positive?" do
|
80
|
+
expect([a == 0, x == a.positive?]).to have_solution(x => false)
|
81
|
+
expect([a == 100, x == a.positive?]).to have_solution(x => true)
|
82
|
+
expect([a == -200, x == a.positive?]).to have_solution(x => false)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "negative?" do
|
86
|
+
expect([a == 0, x == a.negative?]).to have_solution(x => false)
|
87
|
+
expect([a == 100, x == a.negative?]).to have_solution(x => false)
|
88
|
+
expect([a == -200, x == a.negative?]).to have_solution(x => true)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "abs" do
|
92
|
+
expect([a == 3, b == 2, c == (a - b).abs]).to have_solution(c => 1)
|
93
|
+
expect([a == 2, b == 3, c == (a - b).abs]).to have_solution(c => 1)
|
94
|
+
expect([a == 2, b == 2, c == (a - b).abs]).to have_solution(c => 0)
|
95
|
+
end
|
96
|
+
|
67
97
|
it "unary -" do
|
68
98
|
expect([a == 3, b == -a]).to have_solution(b => -3)
|
69
99
|
expect([a == 0, b == -a]).to have_solution(b => 0)
|
data/spec/spec_helper.rb
CHANGED
@@ -155,3 +155,22 @@ RSpec::Matchers.define :have_solutions do |expected|
|
|
155
155
|
end
|
156
156
|
end
|
157
157
|
end
|
158
|
+
|
159
|
+
RSpec::Matchers.define :have_no_solution do
|
160
|
+
match do |asts|
|
161
|
+
solver = setup_solver(asts)
|
162
|
+
!solver.satisfiable?
|
163
|
+
end
|
164
|
+
|
165
|
+
failure_message do |asts|
|
166
|
+
"expected #{asts.inspect} to have no solutions, but solution found"
|
167
|
+
end
|
168
|
+
|
169
|
+
def setup_solver(asts)
|
170
|
+
Z3::Solver.new.tap do |solver|
|
171
|
+
asts.each do |ast|
|
172
|
+
solver.assert ast
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: z3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20230107
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomasz Wegrzanowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -288,6 +288,7 @@ files:
|
|
288
288
|
- spec/integration/examples/knights_puzzle-2.txt
|
289
289
|
- spec/integration/examples/knights_puzzle-3.txt
|
290
290
|
- spec/integration/examples/knights_puzzle-4.txt
|
291
|
+
- spec/integration/examples/knights_puzzle-5.txt
|
291
292
|
- spec/integration/four_hackers_puzzle_spec.rb
|
292
293
|
- spec/integration/futoshiki_spec.rb
|
293
294
|
- spec/integration/geometry_problem_spec.rb
|