Class: Z3::Solver

Inherits:
Object
  • Object
show all
Includes:
ReferenceCounted
Defined in:
lib/z3/solver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReferenceCounted

finalizer

Constructor Details

#initialize(params = {}) ⇒ Solver

Returns a new instance of Solver.



6
7
8
9
10
11
12
13
# File 'lib/z3/solver.rb', line 6

def initialize(params = {})
  @_solver = LowLevel.mk_solver
  inc_ref! :solver, @_solver
  reset_model!
  # Skipped for the common no-parameters case, as #set_params has to build
  # the parameter descriptions to check against, and there are hundreds of them
  set_params(params) unless params == {}
end

Instance Attribute Details

#_solverObject (readonly)

Returns the value of attribute _solver.



5
6
7
# File 'lib/z3/solver.rb', line 5

def _solver
  @_solver
end

Instance Method Details

#assert(ast) ⇒ Object



45
46
47
48
# File 'lib/z3/solver.rb', line 45

def assert(ast)
  reset_model!
  LowLevel.solver_assert(self, ast)
end

#assert_and_track(ast, tracker) ⇒ Object

tracker is a Bool const standing in for ast, and it's what shows up in #unsat_core if the solver blames this assertion



52
53
54
55
# File 'lib/z3/solver.rb', line 52

def assert_and_track(ast, tracker)
  reset_model!
  LowLevel.solver_assert_and_track(self, ast, tracker)
end

#assertionsObject



94
95
96
97
# File 'lib/z3/solver.rb', line 94

def assertions
  _ast_vector = LowLevel.solver_get_assertions(self)
  LowLevel.unpack_ast_vector(_ast_vector)
end

#checkObject



57
58
59
60
61
62
# File 'lib/z3/solver.rb', line 57

def check
  reset_model!
  result = check_sat_results(LowLevel.solver_check(self))
  @has_model = true if result == :sat
  result
end

#helpObject



111
112
113
# File 'lib/z3/solver.rb', line 111

def help
  LowLevel.solver_get_help(self)
end

#modelObject



86
87
88
89
90
91
92
# File 'lib/z3/solver.rb', line 86

def model
  if @has_model
    @model ||= Z3::Model.new(LowLevel.solver_get_model(self))
  else
    raise Z3::Exception, "You need to check that it's satisfiable before asking for the model"
  end
end

#num_scopesObject



115
116
117
# File 'lib/z3/solver.rb', line 115

def num_scopes
  LowLevel.solver_get_num_scopes(self)
end

#param_descrsObject

Z3_solver_get_param_descrs lists every parameter the solver takes, #help describes them



17
18
19
# File 'lib/z3/solver.rb', line 17

def param_descrs
  ParamDescrs.new(LowLevel.solver_get_param_descrs(self))
end

#pop(n = 1) ⇒ Object



35
36
37
38
# File 'lib/z3/solver.rb', line 35

def pop(n=1)
  reset_model!
  LowLevel.solver_pop(self, n)
end

#prove!(ast) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/z3/solver.rb', line 131

def prove!(ast)
  @has_model = false
  push
  assert(~ast)
  case check
  when :sat
    puts "Counterexample exists"
    model.each do |n,v|
      puts "* #{n} = #{v}"
    end
  when :unknown
    puts "Unknown"
  when :unsat
    puts "Proven"
  else
    raise "Wrong SAT result #{r}"
  end
ensure
  pop
end

#pushObject



30
31
32
33
# File 'lib/z3/solver.rb', line 30

def push
  reset_model!
  LowLevel.solver_push(self)
end

#reason_unknownObject



119
120
121
# File 'lib/z3/solver.rb', line 119

def reason_unknown
  LowLevel.solver_get_reason_unknown(self)
end

#resetObject



40
41
42
43
# File 'lib/z3/solver.rb', line 40

def reset
  reset_model!
  LowLevel.solver_reset(self)
end

#satisfiable?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/z3/solver.rb', line 64

def satisfiable?
  case check
  when :sat
    true
  when :unsat
    false
  else
    raise Z3::Exception, "Satisfiability unknown"
  end
end

#set_params(params) ⇒ Object

Parameters accumulate - setting one twice overrides it, but parameters set by earlier calls stay. Pass a Params if you want to skip the name and type checks, a Hash if you don't.



24
25
26
27
28
# File 'lib/z3/solver.rb', line 24

def set_params(params)
  params = Params.new(params, param_descrs) unless params.is_a?(Params)
  LowLevel.solver_set_params(self, params)
  self
end

#statisticsObject



106
107
108
109
# File 'lib/z3/solver.rb', line 106

def statistics
  _stats = LowLevel::solver_get_statistics(self)
  LowLevel.unpack_statistics(_stats)
end

#to_dimacs(include_names = true) ⇒ Object



127
128
129
# File 'lib/z3/solver.rb', line 127

def to_dimacs(include_names=true)
  LowLevel.solver_to_dimacs_string(self, include_names)
end

#to_sObject



123
124
125
# File 'lib/z3/solver.rb', line 123

def to_s
  LowLevel.solver_to_string(self)
end

#unsat_coreObject

Only the trackers passed to #assert_and_track can ever show up here, plainly asserted formulas are never blamed



101
102
103
104
# File 'lib/z3/solver.rb', line 101

def unsat_core
  _ast_vector = LowLevel.solver_get_unsat_core(self)
  LowLevel.unpack_ast_vector(_ast_vector)
end

#unsatisfiable?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
# File 'lib/z3/solver.rb', line 75

def unsatisfiable?
  case check
  when :unsat
    true
  when :sat
    false
  else
    raise Z3::Exception, "Satisfiability unknown"
  end
end