z3 0.0.20180616 → 0.0.20180624

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1a008f1afd884efa0e1dc6e9964e9f0be381bd77884a2526a07bff5efe64212
4
- data.tar.gz: d9379bd2084cb2e5222856da043d58e8ddd399cd86abdce01f0243464e2c6097
3
+ metadata.gz: 4c5b58b092ac3d83d29341b7286e74068497c6f3e59232b8b0e23373a5e2e4eb
4
+ data.tar.gz: 5dfef87be452768052ece356eb0cd52f0725556769937d0030b1403a9422946e
5
5
  SHA512:
6
- metadata.gz: 27b7f713bedcbe819207d9d120d081366a274d9f61c952625e7256d3c5f7d177ba6de6d55f31971c06e084fcefa80c52004cd271d7dc7163753daf61e156edcc
7
- data.tar.gz: a6f97e4b4916e5189a94bc439bc174342dae679fec6a246ac1a4c6c4245d7355b619cf922515913e41f5d9070b719c0a562ca9682d4d37fcbe6b1d6959c0033b
6
+ metadata.gz: 209cd2c1ced0b55bfad330f725b296075b14651af5d7faee5e236e920a9c48d8e65db30a7b31aabc0b4744aca2389aae8ea5db78815e9fbde5c8f9e379fba091
7
+ data.tar.gz: 43d4e8ec32e9a3eb433ea6a73b9e713ccd75182be3702be42ee2f60592a1c61a5a56ba0c0aca3711477fa7d6d5dfe85e21b5922701b38bdbef2882ce57569d71
data/lib/z3/hacks.rb ADDED
@@ -0,0 +1,92 @@
1
+ # This is going to slow down ruby, but the alternative is very inconsistent API
2
+ module EqualityHacks
3
+ def ==(other)
4
+ if other.is_a?(Z3::Expr)
5
+ return other == self
6
+ end
7
+ super
8
+ end
9
+
10
+ def !=(other)
11
+ if other.is_a?(Z3::Expr)
12
+ return other != self
13
+ end
14
+ super
15
+ end
16
+ end
17
+
18
+ module CompareHacks
19
+ def ==(other)
20
+ if other.is_a?(Z3::Expr)
21
+ raise ArgumentError.new unless other.respond_to?(:coerce)
22
+ a, b = other.coerce(self)
23
+ return a == b
24
+ end
25
+ super
26
+ end
27
+
28
+ def !=(other)
29
+ if other.is_a?(Z3::Expr)
30
+ raise ArgumentError.new unless other.respond_to?(:coerce)
31
+ a, b = other.coerce(self)
32
+ return a != b
33
+ end
34
+ super
35
+ end
36
+
37
+ def >=(other)
38
+ if other.is_a?(Z3::Expr)
39
+ raise ArgumentError.new unless other.respond_to?(:coerce)
40
+ a, b = other.coerce(self)
41
+ return a >= b
42
+ end
43
+ super
44
+ end
45
+
46
+ def >(other)
47
+ if other.is_a?(Z3::Expr)
48
+ raise ArgumentError.new unless other.respond_to?(:coerce)
49
+ a, b = other.coerce(self)
50
+ return a > b
51
+ end
52
+ super
53
+ end
54
+
55
+ def <=(other)
56
+ if other.is_a?(Z3::Expr)
57
+ raise ArgumentError.new unless other.respond_to?(:coerce)
58
+ a, b = other.coerce(self)
59
+ return a <= b
60
+ end
61
+ super
62
+ end
63
+
64
+ def <(other)
65
+ if other.is_a?(Z3::Expr)
66
+ raise ArgumentError.new unless other.respond_to?(:coerce)
67
+ a, b = other.coerce(self)
68
+ return a < b
69
+ end
70
+ super
71
+ end
72
+ end
73
+
74
+ class TrueClass
75
+ prepend EqualityHacks
76
+ end
77
+
78
+ class FalseClass
79
+ prepend EqualityHacks
80
+ end
81
+
82
+ class Rational
83
+ prepend CompareHacks
84
+ end
85
+
86
+ class Integer
87
+ prepend CompareHacks
88
+ end
89
+
90
+ class Float
91
+ prepend CompareHacks
92
+ end
data/lib/z3/low_level.rb CHANGED
@@ -23,7 +23,7 @@ module Z3
23
23
 
24
24
  def model_eval(model, ast, model_completion)
25
25
  rv_ptr = FFI::MemoryPointer.new(:pointer)
26
- result = Z3::VeryLowLevel.Z3_model_eval(_ctx_pointer, model._model, ast._ast, !!model_completion, rv_ptr)
26
+ result = Z3::VeryLowLevel.Z3_model_eval(_ctx_pointer, model._model, ast._ast, !!model_completion, rv_ptr) & 0xFF
27
27
  if result == 1
28
28
  rv_ptr.get_pointer(0)
29
29
  else
data/lib/z3.rb CHANGED
@@ -49,3 +49,6 @@ require_relative "z3/interface"
49
49
 
50
50
  # Printer
51
51
  require_relative "z3/printer"
52
+
53
+ # Hacks
54
+ require_relative "z3/hacks"
data/spec/expr_spec.rb CHANGED
@@ -68,21 +68,25 @@ module Z3
68
68
  %W[+ - * <= < >= >].each do |op|
69
69
  describe "#{op} arithmetic operator" do
70
70
  it "allows + of int or real variables" do
71
- expect((a.send op, b).sexpr).to eq "(#{op} a b)"
72
- expect((e.send op, f).sexpr).to eq "(#{op} e f)"
71
+ expect(a.send(op, b).sexpr).to eq "(#{op} a b)"
72
+ expect(e.send(op, f).sexpr).to eq "(#{op} e f)"
73
73
  end
74
74
 
75
75
  it "casts to correct type if possible" do
76
- expect((a.send op, e).sexpr).to eq "(#{op} (to_real a) e)"
77
- expect((e.send op, a).sexpr).to eq "(#{op} e (to_real a))"
78
- expect((a.send op, 42).sexpr).to eq "(#{op} a 42)"
79
- expect((42.send op, a).sexpr).to eq "(#{op} 42 a)"
80
- expect((a.send op, 42.5).sexpr).to eq "(#{op} (to_real a) (/ 85.0 2.0))"
81
- expect((42.5.send op, a).sexpr).to eq "(#{op} (/ 85.0 2.0) (to_real a))"
82
- expect((e.send op, 42).sexpr).to eq "(#{op} e 42.0)"
83
- expect((42.send op, e).sexpr).to eq "(#{op} 42.0 e)"
84
- expect((e.send op, 42.5).sexpr).to eq "(#{op} e (/ 85.0 2.0))"
85
- expect((42.5.send op, e).sexpr).to eq "(#{op} (/ 85.0 2.0) e)"
76
+ expect(a.send(op, e).sexpr).to eq "(#{op} (to_real a) e)"
77
+ expect(e.send(op, a).sexpr).to eq "(#{op} e (to_real a))"
78
+ expect(a.send(op, 42).sexpr).to eq "(#{op} a 42)"
79
+ expect(42.send(op, a).sexpr).to eq "(#{op} 42 a)"
80
+ expect(a.send(op, 42.5).sexpr).to eq "(#{op} (to_real a) (/ 85.0 2.0))"
81
+ expect(42.5.send(op, a).sexpr).to eq "(#{op} (/ 85.0 2.0) (to_real a))"
82
+ expect(a.send(op, Rational(19, 7)).sexpr).to eq "(#{op} (to_real a) (/ 19.0 7.0))"
83
+ expect(Rational(19, 7).send(op, a).sexpr).to eq "(#{op} (/ 19.0 7.0) (to_real a))"
84
+ expect(e.send(op, 42).sexpr).to eq "(#{op} e 42.0)"
85
+ expect(42.send(op, e).sexpr).to eq "(#{op} 42.0 e)"
86
+ expect(e.send(op, 42.5).sexpr).to eq "(#{op} e (/ 85.0 2.0))"
87
+ expect(42.5.send(op, e).sexpr).to eq "(#{op} (/ 85.0 2.0) e)"
88
+ expect(e.send(op, Rational(19, 7)).sexpr).to eq "(#{op} e (/ 19.0 7.0))"
89
+ expect(Rational(19, 7).send(op, e).sexpr).to eq "(#{op} (/ 19.0 7.0) e)"
86
90
  end
87
91
 
88
92
  it "raises exception if type cast is not possible" do
@@ -113,18 +117,19 @@ module Z3
113
117
 
114
118
  it "casts to correct type if possible" do
115
119
  expect((a == 42).sexpr).to eq "(= a 42)"
116
- # https://bugs.ruby-lang.org/issues/14437
117
- #expect((42 == a).sexpr).to eq "(= a 42)"
120
+ expect((42 == a).sexpr).to eq "(= 42 a)"
118
121
  expect((a == e).sexpr).to eq "(= (to_real a) e)"
119
122
  expect((e == a).sexpr).to eq "(= e (to_real a))"
120
123
  expect((c == true).sexpr).to eq "(= c true)"
121
124
  expect((c == false).sexpr).to eq "(= c false)"
122
125
  expect((a == 42.5).sexpr).to eq "(= (to_real a) (/ 85.0 2.0))"
123
- # expect((42.5 == a).sexpr).to eq "(= (to_real a) (/ 85.0 2.0))"
126
+ expect((42.5 == a).sexpr).to eq "(= (/ 85.0 2.0) (to_real a))"
124
127
  expect((e == 42.5).sexpr).to eq "(= e (/ 85.0 2.0))"
125
- # expect((42.5 == e).sexpr).to eq "(= e (/ 85.0 2.0))"
126
- # expect((true == c).sexpr).to eq "(= true c)"
127
- # expect((false == c).sexpr).to eq "(= false c)"
128
+ expect((42.5 == e).sexpr).to eq "(= (/ 85.0 2.0) e)"
129
+ expect((Rational(19, 7) == e).sexpr).to eq "(= (/ 19.0 7.0) e)"
130
+ expect((e == Rational(19, 7)).sexpr).to eq "(= e (/ 19.0 7.0))"
131
+ expect((true == c).sexpr).to eq "(= c true)"
132
+ expect((false == c).sexpr).to eq "(= c false)"
128
133
  end
129
134
 
130
135
  it "raises exception if type cast is not possible" do
@@ -132,8 +137,8 @@ module Z3
132
137
  expect{e == c}.to raise_error(ArgumentError)
133
138
  expect{a == true}.to raise_error(ArgumentError)
134
139
  expect{e == true}.to raise_error(ArgumentError)
135
- # expect{true == a}.to raise_error(ArgumentError)
136
- # expect{true == e}.to raise_error(ArgumentError)
140
+ expect{true == a}.to raise_error(ArgumentError)
141
+ expect{true == e}.to raise_error(ArgumentError)
137
142
  expect{c == 42}.to raise_error(ArgumentError)
138
143
  expect{c == 42.5}.to raise_error(ArgumentError)
139
144
  expect{42 == c}.to raise_error(ArgumentError)
@@ -150,18 +155,17 @@ module Z3
150
155
 
151
156
  it "casts to correct type if possible" do
152
157
  expect((a != 42).sexpr).to eq "(distinct a 42)"
153
- # https://bugs.ruby-lang.org/issues/14437
154
- # expect((42 != a).sexpr).to eq "(distinct a 42)"
158
+ expect((42 != a).sexpr).to eq "(distinct 42 a)"
155
159
  expect((a != e).sexpr).to eq "(distinct (to_real a) e)"
156
160
  expect((e != a).sexpr).to eq "(distinct e (to_real a))"
157
161
  expect((c != true).sexpr).to eq "(distinct c true)"
158
162
  expect((c != false).sexpr).to eq "(distinct c false)"
159
163
  expect((a != 42.5).sexpr).to eq "(distinct (to_real a) (/ 85.0 2.0))"
160
- # expect((42.5 != a).sexpr).to eq "(distinct (to_real a) (/ 85.0 2.0))"
164
+ expect((42.5 != a).sexpr).to eq "(distinct (/ 85.0 2.0) (to_real a))"
161
165
  expect((e != 42.5).sexpr).to eq "(distinct e (/ 85.0 2.0))"
162
- # expect((42.5 != e).sexpr).to eq "(distinct e (/ 85.0 2.0))"
163
- # expect((true != c).sexpr).to eq "(distinct true c)"
164
- # expect((false != c).sexpr).to eq "(distinct false c)"
166
+ expect((42.5 != e).sexpr).to eq "(distinct (/ 85.0 2.0) e)"
167
+ expect((true != c).sexpr).to eq "(distinct c true)"
168
+ expect((false != c).sexpr).to eq "(distinct c false)"
165
169
  end
166
170
 
167
171
  it "raises exception if type cast is not possible" do
@@ -169,12 +173,13 @@ module Z3
169
173
  expect{e != c}.to raise_error(ArgumentError)
170
174
  expect{a != true}.to raise_error(ArgumentError)
171
175
  expect{e != true}.to raise_error(ArgumentError)
172
- # expect{true != a}.to raise_error(ArgumentError)
173
- # expect{true != e}.to raise_error(ArgumentError)
176
+ expect{true != a}.to raise_error(ArgumentError)
177
+ expect{true != e}.to raise_error(ArgumentError)
174
178
  expect{c != 42}.to raise_error(ArgumentError)
175
179
  expect{c != 42.5}.to raise_error(ArgumentError)
176
- # expect{42 != c}.to raise_error(ArgumentError)
177
- # expect{42.5 != c}.to raise_error(ArgumentError)
180
+ expect{c != Rational(19, 7)}.to raise_error(ArgumentError)
181
+ expect{42 != c}.to raise_error(ArgumentError)
182
+ expect{Rational(19, 7) != c}.to raise_error(ArgumentError)
178
183
  end
179
184
  end
180
185
  end
@@ -1,5 +1,5 @@
1
1
  # There are multiple solutions, so this test is nondeterministic
2
- # Changing it to one returned by z3 4.6.0, but perhaps it needs some serious fixing
2
+ # Changing it to one returned by z3 4.7.1, but perhaps it needs some serious fixing
3
3
  describe "Knights Swap Puzzle" do
4
4
  it do
5
5
  expect("knights_puzzle").to have_output <<EOF
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.20180616
4
+ version: 0.0.20180624
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Wegrzanowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-16 00:00:00.000000000 Z
11
+ date: 2018-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -173,6 +173,7 @@ files:
173
173
  - lib/z3/expr/set_expr.rb
174
174
  - lib/z3/func_decl.rb
175
175
  - lib/z3/goal.rb
176
+ - lib/z3/hacks.rb
176
177
  - lib/z3/interface.rb
177
178
  - lib/z3/low_level.rb
178
179
  - lib/z3/low_level_auto.rb