z3 0.0.20160221 → 0.0.20160323
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/examples/algebra_problems +24 -24
- data/examples/basic_int_math +8 -8
- data/examples/basic_logic +8 -8
- data/examples/bit_tricks +161 -0
- data/examples/bridges_solver +1 -1
- data/examples/clogic_puzzle_solver +135 -0
- data/examples/four_hackers_puzzle +194 -0
- data/examples/geometry_problem +11 -11
- data/examples/kakuro_solver +3 -3
- data/examples/kinematics_problems +37 -37
- data/examples/letter_connections_solver +11 -11
- data/examples/light_up_solver +5 -5
- data/examples/minisudoku_solver +4 -4
- data/examples/selfref_solver +35 -35
- data/examples/sudoku_solver +4 -4
- data/examples/verbal_arithmetic +2 -2
- data/lib/z3/exception.rb +25 -0
- data/lib/z3/func_decl.rb +7 -7
- data/lib/z3/interface.rb +255 -0
- data/lib/z3/low_level.rb +4 -6
- data/lib/z3/low_level_auto.rb +1551 -1547
- data/lib/z3/model.rb +3 -2
- data/lib/z3/solver.rb +65 -54
- data/lib/z3/sort/bitvec_sort.rb +40 -0
- data/lib/z3/sort/bool_sort.rb +31 -0
- data/lib/z3/sort/int_sort.rb +21 -0
- data/lib/z3/sort/real_sort.rb +36 -0
- data/lib/z3/sort/sort.rb +76 -0
- data/lib/z3/value/arith_value.rb +53 -0
- data/lib/z3/value/bitvec_value.rb +67 -0
- data/lib/z3/value/bool_value.rb +29 -0
- data/lib/z3/value/int_value.rb +7 -0
- data/lib/z3/value/real_value.rb +7 -0
- data/lib/z3/value/value.rb +48 -0
- data/lib/z3/very_low_level.rb +28 -45
- data/lib/z3/very_low_level_auto.rb +518 -516
- data/lib/z3.rb +23 -33
- data/spec/integration/bit_tricks_spec.rb +21 -0
- data/spec/model_spec.rb +9 -9
- data/spec/solver_spec.rb +2 -2
- data/spec/sort_spec.rb +38 -13
- data/spec/{ast_spec.rb → value_spec.rb} +60 -57
- metadata +21 -6
- data/lib/z3/ast.rb +0 -302
- data/lib/z3/sort.rb +0 -33
- /data/spec/integration/{bagic_int_math_spec.rb → basic_int_math_spec.rb} +0 -0
data/examples/verbal_arithmetic
CHANGED
@@ -6,7 +6,7 @@ class VerbalArithmetic
|
|
6
6
|
def initialize(a, b, c)
|
7
7
|
@solver = Z3::Solver.new
|
8
8
|
@vars = Hash.new do |ht,name|
|
9
|
-
v = Z3
|
9
|
+
v = Z3.Int(name)
|
10
10
|
@solver.assert v >= 0
|
11
11
|
@solver.assert v <= 9
|
12
12
|
ht[name] = v
|
@@ -21,7 +21,7 @@ class VerbalArithmetic
|
|
21
21
|
@solver.assert @b[0] != 0
|
22
22
|
@solver.assert @c[0] != 0
|
23
23
|
@solver.assert word_value(@a) + word_value(@b) == word_value(@c)
|
24
|
-
@solver.assert Z3
|
24
|
+
@solver.assert Z3.Distinct(*@vars.values)
|
25
25
|
|
26
26
|
if @solver.check == :sat
|
27
27
|
@model = @solver.model
|
data/lib/z3/exception.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Z3
|
2
|
+
class Exception < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
|
6
|
+
Z3::LowLevel.set_error_handler do |ctx, error|
|
7
|
+
error_codes_enum = %W[
|
8
|
+
Z3_OK
|
9
|
+
Z3_SORT_ERROR
|
10
|
+
Z3_IOB
|
11
|
+
Z3_INVALID_ARG
|
12
|
+
Z3_PARSER_ERROR
|
13
|
+
Z3_NO_PARSER
|
14
|
+
Z3_INVALID_PATTERN
|
15
|
+
Z3_MEMOUT_FAIL
|
16
|
+
Z3_FILE_ACCESS_ERROR
|
17
|
+
Z3_INTERNAL_FATAL
|
18
|
+
Z3_INVALID_USAGE
|
19
|
+
Z3_DEC_REF_ERROR
|
20
|
+
Z3_EXCEPTION
|
21
|
+
]
|
22
|
+
error = error_codes_enum[error] || error
|
23
|
+
raise Z3::Exception, "Z3 library failed with error #{error}"
|
24
|
+
end
|
25
|
+
end
|
data/lib/z3/func_decl.rb
CHANGED
@@ -12,14 +12,14 @@ class Z3::FuncDecl
|
|
12
12
|
Z3::LowLevel.get_arity(self)
|
13
13
|
end
|
14
14
|
|
15
|
-
def to_ast
|
16
|
-
|
17
|
-
end
|
15
|
+
# def to_ast
|
16
|
+
# Z3::Ast.new(Z3::LowLevel.func_decl_to_ast(self))
|
17
|
+
# end
|
18
18
|
|
19
|
-
def ast_parameter(i)
|
20
|
-
|
21
|
-
|
22
|
-
end
|
19
|
+
# def ast_parameter(i)
|
20
|
+
# # vs arity ?
|
21
|
+
# Z3::Ast.new(Z3::LowLevel.get_decl_ast_parameter(self, i))
|
22
|
+
# end
|
23
23
|
|
24
24
|
def to_s
|
25
25
|
name
|
data/lib/z3/interface.rb
ADDED
@@ -0,0 +1,255 @@
|
|
1
|
+
module Z3
|
2
|
+
def Int(v)
|
3
|
+
IntSort.new.var(v)
|
4
|
+
end
|
5
|
+
|
6
|
+
def Real(v)
|
7
|
+
RealSort.new.var(v)
|
8
|
+
end
|
9
|
+
|
10
|
+
def Bool(v)
|
11
|
+
BoolSort.new.var(v)
|
12
|
+
end
|
13
|
+
|
14
|
+
def Bitvec(v, n)
|
15
|
+
BitvecSort.new(n).var(v)
|
16
|
+
end
|
17
|
+
|
18
|
+
def True
|
19
|
+
BoolSort.new.True
|
20
|
+
end
|
21
|
+
|
22
|
+
def False
|
23
|
+
BoolSort.new.False
|
24
|
+
end
|
25
|
+
|
26
|
+
def And(*args)
|
27
|
+
args = coerce_to_same_sort(*args)
|
28
|
+
case args[0]
|
29
|
+
when BoolValue
|
30
|
+
BoolSort.new.new(Z3::LowLevel.mk_and(args))
|
31
|
+
when BitvecValue
|
32
|
+
args.inject do |a,b|
|
33
|
+
a.sort.new(Z3::LowLevel.mk_bvand(a, b))
|
34
|
+
end
|
35
|
+
else
|
36
|
+
raise ArgumentError, "Can't perform logic operations on #{a.sort} values, only Bool and Bitvec"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def Or(*args)
|
41
|
+
args = coerce_to_same_sort(*args)
|
42
|
+
case args[0]
|
43
|
+
when BoolValue
|
44
|
+
BoolSort.new.new(Z3::LowLevel.mk_or(args))
|
45
|
+
when BitvecValue
|
46
|
+
args.inject do |a,b|
|
47
|
+
a.sort.new(Z3::LowLevel.mk_bvor(a, b))
|
48
|
+
end
|
49
|
+
else
|
50
|
+
raise ArgumentError, "Can't perform logic operations on #{a.sort} values, only Bool and Bitvec"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def Xor(*args)
|
55
|
+
args = coerce_to_same_sort(*args)
|
56
|
+
case args[0]
|
57
|
+
when BoolValue
|
58
|
+
BoolSort.new.new(Z3::LowLevel.mk_xor(args))
|
59
|
+
when BitvecValue
|
60
|
+
args.inject do |a,b|
|
61
|
+
a.sort.new(Z3::LowLevel.mk_bvxor(a, b))
|
62
|
+
end
|
63
|
+
else
|
64
|
+
raise ArgumentError, "Can't perform logic operations on #{a.sort} values, only Bool and Bitvec"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def Implies(a,b)
|
69
|
+
a, b = coerce_to_same_bool_sort(a, b)
|
70
|
+
BoolSort.new.new(Z3::LowLevel.mk_implies(a, b))
|
71
|
+
end
|
72
|
+
|
73
|
+
def Iff(a,b)
|
74
|
+
a, b = coerce_to_same_bool_sort(a, b)
|
75
|
+
BoolSort.new.new(Z3::LowLevel.mk_iff(a, b))
|
76
|
+
end
|
77
|
+
|
78
|
+
def Add(*args)
|
79
|
+
raise ArgumentError if args.empty?
|
80
|
+
args = coerce_to_same_sort(*args)
|
81
|
+
case args[0]
|
82
|
+
when ArithValue
|
83
|
+
args[0].sort.new(LowLevel.mk_add(args))
|
84
|
+
when BitvecValue
|
85
|
+
args.inject do |a,b|
|
86
|
+
a.sort.new(LowLevel.mk_bvadd(a,b))
|
87
|
+
end
|
88
|
+
else
|
89
|
+
raise ArgumentError, "Can't perform logic operations on #{args[0].sort} values, only Int/Real/Bitvec"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def Sub(*args)
|
94
|
+
args = coerce_to_same_sort(*args)
|
95
|
+
case args[0]
|
96
|
+
when ArithValue
|
97
|
+
args[0].sort.new(LowLevel.mk_sub(args))
|
98
|
+
when BitvecValue
|
99
|
+
args.inject do |a,b|
|
100
|
+
a.sort.new(LowLevel.mk_bvsub(a,b))
|
101
|
+
end
|
102
|
+
else
|
103
|
+
raise ArgumentError, "Can't perform logic operations on #{args[0].sort} values, only Int/Real/Bitvec"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def Mul(*args)
|
108
|
+
args = coerce_to_same_sort(*args)
|
109
|
+
case args[0]
|
110
|
+
when ArithValue
|
111
|
+
args[0].sort.new(LowLevel.mk_mul(args))
|
112
|
+
when BitvecValue
|
113
|
+
args.inject do |a,b|
|
114
|
+
a.sort.new(LowLevel.mk_bvmul(a,b))
|
115
|
+
end
|
116
|
+
else
|
117
|
+
raise ArgumentError, "Can't perform logic operations on #{args[0].sort} values, only Int/Real/Bitvec"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def Div(a,b)
|
122
|
+
a, b = coerce_to_same_arith_sort(a, b)
|
123
|
+
a.sort.new(LowLevel.mk_div(a, b))
|
124
|
+
end
|
125
|
+
|
126
|
+
def Power(a, b)
|
127
|
+
# Wait, is this even legitimate that it's I**I and R**R?
|
128
|
+
a, b = coerce_to_same_arith_sort(a, b)
|
129
|
+
a.sort.new(LowLevel.mk_power(a, b))
|
130
|
+
end
|
131
|
+
|
132
|
+
def Eq(a, b)
|
133
|
+
a, b = coerce_to_same_sort(a, b)
|
134
|
+
BoolSort.new.new(LowLevel.mk_eq(a, b))
|
135
|
+
end
|
136
|
+
|
137
|
+
def Distinct(*args)
|
138
|
+
args = coerce_to_same_sort(*args)
|
139
|
+
BoolSort.new.new(LowLevel.mk_distinct(args))
|
140
|
+
end
|
141
|
+
|
142
|
+
def Gt(a, b)
|
143
|
+
a, b = coerce_to_same_sort(a, b)
|
144
|
+
case a
|
145
|
+
when ArithValue
|
146
|
+
BoolSort.new.new(LowLevel.mk_gt(a, b))
|
147
|
+
when BitvecValue
|
148
|
+
BoolSort.new.new(LowLevel.mk_bvsgt(a, b))
|
149
|
+
else
|
150
|
+
raise ArgumentError, "Can't compare #{a.sort} values"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def Ge(a, b)
|
155
|
+
a, b = coerce_to_same_sort(a, b)
|
156
|
+
case a
|
157
|
+
when ArithValue
|
158
|
+
BoolSort.new.new(LowLevel.mk_ge(a, b))
|
159
|
+
when BitvecValue
|
160
|
+
BoolSort.new.new(LowLevel.mk_bvsge(a, b))
|
161
|
+
else
|
162
|
+
raise ArgumentError, "Can't compare #{a.sort} values"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def Lt(a, b)
|
167
|
+
a, b = coerce_to_same_sort(a, b)
|
168
|
+
case a
|
169
|
+
when ArithValue
|
170
|
+
BoolSort.new.new(LowLevel.mk_lt(a, b))
|
171
|
+
when BitvecValue
|
172
|
+
BoolSort.new.new(LowLevel.mk_bvslt(a, b))
|
173
|
+
else
|
174
|
+
raise ArgumentError, "Can't compare #{a.sort} values"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def Le(a, b)
|
179
|
+
a, b = coerce_to_same_sort(a, b)
|
180
|
+
case a
|
181
|
+
when ArithValue
|
182
|
+
BoolSort.new.new(LowLevel.mk_le(a, b))
|
183
|
+
when BitvecValue
|
184
|
+
BoolSort.new.new(LowLevel.mk_bvsle(a, b))
|
185
|
+
else
|
186
|
+
raise ArgumentError, "Can't compare #{a.sort} values"
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def Not(a)
|
191
|
+
a = Value.from_const(a) unless a.is_a?(Value)
|
192
|
+
raise Z3::Exception, "No idea how to autoconvert `#{a.class}': `#{a.inspect}'" unless a.is_a?(BoolValue)
|
193
|
+
BoolSort.new.new(LowLevel.mk_not(a))
|
194
|
+
end
|
195
|
+
|
196
|
+
# Presume arithmetic (sign-extend)
|
197
|
+
def RShift(a, b)
|
198
|
+
a, b = coerce_to_same_bv_sort(a, b)
|
199
|
+
a.sort.new(LowLevel.mk_bvashr(a, b))
|
200
|
+
end
|
201
|
+
|
202
|
+
def LShift(a, b)
|
203
|
+
a, b = coerce_to_same_bv_sort(a, b)
|
204
|
+
a.sort.new(LowLevel.mk_bvshl(a, b))
|
205
|
+
end
|
206
|
+
|
207
|
+
def version
|
208
|
+
LowLevel.get_version.join(".")
|
209
|
+
end
|
210
|
+
|
211
|
+
def set_param(k,v)
|
212
|
+
LowLevel.global_param_set(k,v)
|
213
|
+
end
|
214
|
+
|
215
|
+
private
|
216
|
+
|
217
|
+
def coerce_to_same_sort(*args)
|
218
|
+
# This will raise exception unless one of the sorts is highest
|
219
|
+
# So [true, IntSort]
|
220
|
+
max_sort = args.map{|a| a.is_a?(Value) ? a.sort : Value.sort_for_const(a)}.max
|
221
|
+
args.map do |a|
|
222
|
+
if a.is_a?(Value)
|
223
|
+
if a.sort == max_sort
|
224
|
+
a
|
225
|
+
else
|
226
|
+
max_sort.from_value(a)
|
227
|
+
end
|
228
|
+
else
|
229
|
+
max_sort.from_const(a)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def coerce_to_same_bool_sort(*args)
|
235
|
+
args = coerce_to_same_sort(*args)
|
236
|
+
raise Z3::Exception, "Bool value expected" unless args[0].is_a?(BoolValue)
|
237
|
+
args
|
238
|
+
end
|
239
|
+
|
240
|
+
def coerce_to_same_arith_sort(*args)
|
241
|
+
args = coerce_to_same_sort(*args)
|
242
|
+
raise Z3::Exception, "Int or Real value expected" unless args[0].is_a?(IntValue) or args[0].is_a?(RealValue)
|
243
|
+
args
|
244
|
+
end
|
245
|
+
|
246
|
+
def coerce_to_same_bv_sort(*args)
|
247
|
+
args = coerce_to_same_sort(*args)
|
248
|
+
raise Z3::Exception, "Bitvec value with same nize expected" unless args[0].is_a?(BitvecValue)
|
249
|
+
args
|
250
|
+
end
|
251
|
+
|
252
|
+
class << self
|
253
|
+
include Z3
|
254
|
+
end
|
255
|
+
end
|
data/lib/z3/low_level.rb
CHANGED
@@ -54,8 +54,10 @@ module Z3::LowLevel
|
|
54
54
|
Z3::VeryLowLevel.Z3_mk_distinct(_ctx_pointer, asts.size, asts_vector(asts))
|
55
55
|
end
|
56
56
|
|
57
|
-
|
58
|
-
|
57
|
+
# Should be private
|
58
|
+
def _ctx_pointer
|
59
|
+
@_ctx_pointer ||= Z3::Context.instance._context
|
60
|
+
end
|
59
61
|
|
60
62
|
private
|
61
63
|
|
@@ -65,9 +67,5 @@ module Z3::LowLevel
|
|
65
67
|
c_args.write_array_of_pointer args.map(&:_ast)
|
66
68
|
c_args
|
67
69
|
end
|
68
|
-
|
69
|
-
def _ctx_pointer
|
70
|
-
@_ctx_pointer ||= Z3::Context.instance._context
|
71
|
-
end
|
72
70
|
end
|
73
71
|
end
|