z3 0.0.20220203 → 0.0.20220828

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: d7d242f957aeaacf005f52841ebc5eaa0b1ecd507cc6d1f80c6ca0f9d2303813
4
- data.tar.gz: 926ecb103b3a90f45defb57e90610eeb6b67f160a0f2b5bf64ed448df26e87d7
3
+ metadata.gz: 94779e8d2fdaee7a213640164c8113fee96135e5247d9dc161d02dae87da6e94
4
+ data.tar.gz: d11a1326c693216fe857b436460d63176b466cc8b9927378940fc5543506a02e
5
5
  SHA512:
6
- metadata.gz: 76e3973739810e87ae9fed8765c9a8e7c96efac5a02c100cacfc8a3087f005ac6d866a251defe84a5f79dcec9991a98adef58ea64eb57ede5923a54e41f5a262
7
- data.tar.gz: e1c805dc4185374113cb639133d4546df210ab9ceb7dd65be2dd6a82ddfb9d1930f31f7d9a6830fa0a82c217c6eeec3a04a4f720e9ab70fcb48dad3d21824387
6
+ metadata.gz: 5282dc961ae5f4f9dbe08c3f0e225ad3e8849522d5f0f7de7deaa233463479197498c91327dc8e6455478ee4469cbec660f6c278a45b09218a45e3d3d8f5736d
7
+ data.tar.gz: b884d26fb02ec6714d5ff54e862cbe5dccec2853b4ebcdda84410ccda65fc7693428e163b390a7d71b741a538d5ef9e0c5aac365d8709ff301e0344bb7bd8880
@@ -49,11 +49,31 @@ module Z3
49
49
  end
50
50
 
51
51
  def /(other)
52
- raise "Use signed_div or unsigned_div"
52
+ raise Z3::Exception, "Use signed_div or unsigned_div"
53
+ end
54
+
55
+ def signed_div(other)
56
+ BitvecExpr.SignedDiv(self, other)
57
+ end
58
+
59
+ def unsigned_div(other)
60
+ BitvecExpr.UnsignedDiv(self, other)
53
61
  end
54
62
 
55
63
  def %(other)
56
- raise "Use signed_mod or signed_rem or unsigned_rem"
64
+ raise Z3::Exception, "Use signed_mod or signed_rem or unsigned_rem"
65
+ end
66
+
67
+ def signed_mod(other)
68
+ BitvecExpr.SignedMod(self, other)
69
+ end
70
+
71
+ def signed_rem(other)
72
+ BitvecExpr.SignedRem(self, other)
73
+ end
74
+
75
+ def unsigned_rem(other)
76
+ BitvecExpr.UnsignedRem(self, other)
57
77
  end
58
78
 
59
79
  def rotate_left(num)
@@ -257,6 +277,31 @@ module Z3
257
277
  a.sort.new(LowLevel.mk_bvshl(a, b))
258
278
  end
259
279
 
280
+ def SignedDiv(a, b)
281
+ a, b = coerce_to_same_bv_sort(a, b)
282
+ a.sort.new(LowLevel.mk_bvsdiv(a, b))
283
+ end
284
+
285
+ def UnsignedDiv(a, b)
286
+ a, b = coerce_to_same_bv_sort(a, b)
287
+ a.sort.new(LowLevel.mk_bvudiv(a, b))
288
+ end
289
+
290
+ def SignedMod(a, b)
291
+ a, b = coerce_to_same_bv_sort(a, b)
292
+ a.sort.new(LowLevel.mk_bvsmod(a, b))
293
+ end
294
+
295
+ def SignedRem(a, b)
296
+ a, b = coerce_to_same_bv_sort(a, b)
297
+ a.sort.new(LowLevel.mk_bvsrem(a, b))
298
+ end
299
+
300
+ def UnsignedRem(a, b)
301
+ a, b = coerce_to_same_bv_sort(a, b)
302
+ a.sort.new(LowLevel.mk_bvurem(a, b))
303
+ end
304
+
260
305
  def Xnor(*args)
261
306
  args = coerce_to_same_bv_sort(*args)
262
307
  args.inject do |a,b|
data/lib/z3/optimize.rb CHANGED
@@ -23,9 +23,9 @@ module Z3
23
23
  LowLevel.optimize_assert(self, ast)
24
24
  end
25
25
 
26
- def assert_soft(ast)
26
+ def assert_soft(ast, weight = "1", id = nil)
27
27
  reset_model!
28
- LowLevel.optimize_assert_soft(self, ast)
28
+ LowLevel.optimize_assert_soft(self, ast, weight, id)
29
29
  end
30
30
 
31
31
  def check(*args)
@@ -4,7 +4,7 @@ require "ffi"
4
4
  module Z3
5
5
  module VeryLowLevel
6
6
  extend FFI::Library
7
- ffi_lib ["libz3.so.4.8", "libz3.so", "z3"]
7
+ ffi_lib ["libz3.so.4.8", "libz3.so", "z3", "libz3"]
8
8
 
9
9
  class << self
10
10
  # Aliases defined just to make APIs below look nicer
@@ -36,6 +36,19 @@ module Z3
36
36
  expect([a == 30, b == 42, c == (a * b)]).to have_solution(c => 236)
37
37
  end
38
38
 
39
+ it "/" do
40
+ expect{ a / b }.to raise_error(Z3::Exception)
41
+ expect([a == 200, b == 20, c == a.unsigned_div(b)]).to have_solution(c => 10)
42
+ expect([a == 200, b == 20, c == a.signed_div(b)]).to have_solution(c => 254)
43
+ end
44
+
45
+ it "%" do
46
+ expect{ a % b }.to raise_error(Z3::Exception)
47
+ expect([a == 200, b == 20, c == a.signed_mod(b)]).to have_solution(c => 4)
48
+ expect([a == 200, b == 20, c == a.signed_rem(b)]).to have_solution(c => 240)
49
+ expect([a == 200, b == 20, c == a.unsigned_rem(b)]).to have_solution(c => 0)
50
+ end
51
+
39
52
  it "&" do
40
53
  expect([a == 50, b == 27, c == (a & b)]).to have_solution(c => 18)
41
54
  end
@@ -1,21 +1,5 @@
1
- # There are multiple solutions, so this test is nondeterministic
2
- # This is what Z3 4.8.13 returns
3
1
  describe "ABC Path" do
4
2
  it do
5
- expect("abc_path").to have_output <<'EOF'
6
- C O R D F B U
7
-
8
- N o-n l-k-j L
9
- | |/ |
10
- G p m g-f i I
11
- | x /
12
- Q q-r e h a H
13
- / | |
14
- V s v d-c-b S
15
- x \
16
- T u-t w-x-y X
17
-
18
- J P M W K Y E
19
- EOF
3
+ expect("abc_path").to have_output_matching_saved_example
20
4
  end
21
5
  end
@@ -0,0 +1,13 @@
1
+ C O R D F B U
2
+
3
+ N o-n l-k-j L
4
+ | |/ |
5
+ G p m g-f i I
6
+ | x /
7
+ Q q-r e h a H
8
+ / | |
9
+ V s v d-c-b S
10
+ x \
11
+ T u-t w-x-y X
12
+
13
+ J P M W K Y E
@@ -0,0 +1,13 @@
1
+ C O R D F B U
2
+
3
+ N o-n l-k-j L
4
+ | |/ |
5
+ G p m g-f i I
6
+ | x /
7
+ Q q-r e h b H
8
+ / | /|
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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
+ w: 3,2 -> 2,0
64
+
65
+ State 9:
66
+ wwww
67
+ xbxb
68
+ .bb.
69
+ x.x.
70
+ b: 1,1 -> 3,2
71
+
72
+ State 10:
73
+ wwww
74
+ x.xb
75
+ .bbb
76
+ x.x.
77
+ b: 1,2 -> 3,3
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
@@ -0,0 +1,90 @@
1
+ Solved
2
+ State 0:
3
+ bbb.
4
+ xbxw
5
+ ..ww
6
+ x.xw
7
+ w: 2,2 -> 3,0
8
+
9
+ State 1:
10
+ bbbw
11
+ xbxw
12
+ ...w
13
+ x.xw
14
+ b: 1,0 -> 2,2
15
+
16
+ State 2:
17
+ b.bw
18
+ xbxw
19
+ ..bw
20
+ x.xw
21
+ w: 3,1 -> 1,0
22
+
23
+ State 3:
24
+ bwbw
25
+ xbx.
26
+ ..bw
27
+ x.xw
28
+ b: 0,0 -> 1,2
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
@@ -0,0 +1,90 @@
1
+ Solved
2
+ State 0:
3
+ bbb.
4
+ xbxw
5
+ ..ww
6
+ x.xw
7
+ w: 2,2 -> 3,0
8
+
9
+ State 1:
10
+ bbbw
11
+ xbxw
12
+ ...w
13
+ x.xw
14
+ b: 1,0 -> 2,2
15
+
16
+ State 2:
17
+ b.bw
18
+ xbxw
19
+ ..bw
20
+ x.xw
21
+ b: 0,0 -> 1,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
@@ -1,23 +1,5 @@
1
1
  describe "Futoshiki" do
2
2
  it do
3
- expect("futoshiki").to have_output <<EOF
4
- 2 5 3 4 1 7 6<9 8
5
-
6
- 1 9 7 2 8 5 3<4<6
7
- _
8
- 7<8 2 5>3>1 4<6 9
9
- ^
10
- 8 1<5<9 7 6 2 3 4
11
- ^
12
- 4 7>6>1 9 2 5 8 3
13
- ^ ^ ^
14
- 6 4 1 8 2 3 9 5 7
15
- _ _ ^ _ _
16
- 9 3 4 6>5 8 7 1 2
17
- _
18
- 5 2 9>3 6>4 8>7 1
19
- _
20
- 3 6 8>7>4 9 1<2 5
21
- EOF
3
+ expect("futoshiki").to have_output_matching_saved_example
22
4
  end
23
5
  end
@@ -1,98 +1,5 @@
1
- # There are multiple solutions, so this test is nondeterministic
2
- # This is what Z3 4.8.13 returns
3
1
  describe "Knights Swap Puzzle" do
4
2
  it do
5
- expect("knights_puzzle").to have_output <<EOF
6
- Solved
7
- State 0:
8
- bbb.
9
- xbxw
10
- ..ww
11
- x.xw
12
- w: 2,2 -> 3,0
13
-
14
- State 1:
15
- bbbw
16
- xbxw
17
- ...w
18
- x.xw
19
- b: 1,0 -> 2,2
20
-
21
- State 2:
22
- b.bw
23
- xbxw
24
- ..bw
25
- x.xw
26
- w: 3,1 -> 1,0
27
-
28
- State 3:
29
- bwbw
30
- xbx.
31
- ..bw
32
- x.xw
33
- b: 0,0 -> 1,2
34
-
35
- State 4:
36
- .wbw
37
- xbx.
38
- .bbw
39
- x.xw
40
- b: 1,2 -> 3,1
41
-
42
- State 5:
43
- .wbw
44
- xbxb
45
- ..bw
46
- x.xw
47
- w: 3,3 -> 1,2
48
-
49
- State 6:
50
- .wbw
51
- xbxb
52
- .wbw
53
- x.x.
54
- w: 1,2 -> 0,0
55
-
56
- State 7:
57
- wwbw
58
- xbxb
59
- ..bw
60
- x.x.
61
- b: 2,0 -> 1,2
62
-
63
- State 8:
64
- ww.w
65
- xbxb
66
- .bbw
67
- x.x.
68
- w: 3,2 -> 2,0
69
-
70
- State 9:
71
- wwww
72
- xbxb
73
- .bb.
74
- x.x.
75
- b: 1,1 -> 3,2
76
-
77
- State 10:
78
- wwww
79
- x.xb
80
- .bbb
81
- x.x.
82
- b: 1,2 -> 3,3
83
-
84
- State 11:
85
- wwww
86
- x.xb
87
- ..bb
88
- x.xb
89
- w: 3,0 -> 1,1
90
-
91
- State 12:
92
- www.
93
- xwxb
94
- ..bb
95
- x.xb
96
- EOF
3
+ expect("knights_puzzle").to have_output_matching_saved_example
97
4
  end
98
5
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Disabled as it crashes on Z3 4.8.13
4
4
  module Z3
5
- xdescribe Optimize do
5
+ describe Optimize do
6
6
  let(:optimize) { Optimize.new }
7
7
  let(:a) { Z3.Int("a") }
8
8
  let(:b) { Z3.Int("b") }
@@ -34,6 +34,15 @@ module Z3
34
34
  ])
35
35
  end
36
36
 
37
+ it "#assert_soft" do
38
+ optimize.assert_soft a > 0
39
+ optimize.assert_soft a < 0
40
+ optimize.assert_soft a < 10
41
+ optimize.maximize a
42
+ expect(optimize).to be_satisfiable
43
+ expect(optimize.model[a].to_i).to eq 9
44
+ end
45
+
37
46
  it "#statistics" do
38
47
  optimize.assert a + b == 4
39
48
  optimize.assert b >= 2
@@ -42,16 +51,6 @@ module Z3
42
51
  expect(stats.keys).to match_array(["rlimit count", "max memory", "memory", "num allocs"])
43
52
  end
44
53
 
45
- # This is a very simple example of unknown satisfiablity
46
- # so we might need more complex one in the future
47
- # Unlike Z3::Solver, this is unknown even 4.6.0
48
- it "unknown satisfiability" do
49
- optimize.assert a**3 == a
50
- expect(optimize.check).to eq(:unknown)
51
- expect{optimize.satisfiable?}.to raise_error("Satisfiability unknown")
52
- expect{optimize.unsatisfiable?}.to raise_error("Satisfiability unknown")
53
- end
54
-
55
54
  it "unknown satisfiability" do
56
55
  optimize.assert a**a == a
57
56
  expect(optimize.check).to eq(:unknown)
data/spec/spec_helper.rb CHANGED
@@ -38,6 +38,20 @@ RSpec::Matchers.define :have_output do |expected|
38
38
  end
39
39
  end
40
40
 
41
+ RSpec::Matchers.define :have_output_matching_saved_example do
42
+ match do |file_name|
43
+ executable_path = "#{__dir__}/../examples/#{file_name}"
44
+ actual = IO.popen("ruby -r./spec/coverage_helper #{executable_path}").read
45
+ @actual = actual.gsub(/ *$/, "")
46
+ @expected = Pathname("spec/integration/examples").glob("#{file_name}-*.txt").map(&:read).map{|o| o.gsub(/ *$/, "")}
47
+ @expected.include?(@actual)
48
+ end
49
+
50
+ failure_message do |actual|
51
+ "Expected one of saved examples, but got:\n#{@actual}"
52
+ end
53
+ end
54
+
41
55
  RSpec::Matchers.define :have_output_no_color do |expected|
42
56
  match do |file_name|
43
57
  executable_path = "#{__dir__}/../examples/#{file_name}"
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.20220203
4
+ version: 0.0.20220828
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-02-03 00:00:00.000000000 Z
11
+ date: 2022-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -280,6 +280,13 @@ files:
280
280
  - spec/integration/dominion_spec.rb
281
281
  - spec/integration/dominosa_spec.rb
282
282
  - spec/integration/eulero_spec.rb
283
+ - spec/integration/examples/abc_path-1.txt
284
+ - spec/integration/examples/abc_path-2.txt
285
+ - spec/integration/examples/futoshiki-1.txt
286
+ - spec/integration/examples/futoshiki-2.txt
287
+ - spec/integration/examples/knights_puzzle-1.txt
288
+ - spec/integration/examples/knights_puzzle-2.txt
289
+ - spec/integration/examples/knights_puzzle-3.txt
283
290
  - spec/integration/four_hackers_puzzle_spec.rb
284
291
  - spec/integration/futoshiki_spec.rb
285
292
  - spec/integration/geometry_problem_spec.rb
@@ -349,7 +356,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
349
356
  version: '0'
350
357
  requirements:
351
358
  - z3 library (4.8+)
352
- rubygems_version: 3.1.2
359
+ rubygems_version: 3.3.7
353
360
  signing_key:
354
361
  specification_version: 4
355
362
  summary: Z3 Constraint Solver