z3 0.0.20221020 → 0.0.20230311

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: abba86119ff297f1b65693b634c5f33b11e53ea24f76b5b851d11918bf061694
4
- data.tar.gz: bf2253285a83d24d1e7a5264d839506f65b32a45c27203dcf12202daab0c527a
3
+ metadata.gz: bba953278a11ef2e902d7f7103195a57c2d94b1a515de357e23c4cce3a8ced81
4
+ data.tar.gz: c4dc956299e225582b8913d135adcd67595fcfb28c20565a1121b5d5ed73f983
5
5
  SHA512:
6
- metadata.gz: d63a2a62b5f8103eca1c0ae9429a5538cb5a0add23ca14fdceb1759f821d5a0f8ddeb22c06efad3780b5498d8c6ff0fea4f9d0c86c3d1e1dbfd443265f3b4f4a
7
- data.tar.gz: 7cd9d08794c1fa4cb2c47575c3977d35a781874c5f27f03ee31eb251e153062f91e8bd38236da887cc1a1fb6e401250038e98cd44d63197e120d84cd8171ef1f
6
+ metadata.gz: ca74743545f86daa86cb85a3f33ce0db5e3d159dcbe133e85a389b795d5ca6ad5b15842e275ef18a21e0a3317d010d1906143fc8bd6cbc27513777d0186a88df
7
+ data.tar.gz: a6ef5bb470556ad7d1bb39c54537ca9516aa64cc0bfb53fc8984352033ced94443e0272de9fa078a451f680af3a08d4c2c706e43f134a564bb2aef9fd677a8be
@@ -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)
@@ -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
@@ -81,6 +81,10 @@ module Z3
81
81
  BoolSort.new.new LowLevel.mk_fpa_is_zero(self)
82
82
  end
83
83
 
84
+ def nonzero?
85
+ Z3.And(~zero?, ~nan?)
86
+ end
87
+
84
88
  def max(other)
85
89
  FloatExpr.Max(self, other)
86
90
  end
@@ -4,6 +4,10 @@ module Z3
4
4
  IntExpr.Mod(self, other)
5
5
  end
6
6
 
7
+ def %(other)
8
+ IntExpr.Mod(self, other)
9
+ end
10
+
7
11
  def rem(other)
8
12
  IntExpr.Rem(self, other)
9
13
  end
@@ -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
@@ -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
@@ -39,6 +39,15 @@ module Z3
39
39
  expect([a == -10, b == -3, c == a.mod(b)]).to have_solution(c => 2)
40
40
  end
41
41
 
42
+ # It doesn't match Ruby on negative right side, but nobody does modulo a negative anyway
43
+ # Python Z3 API does the same thing
44
+ it "%" do
45
+ expect([a == 10, b == 3, c == a % b]).to have_solution(c => 1)
46
+ expect([a == 10, b == -3, c == a % b]).to have_solution(c => 1)
47
+ expect([a == -10, b == 3, c == a % b]).to have_solution(c => 2)
48
+ expect([a == -10, b == -3, c == a % b]).to have_solution(c => 2)
49
+ end
50
+
42
51
  it "==" do
43
52
  expect([a == 2, b == 2, x == (a == b)]).to have_solution(x => true)
44
53
  expect([a == 2, b == 3, x == (a == b)]).to have_solution(x => false)
@@ -81,6 +90,36 @@ module Z3
81
90
  expect([a == 3, b == -a]).to have_solution(b => -3)
82
91
  end
83
92
 
93
+ it "zero?" do
94
+ expect([a == 0, x == a.zero?]).to have_solution(x => true)
95
+ expect([a == 100, x == a.zero?]).to have_solution(x => false)
96
+ expect([a == -200, x == a.zero?]).to have_solution(x => false)
97
+ end
98
+
99
+ it "nonzero?" do
100
+ expect([a == 0, x == a.nonzero?]).to have_solution(x => false)
101
+ expect([a == 100, x == a.nonzero?]).to have_solution(x => true)
102
+ expect([a == -200, x == a.nonzero?]).to have_solution(x => true)
103
+ end
104
+
105
+ it "positive?" do
106
+ expect([a == 0, x == a.positive?]).to have_solution(x => false)
107
+ expect([a == 100, x == a.positive?]).to have_solution(x => true)
108
+ expect([a == -200, x == a.positive?]).to have_solution(x => false)
109
+ end
110
+
111
+ it "negative?" do
112
+ expect([a == 0, x == a.negative?]).to have_solution(x => false)
113
+ expect([a == 100, x == a.negative?]).to have_solution(x => false)
114
+ expect([a == -200, x == a.negative?]).to have_solution(x => true)
115
+ end
116
+
117
+ it "abs" do
118
+ expect([a == 3, b == 2, c == (a - b).abs]).to have_solution(c => 1)
119
+ expect([a == 2, b == 3, c == (a - b).abs]).to have_solution(c => 1)
120
+ expect([a == 2, b == 2, c == (a - b).abs]).to have_solution(c => 0)
121
+ end
122
+
84
123
  it "simplify" do
85
124
  a = Z3.Const(5)
86
125
  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
@@ -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.20221020
4
+ version: 0.0.20230311
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Wegrzanowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-20 00:00:00.000000000 Z
11
+ date: 2023-03-11 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
@@ -357,7 +358,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
357
358
  version: '0'
358
359
  requirements:
359
360
  - z3 library (4.8+)
360
- rubygems_version: 3.3.7
361
+ rubygems_version: 3.3.26
361
362
  signing_key:
362
363
  specification_version: 4
363
364
  summary: Z3 Constraint Solver