z3 0.0.20160221 → 0.0.20160323

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -1
  3. data/examples/algebra_problems +24 -24
  4. data/examples/basic_int_math +8 -8
  5. data/examples/basic_logic +8 -8
  6. data/examples/bit_tricks +161 -0
  7. data/examples/bridges_solver +1 -1
  8. data/examples/clogic_puzzle_solver +135 -0
  9. data/examples/four_hackers_puzzle +194 -0
  10. data/examples/geometry_problem +11 -11
  11. data/examples/kakuro_solver +3 -3
  12. data/examples/kinematics_problems +37 -37
  13. data/examples/letter_connections_solver +11 -11
  14. data/examples/light_up_solver +5 -5
  15. data/examples/minisudoku_solver +4 -4
  16. data/examples/selfref_solver +35 -35
  17. data/examples/sudoku_solver +4 -4
  18. data/examples/verbal_arithmetic +2 -2
  19. data/lib/z3/exception.rb +25 -0
  20. data/lib/z3/func_decl.rb +7 -7
  21. data/lib/z3/interface.rb +255 -0
  22. data/lib/z3/low_level.rb +4 -6
  23. data/lib/z3/low_level_auto.rb +1551 -1547
  24. data/lib/z3/model.rb +3 -2
  25. data/lib/z3/solver.rb +65 -54
  26. data/lib/z3/sort/bitvec_sort.rb +40 -0
  27. data/lib/z3/sort/bool_sort.rb +31 -0
  28. data/lib/z3/sort/int_sort.rb +21 -0
  29. data/lib/z3/sort/real_sort.rb +36 -0
  30. data/lib/z3/sort/sort.rb +76 -0
  31. data/lib/z3/value/arith_value.rb +53 -0
  32. data/lib/z3/value/bitvec_value.rb +67 -0
  33. data/lib/z3/value/bool_value.rb +29 -0
  34. data/lib/z3/value/int_value.rb +7 -0
  35. data/lib/z3/value/real_value.rb +7 -0
  36. data/lib/z3/value/value.rb +48 -0
  37. data/lib/z3/very_low_level.rb +28 -45
  38. data/lib/z3/very_low_level_auto.rb +518 -516
  39. data/lib/z3.rb +23 -33
  40. data/spec/integration/bit_tricks_spec.rb +21 -0
  41. data/spec/model_spec.rb +9 -9
  42. data/spec/solver_spec.rb +2 -2
  43. data/spec/sort_spec.rb +38 -13
  44. data/spec/{ast_spec.rb → value_spec.rb} +60 -57
  45. metadata +21 -6
  46. data/lib/z3/ast.rb +0 -302
  47. data/lib/z3/sort.rb +0 -33
  48. /data/spec/integration/{bagic_int_math_spec.rb → basic_int_math_spec.rb} +0 -0
data/lib/z3.rb CHANGED
@@ -1,43 +1,33 @@
1
1
  module Z3
2
- class <<self
3
- def version
4
- Z3::LowLevel.get_version.join(".")
5
- end
6
-
7
- def set_param(k,v)
8
- Z3::LowLevel.global_param_set(k,v)
9
- end
10
- end
11
- end
12
-
13
- class Z3::Exception < StandardError
14
2
  end
15
3
 
4
+ # Low level base classes, do not use directly
16
5
  require_relative "z3/very_low_level"
6
+ require_relative "z3/very_low_level_auto"
17
7
  require_relative "z3/low_level"
8
+ require_relative "z3/low_level_auto"
9
+
10
+ # Classes
18
11
  require_relative "z3/context"
19
12
  require_relative "z3/solver"
20
- require_relative "z3/sort"
21
- require_relative "z3/ast"
22
13
  require_relative "z3/model"
14
+ require_relative "z3/exception"
23
15
  require_relative "z3/func_decl"
24
16
 
25
- Z3::LowLevel.set_error_handler do |ctx, error|
26
- error_codes_enum = %W[
27
- Z3_OK
28
- Z3_SORT_ERROR
29
- Z3_IOB
30
- Z3_INVALID_ARG
31
- Z3_PARSER_ERROR
32
- Z3_NO_PARSER
33
- Z3_INVALID_PATTERN
34
- Z3_MEMOUT_FAIL
35
- Z3_FILE_ACCESS_ERROR
36
- Z3_INTERNAL_FATAL
37
- Z3_INVALID_USAGE
38
- Z3_DEC_REF_ERROR
39
- Z3_EXCEPTION
40
- ]
41
- error = error_codes_enum[error] || error
42
- raise Z3::Exception, "Z3 library failed with error #{error}"
43
- end
17
+ # Sorts
18
+ require_relative "z3/sort/sort"
19
+ require_relative "z3/sort/int_sort"
20
+ require_relative "z3/sort/real_sort"
21
+ require_relative "z3/sort/bool_sort"
22
+ require_relative "z3/sort/bitvec_sort"
23
+
24
+ # ASTs
25
+ require_relative "z3/value/value"
26
+ require_relative "z3/value/arith_value"
27
+ require_relative "z3/value/int_value"
28
+ require_relative "z3/value/real_value"
29
+ require_relative "z3/value/bool_value"
30
+ require_relative "z3/value/bitvec_value"
31
+
32
+ # Python-style interface
33
+ require_relative "z3/interface"
@@ -0,0 +1,21 @@
1
+ describe "Bit Tricks" do
2
+ let(:executable) { "#{__dir__}/../../examples/bit_tricks" }
3
+ it "can validate bit tricks" do
4
+ expect(IO.popen(executable).read.gsub(/ *$/, "")).to eq <<EOF
5
+ Validating sign trick:
6
+ Proven
7
+ Validating sign trick:
8
+ Proven
9
+ Validating abs without branching, version 1
10
+ Proven
11
+ Validating abs without branching, version 2
12
+ Proven
13
+ Validating min without branching
14
+ Proven
15
+ Validating max without branching
16
+ Proven
17
+ Validating is power of two
18
+ Proven
19
+ EOF
20
+ end
21
+ end
data/spec/model_spec.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  # Solver and Model specs are codependent, so half of functionality of each is tested in other class's tests
2
2
  describe Z3::Model do
3
3
  let(:solver) { Z3::Solver.new }
4
- let(:a) { Z3::Ast.int("a") }
5
- let(:b) { Z3::Ast.int("b") }
6
- let(:c) { Z3::Ast.int("c") }
4
+ let(:a) { Z3.Int("a") }
5
+ let(:b) { Z3.Int("b") }
6
+ let(:c) { Z3.Int("c") }
7
7
  let(:model) { solver.model }
8
8
 
9
9
  it "knows how many variables are in the model" do
@@ -19,12 +19,12 @@ describe Z3::Model do
19
19
  solver.assert(a == 2)
20
20
  solver.assert(b == a+2)
21
21
  expect(solver.check).to eq(:sat)
22
- expect(model.model_eval(a).inspect).to eq("Z3::Ast<2 :: Int>")
23
- expect(model.model_eval(b).inspect).to eq("Z3::Ast<4 :: Int>")
24
- expect(model.model_eval(c).inspect).to eq("Z3::Ast<c :: Int>")
25
- expect(model.model_eval(a, true).inspect).to eq("Z3::Ast<2 :: Int>")
26
- expect(model.model_eval(b, true).inspect).to eq("Z3::Ast<4 :: Int>")
27
- expect(model.model_eval(c, true).inspect).to eq("Z3::Ast<0 :: Int>")
22
+ expect(model.model_eval(a).to_s).to eq("2")
23
+ expect(model.model_eval(b).to_s).to eq("4")
24
+ expect(model.model_eval(c).to_s).to eq("c")
25
+ expect(model.model_eval(a, true).to_s).to eq("2")
26
+ expect(model.model_eval(b, true).to_s).to eq("4")
27
+ expect(model.model_eval(c, true).to_s).to eq("0")
28
28
  end
29
29
 
30
30
  it "#to_s" do
data/spec/solver_spec.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # Solver and Model specs are codependent, so half of functionality of each is tested in other class's tests
2
2
  describe Z3::Solver do
3
3
  let(:solver) { Z3::Solver.new }
4
- let(:a) { Z3::Ast.int("a") }
5
- let(:b) { Z3::Ast.int("b") }
4
+ let(:a) { Z3.Int("a") }
5
+ let(:b) { Z3.Int("b") }
6
6
 
7
7
  it "basic functionality" do
8
8
  solver.assert(a == b)
data/spec/sort_spec.rb CHANGED
@@ -1,22 +1,47 @@
1
1
  describe Z3::Sort do
2
- it "#bool creates Bool sort" do
3
- expect(Z3::Sort.bool.to_s).to eq("Bool")
4
- expect(Z3::Sort.bool.inspect).to eq("Z3::Sort<Bool>")
2
+ let(:bool_sort) { Z3::BoolSort.new }
3
+ let(:int_sort) { Z3::IntSort.new }
4
+ let(:real_sort) { Z3::RealSort.new }
5
+ let(:bv8_sort) { Z3::BitvecSort.new(8) }
6
+ let(:bv32_sort) { Z3::BitvecSort.new(32) }
7
+
8
+ let(:sorts) { [bool_sort, int_sort, real_sort, bv8_sort, bv32_sort] }
9
+
10
+ it "can't instantiate Sort abstract superclass" do
11
+ expect{ Z3::Sort.new }.to raise_error(NoMethodError)
5
12
  end
6
13
 
7
- it "#int creates Int sort" do
8
- expect(Z3::Sort.int.to_s).to eq("Int")
9
- expect(Z3::Sort.int.inspect).to eq("Z3::Sort<Int>")
14
+ it "#to_s" do
15
+ expect(bool_sort.to_s).to eq("Bool")
16
+ expect( int_sort.to_s).to eq("Int")
17
+ expect(real_sort.to_s).to eq("Real")
18
+ expect( bv8_sort.to_s).to eq("Bitvec(8)")
19
+ expect(bv32_sort.to_s).to eq("Bitvec(32)")
10
20
  end
11
21
 
12
- it "#real creates Real sort" do
13
- expect(Z3::Sort.real.to_s).to eq("Real")
14
- expect(Z3::Sort.real.inspect).to eq("Z3::Sort<Real>")
22
+ it "#inspect" do
23
+ expect(bool_sort.inspect).to eq("BoolSort")
24
+ expect( int_sort.inspect).to eq("IntSort")
25
+ expect(real_sort.inspect).to eq("RealSort")
26
+ expect( bv8_sort.inspect).to eq("BitvecSort(8)")
27
+ expect(bv32_sort.inspect).to eq("BitvecSort(32)")
15
28
  end
16
29
 
17
- it "supports ==" do
18
- expect(Z3::Sort.bool).to eq(Z3::Sort.bool)
19
- expect(Z3::Sort.int).to eq(Z3::Sort.int)
20
- expect(Z3::Sort.bool).to_not eq(Z3::Sort.int)
30
+ describe "==" do
31
+ it "all Sorts are value-objects" do
32
+ expect(bool_sort).to eq( Z3::BoolSort.new )
33
+ expect( int_sort).to eq( Z3::IntSort.new )
34
+ expect(real_sort).to eq( Z3::RealSort.new )
35
+ expect( bv8_sort).to eq( Z3::BitvecSort.new(8) )
36
+ expect(bv32_sort).to eq( Z3::BitvecSort.new(32) )
37
+ end
38
+
39
+ it "is == to itself and no other sort" do
40
+ sorts.each do |sort1|
41
+ sorts.each do |sort2|
42
+ expect(sort1 == sort2).to eq(sort1.to_s == sort2.to_s)
43
+ end
44
+ end
45
+ end
21
46
  end
22
47
  end
@@ -1,15 +1,15 @@
1
- describe Z3::Ast do
2
- let(:a) { Z3::Ast.int("a") }
3
- let(:b) { Z3::Ast.int("b") }
4
- let(:c) { Z3::Ast.bool("c") }
5
- let(:d) { Z3::Ast.bool("d") }
6
- let(:e) { Z3::Ast.real("e") }
7
- let(:f) { Z3::Ast.real("f") }
1
+ describe Z3::Value do
2
+ let(:a) { Z3.Int("a") }
3
+ let(:b) { Z3.Int("b") }
4
+ let(:c) { Z3.Bool("c") }
5
+ let(:d) { Z3.Bool("d") }
6
+ let(:e) { Z3.Real("e") }
7
+ let(:f) { Z3.Real("f") }
8
8
 
9
9
  it "#sort returns Sort object" do
10
- expect(a.sort).to eq(Z3::Sort.int)
11
- expect(c.sort).to eq(Z3::Sort.bool)
12
- expect(e.sort).to eq(Z3::Sort.real)
10
+ expect(a.sort).to eq(Z3::IntSort.new)
11
+ expect(c.sort).to eq(Z3::BoolSort.new)
12
+ expect(e.sort).to eq(Z3::RealSort.new)
13
13
  end
14
14
 
15
15
  it "#to_s" do
@@ -17,8 +17,8 @@ describe Z3::Ast do
17
17
  end
18
18
 
19
19
  it "#inspect" do
20
- expect(a.inspect).to eq("Z3::Ast<a :: Int>")
21
- expect((e+f).inspect).to eq("Z3::Ast<(+ e f) :: Real>")
20
+ expect(a.inspect).to eq("Value<a :: Int>")
21
+ expect((e+f).inspect).to eq("Value<(+ e f) :: Real>")
22
22
  end
23
23
 
24
24
  describe "#~" do
@@ -27,8 +27,8 @@ describe Z3::Ast do
27
27
  end
28
28
 
29
29
  it "raises exception if type cast is not possible" do
30
- expect{~a}.to raise_error(Z3::Exception)
31
- expect{~e}.to raise_error(Z3::Exception)
30
+ expect{~a}.to raise_error(NoMethodError)
31
+ expect{~e}.to raise_error(NoMethodError)
32
32
  end
33
33
  end
34
34
 
@@ -38,12 +38,12 @@ describe Z3::Ast do
38
38
  end
39
39
 
40
40
  it "raises exception if type cast is not possible" do
41
- expect{a&b}.to raise_error(Z3::Exception)
42
- expect{e&f}.to raise_error(Z3::Exception)
43
- expect{a&c}.to raise_error(Z3::Exception)
44
- expect{e&c}.to raise_error(Z3::Exception)
45
- expect{c&a}.to raise_error(Z3::Exception)
46
- expect{c&e}.to raise_error(Z3::Exception)
41
+ expect{a&b}.to raise_error(NoMethodError)
42
+ expect{e&f}.to raise_error(NoMethodError)
43
+ expect{a&c}.to raise_error(NoMethodError)
44
+ expect{e&c}.to raise_error(NoMethodError)
45
+ expect{c&a}.to raise_error(ArgumentError)
46
+ expect{c&e}.to raise_error(ArgumentError)
47
47
  end
48
48
  end
49
49
 
@@ -53,12 +53,12 @@ describe Z3::Ast do
53
53
  end
54
54
 
55
55
  it "raises exception if type cast is not possible" do
56
- expect{a|b}.to raise_error(Z3::Exception)
57
- expect{e|f}.to raise_error(Z3::Exception)
58
- expect{a|c}.to raise_error(Z3::Exception)
59
- expect{e|c}.to raise_error(Z3::Exception)
60
- expect{c|a}.to raise_error(Z3::Exception)
61
- expect{c|e}.to raise_error(Z3::Exception)
56
+ expect{a|b}.to raise_error(NoMethodError)
57
+ expect{e|f}.to raise_error(NoMethodError)
58
+ expect{a|c}.to raise_error(NoMethodError)
59
+ expect{e|c}.to raise_error(NoMethodError)
60
+ expect{c|a}.to raise_error(ArgumentError)
61
+ expect{c|e}.to raise_error(ArgumentError)
62
62
  end
63
63
  end
64
64
 
@@ -83,17 +83,20 @@ describe Z3::Ast do
83
83
  end
84
84
 
85
85
  it "raises exception if type cast is not possible" do
86
- expect{a.send op, c}.to raise_error(Z3::Exception)
87
- expect{c.send op, a}.to raise_error(Z3::Exception)
88
- expect{e.send op, c}.to raise_error(Z3::Exception)
89
- expect{c.send op, e}.to raise_error(Z3::Exception)
90
- expect{c.send op, d}.to raise_error(Z3::Exception)
91
- expect{a.send op, true}.to raise_error(Z3::Exception)
92
- expect{c.send op, true}.to raise_error(Z3::Exception)
93
- expect{e.send op, true}.to raise_error(Z3::Exception)
94
- expect{a.send op, false}.to raise_error(Z3::Exception)
95
- expect{c.send op, false}.to raise_error(Z3::Exception)
96
- expect{e.send op, false}.to raise_error(Z3::Exception)
86
+ # Int/Real has #>= #+ etc. but they don't like these arguments
87
+ expect{a.send op, c}.to raise_error(ArgumentError)
88
+ expect{e.send op, c}.to raise_error(ArgumentError)
89
+ expect{a.send op, true}.to raise_error(ArgumentError)
90
+ expect{a.send op, false}.to raise_error(ArgumentError)
91
+ expect{e.send op, true}.to raise_error(ArgumentError)
92
+ expect{e.send op, false}.to raise_error(ArgumentError)
93
+
94
+ # Bool doesn't have #>= #+ etc.
95
+ expect{c.send op, a}.to raise_error(NoMethodError)
96
+ expect{c.send op, d}.to raise_error(NoMethodError)
97
+ expect{c.send op, e}.to raise_error(NoMethodError)
98
+ expect{c.send op, true}.to raise_error(NoMethodError)
99
+ expect{c.send op, false}.to raise_error(NoMethodError)
97
100
  end
98
101
  end
99
102
  end
@@ -121,16 +124,16 @@ describe Z3::Ast do
121
124
  end
122
125
 
123
126
  it "raises exception if type cast is not possible" do
124
- expect{a == c}.to raise_error(Z3::Exception)
125
- expect{e == c}.to raise_error(Z3::Exception)
126
- expect{a == true}.to raise_error(Z3::Exception)
127
- expect{e == true}.to raise_error(Z3::Exception)
128
- # expect{true == a}.to raise_error(Z3::Exception)
129
- # expect{true == e}.to raise_error(Z3::Exception)
130
- expect{c == 42}.to raise_error(Z3::Exception)
131
- expect{c == 42.5}.to raise_error(Z3::Exception)
132
- expect{42 == c}.to raise_error(Z3::Exception)
133
- expect{42.5 == c}.to raise_error(Z3::Exception)
127
+ expect{a == c}.to raise_error(ArgumentError)
128
+ expect{e == c}.to raise_error(ArgumentError)
129
+ expect{a == true}.to raise_error(ArgumentError)
130
+ expect{e == true}.to raise_error(ArgumentError)
131
+ # expect{true == a}.to raise_error(ArgumentError)
132
+ # expect{true == e}.to raise_error(ArgumentError)
133
+ expect{c == 42}.to raise_error(ArgumentError)
134
+ expect{c == 42.5}.to raise_error(ArgumentError)
135
+ expect{42 == c}.to raise_error(ArgumentError)
136
+ expect{42.5 == c}.to raise_error(ArgumentError)
134
137
  end
135
138
  end
136
139
 
@@ -157,16 +160,16 @@ describe Z3::Ast do
157
160
  end
158
161
 
159
162
  it "raises exception if type cast is not possible" do
160
- expect{a != c}.to raise_error(Z3::Exception)
161
- expect{e != c}.to raise_error(Z3::Exception)
162
- expect{a != true}.to raise_error(Z3::Exception)
163
- expect{e != true}.to raise_error(Z3::Exception)
164
- # expect{true != a}.to raise_error(Z3::Exception)
165
- # expect{true != e}.to raise_error(Z3::Exception)
166
- expect{c != 42}.to raise_error(Z3::Exception)
167
- expect{c != 42.5}.to raise_error(Z3::Exception)
168
- # expect{42 != c}.to raise_error(Z3::Exception)
169
- # expect{42.5 != c}.to raise_error(Z3::Exception)
163
+ expect{a != c}.to raise_error(ArgumentError)
164
+ expect{e != c}.to raise_error(ArgumentError)
165
+ expect{a != true}.to raise_error(ArgumentError)
166
+ expect{e != true}.to raise_error(ArgumentError)
167
+ # expect{true != a}.to raise_error(ArgumentError)
168
+ # expect{true != e}.to raise_error(ArgumentError)
169
+ expect{c != 42}.to raise_error(ArgumentError)
170
+ expect{c != 42.5}.to raise_error(ArgumentError)
171
+ # expect{42 != c}.to raise_error(ArgumentError)
172
+ # expect{42.5 != c}.to raise_error(ArgumentError)
170
173
  end
171
174
  end
172
175
  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.20160221
4
+ version: 0.0.20160323
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Wegrzanowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-21 00:00:00.000000000 Z
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -34,7 +34,10 @@ files:
34
34
  - examples/algebra_problems
35
35
  - examples/basic_int_math
36
36
  - examples/basic_logic
37
+ - examples/bit_tricks
37
38
  - examples/bridges_solver
39
+ - examples/clogic_puzzle_solver
40
+ - examples/four_hackers_puzzle
38
41
  - examples/geometry_problem
39
42
  - examples/kakuro_solver
40
43
  - examples/kinematics_problems
@@ -45,20 +48,31 @@ files:
45
48
  - examples/sudoku_solver
46
49
  - examples/verbal_arithmetic
47
50
  - lib/z3.rb
48
- - lib/z3/ast.rb
49
51
  - lib/z3/context.rb
52
+ - lib/z3/exception.rb
50
53
  - lib/z3/func_decl.rb
54
+ - lib/z3/interface.rb
51
55
  - lib/z3/low_level.rb
52
56
  - lib/z3/low_level_auto.rb
53
57
  - lib/z3/model.rb
54
58
  - lib/z3/solver.rb
55
- - lib/z3/sort.rb
59
+ - lib/z3/sort/bitvec_sort.rb
60
+ - lib/z3/sort/bool_sort.rb
61
+ - lib/z3/sort/int_sort.rb
62
+ - lib/z3/sort/real_sort.rb
63
+ - lib/z3/sort/sort.rb
64
+ - lib/z3/value/arith_value.rb
65
+ - lib/z3/value/bitvec_value.rb
66
+ - lib/z3/value/bool_value.rb
67
+ - lib/z3/value/int_value.rb
68
+ - lib/z3/value/real_value.rb
69
+ - lib/z3/value/value.rb
56
70
  - lib/z3/very_low_level.rb
57
71
  - lib/z3/very_low_level_auto.rb
58
- - spec/ast_spec.rb
59
72
  - spec/integration/algebra_problems_spec.rb
60
- - spec/integration/bagic_int_math_spec.rb
73
+ - spec/integration/basic_int_math_spec.rb
61
74
  - spec/integration/basic_logic_spec.rb
75
+ - spec/integration/bit_tricks_spec.rb
62
76
  - spec/integration/bridges_spec.rb
63
77
  - spec/integration/geometry_problem_spec.rb
64
78
  - spec/integration/kakuro_spec.rb
@@ -73,6 +87,7 @@ files:
73
87
  - spec/solver_spec.rb
74
88
  - spec/sort_spec.rb
75
89
  - spec/spec_helper.rb
90
+ - spec/value_spec.rb
76
91
  - spec/z3_spec.rb
77
92
  homepage: https://github.com/taw/z3
78
93
  licenses: