Z3
Public Member Functions | Data Fields
Datatype Class Reference

Public Member Functions

def __init__ (self, name, ctx=None)
 
def __deepcopy__ (self, memo={})
 
def declare_core (self, name, rec_name, *args)
 
def declare (self, name, *args)
 
def __repr__ (self)
 
def create (self)
 

Data Fields

 ctx
 
 name
 
 constructors
 

Detailed Description

Helper class for declaring Z3 datatypes.

>>> List = Datatype('List')
>>> List.declare('cons', ('car', IntSort()), ('cdr', List))
>>> List.declare('nil')
>>> List = List.create()
>>> # List is now a Z3 declaration
>>> List.nil
nil
>>> List.cons(10, List.nil)
cons(10, nil)
>>> List.cons(10, List.nil).sort()
List
>>> cons = List.cons
>>> nil  = List.nil
>>> car  = List.car
>>> cdr  = List.cdr
>>> n = cons(1, cons(0, nil))
>>> n
cons(1, cons(0, nil))
>>> simplify(cdr(n))
cons(0, nil)
>>> simplify(car(n))
1

Definition at line 4723 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  name,
  ctx = None 
)

Definition at line 4749 of file z3py.py.

4749  def __init__(self, name, ctx=None):
4750  self.ctx = _get_ctx(ctx)
4751  self.name = name
4752  self.constructors = []
4753 

Member Function Documentation

◆ __deepcopy__()

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 4754 of file z3py.py.

4754  def __deepcopy__(self, memo={}):
4755  r = Datatype(self.name, self.ctx)
4756  r.constructors = copy.deepcopy(self.constructors)
4757  return r
4758 

◆ __repr__()

def __repr__ (   self)

Definition at line 4786 of file z3py.py.

4786  def __repr__(self):
4787  return "Datatype(%s, %s)" % (self.name, self.constructors)
4788 

◆ create()

def create (   self)
Create a Z3 datatype based on the constructors declared using the method `declare()`.

The function `CreateDatatypes()` must be used to define mutually recursive datatypes.

>>> List = Datatype('List')
>>> List.declare('cons', ('car', IntSort()), ('cdr', List))
>>> List.declare('nil')
>>> List = List.create()
>>> List.nil
nil
>>> List.cons(10, List.nil)
cons(10, nil)

Definition at line 4789 of file z3py.py.

4789  def create(self):
4790  """Create a Z3 datatype based on the constructors declared using the method `declare()`.
4791 
4792  The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
4793 
4794  >>> List = Datatype('List')
4795  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4796  >>> List.declare('nil')
4797  >>> List = List.create()
4798  >>> List.nil
4799  nil
4800  >>> List.cons(10, List.nil)
4801  cons(10, nil)
4802  """
4803  return CreateDatatypes([self])[0]
4804 

◆ declare()

def declare (   self,
  name,
args 
)
Declare constructor named `name` with the given accessors `args`.
Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort or a reference to the datatypes being declared.

In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
declares the constructor named `cons` that builds a new List using an integer and a List.
It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer of a `cons` cell,
and `cdr` the list of a `cons` cell. After all constructors were declared, we use the method create() to create
the actual datatype in Z3.

>>> List = Datatype('List')
>>> List.declare('cons', ('car', IntSort()), ('cdr', List))
>>> List.declare('nil')
>>> List = List.create()

Definition at line 4766 of file z3py.py.

4766  def declare(self, name, *args):
4767  """Declare constructor named `name` with the given accessors `args`.
4768  Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort or a reference to the datatypes being declared.
4769 
4770  In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
4771  declares the constructor named `cons` that builds a new List using an integer and a List.
4772  It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer of a `cons` cell,
4773  and `cdr` the list of a `cons` cell. After all constructors were declared, we use the method create() to create
4774  the actual datatype in Z3.
4775 
4776  >>> List = Datatype('List')
4777  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4778  >>> List.declare('nil')
4779  >>> List = List.create()
4780  """
4781  if z3_debug():
4782  _z3_assert(isinstance(name, str), "String expected")
4783  _z3_assert(name != "", "Constructor name cannot be empty")
4784  return self.declare_core(name, "is-" + name, *args)
4785 

◆ declare_core()

def declare_core (   self,
  name,
  rec_name,
args 
)

Definition at line 4759 of file z3py.py.

4759  def declare_core(self, name, rec_name, *args):
4760  if z3_debug():
4761  _z3_assert(isinstance(name, str), "String expected")
4762  _z3_assert(isinstance(rec_name, str), "String expected")
4763  _z3_assert(all([_valid_accessor(a) for a in args]), "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)")
4764  self.constructors.append((name, rec_name, args))
4765 

Referenced by Datatype.declare().

Field Documentation

◆ constructors

constructors

Definition at line 4752 of file z3py.py.

Referenced by Datatype.__deepcopy__(), Datatype.__repr__(), and Datatype.declare_core().

◆ ctx

ctx

Definition at line 4750 of file z3py.py.

Referenced by Probe.__call__(), AstMap.__contains__(), Goal.__copy__(), AstVector.__copy__(), FuncInterp.__copy__(), ModelRef.__copy__(), Solver.__copy__(), Datatype.__deepcopy__(), ParamsRef.__deepcopy__(), ParamDescrsRef.__deepcopy__(), Goal.__deepcopy__(), AstVector.__deepcopy__(), AstMap.__deepcopy__(), FuncEntry.__deepcopy__(), FuncInterp.__deepcopy__(), ModelRef.__deepcopy__(), Statistics.__deepcopy__(), Solver.__deepcopy__(), Fixedpoint.__deepcopy__(), Optimize.__deepcopy__(), ApplyResult.__deepcopy__(), Tactic.__deepcopy__(), Probe.__deepcopy__(), ScopedConstructor.__del__(), ScopedConstructorList.__del__(), ParamsRef.__del__(), ParamDescrsRef.__del__(), Goal.__del__(), AstVector.__del__(), AstMap.__del__(), FuncEntry.__del__(), FuncInterp.__del__(), ModelRef.__del__(), Statistics.__del__(), Solver.__del__(), Fixedpoint.__del__(), Optimize.__del__(), ApplyResult.__del__(), Tactic.__del__(), Probe.__del__(), Probe.__eq__(), Probe.__ge__(), AstVector.__getitem__(), AstMap.__getitem__(), ModelRef.__getitem__(), Statistics.__getitem__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), AstVector.__len__(), AstMap.__len__(), ModelRef.__len__(), Statistics.__len__(), ApplyResult.__len__(), Probe.__lt__(), Probe.__ne__(), ParamsRef.__repr__(), ParamDescrsRef.__repr__(), AstMap.__repr__(), Statistics.__repr__(), AstVector.__setitem__(), AstMap.__setitem__(), Fixedpoint.add_cover(), Fixedpoint.add_rule(), Optimize.add_soft(), Tactic.apply(), FuncEntry.arg_value(), FuncInterp.arity(), Goal.as_expr(), ApplyResult.as_expr(), Solver.assert_and_track(), Optimize.assert_and_track(), Goal.assert_exprs(), Solver.assert_exprs(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Solver.assertions(), Optimize.assertions(), Solver.check(), Optimize.check(), UserPropagateBase.conflict(), Solver.consequences(), Goal.convert_model(), UserPropagateBase.ctx_ref(), ModelRef.decls(), Goal.depth(), Goal.dimacs(), Solver.dimacs(), FuncInterp.else_value(), FuncInterp.entry(), AstMap.erase(), ModelRef.eval(), Solver.from_file(), Optimize.from_file(), Solver.from_string(), Optimize.from_string(), Goal.get(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), ParamDescrsRef.get_documentation(), Fixedpoint.get_ground_sat_answer(), ModelRef.get_interp(), Statistics.get_key_value(), ParamDescrsRef.get_kind(), ParamDescrsRef.get_name(), Fixedpoint.get_num_levels(), Fixedpoint.get_rule_names_along_trace(), Fixedpoint.get_rules(), Fixedpoint.get_rules_along_trace(), ModelRef.get_sort(), ModelRef.get_universe(), Solver.help(), Fixedpoint.help(), Optimize.help(), Tactic.help(), Solver.import_model_converter(), Goal.inconsistent(), AstMap.keys(), Statistics.keys(), Optimize.maximize(), Optimize.minimize(), Solver.model(), Optimize.model(), Solver.non_units(), FuncEntry.num_args(), FuncInterp.num_entries(), Solver.num_scopes(), ModelRef.num_sorts(), Optimize.objectives(), Solver.param_descrs(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Solver.pop(), Optimize.pop(), Goal.prec(), Solver.proof(), AstVector.push(), Solver.push(), Optimize.push(), Fixedpoint.query(), Fixedpoint.query_from_lvl(), Solver.reason_unknown(), Fixedpoint.reason_unknown(), Optimize.reason_unknown(), Fixedpoint.register_relation(), AstMap.reset(), Solver.reset(), AstVector.resize(), ParamsRef.set(), Solver.set(), Fixedpoint.set(), Optimize.set(), Fixedpoint.set_predicate_representation(), Goal.sexpr(), AstVector.sexpr(), ModelRef.sexpr(), Solver.sexpr(), Fixedpoint.sexpr(), Optimize.sexpr(), ApplyResult.sexpr(), ParamDescrsRef.size(), Goal.size(), Tactic.solver(), Solver.statistics(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), Fixedpoint.to_string(), Solver.trail(), Solver.trail_levels(), Goal.translate(), AstVector.translate(), FuncInterp.translate(), ModelRef.translate(), Solver.translate(), Solver.units(), Solver.unsat_core(), Optimize.unsat_core(), Fixedpoint.update_rule(), ParamsRef.validate(), and FuncEntry.value().

◆ name

name

Definition at line 4751 of file z3py.py.

Referenced by Datatype.__deepcopy__(), and Datatype.__repr__().

z3py.UserPropagateBase.__init__
def __init__(self, s, ctx=None)
Definition: z3py.py:10582
z3py.CreateDatatypes
def CreateDatatypes(*ds)
Definition: z3py.py:4823
z3py.z3_debug
def z3_debug()
Definition: z3py.py:56