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 4379 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  name,
  ctx = None 
)

Definition at line 4405 of file z3py.py.

4405  def __init__(self, name, ctx=None):
4406  self.ctx = _get_ctx(ctx)
4407  self.name = name
4408  self.constructors = []
4409 

Member Function Documentation

◆ __deepcopy__()

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 4410 of file z3py.py.

4410  def __deepcopy__(self, memo={}):
4411  r = Datatype(self.name, self.ctx)
4412  r.constructors = copy.deepcopy(self.constructors)
4413  return r
4414 

◆ __repr__()

def __repr__ (   self)

Definition at line 4442 of file z3py.py.

4442  def __repr__(self):
4443  return "Datatype(%s, %s)" % (self.name, self.constructors)
4444 

◆ create()

def create (   self)
Create a Z3 datatype based on the constructors declared using the mehtod `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 4445 of file z3py.py.

Referenced by Datatype.declare().

4445  def create(self):
4446  """Create a Z3 datatype based on the constructors declared using the mehtod `declare()`.
4447 
4448  The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
4449 
4450  >>> List = Datatype('List')
4451  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4452  >>> List.declare('nil')
4453  >>> List = List.create()
4454  >>> List.nil
4455  nil
4456  >>> List.cons(10, List.nil)
4457  cons(10, nil)
4458  """
4459  return CreateDatatypes([self])[0]
4460 
def CreateDatatypes(ds)
Definition: z3py.py:4479

◆ 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 followin 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 4422 of file z3py.py.

Referenced by Datatype.create().

4422  def declare(self, name, *args):
4423  """Declare constructor named `name` with the given accessors `args`.
4424  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.
4425 
4426  In the followin example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
4427  declares the constructor named `cons` that builds a new List using an integer and a List.
4428  It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer of a `cons` cell,
4429  and `cdr` the list of a `cons` cell. After all constructors were declared, we use the method create() to create
4430  the actual datatype in Z3.
4431 
4432  >>> List = Datatype('List')
4433  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4434  >>> List.declare('nil')
4435  >>> List = List.create()
4436  """
4437  if __debug__:
4438  _z3_assert(isinstance(name, str), "String expected")
4439  _z3_assert(name != "", "Constructor name cannot be empty")
4440  return self.declare_core(name, "is_" + name, *args)
4441 

◆ declare_core()

def declare_core (   self,
  name,
  rec_name,
  args 
)

Definition at line 4415 of file z3py.py.

4415  def declare_core(self, name, rec_name, *args):
4416  if __debug__:
4417  _z3_assert(isinstance(name, str), "String expected")
4418  _z3_assert(isinstance(rec_name, str), "String expected")
4419  _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)")
4420  self.constructors.append((name, rec_name, args))
4421 

Field Documentation

◆ constructors

constructors

Definition at line 4408 of file z3py.py.

◆ ctx

ctx

Definition at line 4406 of file z3py.py.

Referenced by Probe.__call__(), Fixedpoint.__deepcopy__(), Optimize.__deepcopy__(), ApplyResult.__deepcopy__(), Tactic.__deepcopy__(), Probe.__deepcopy__(), Fixedpoint.__del__(), Optimize.__del__(), ApplyResult.__del__(), Tactic.__del__(), Probe.__del__(), Probe.__eq__(), Probe.__ge__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), ApplyResult.__len__(), Probe.__lt__(), Probe.__ne__(), Fixedpoint.add_cover(), Fixedpoint.add_rule(), Optimize.add_soft(), Tactic.apply(), ApplyResult.as_expr(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Optimize.assertions(), Optimize.check(), ApplyResult.convert_model(), Optimize.from_file(), Optimize.from_string(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), Fixedpoint.get_ground_sat_answer(), Fixedpoint.get_num_levels(), Fixedpoint.get_rule_names_along_trace(), Fixedpoint.get_rules(), Fixedpoint.get_rules_along_trace(), Fixedpoint.help(), Optimize.help(), Tactic.help(), Optimize.maximize(), Optimize.minimize(), Optimize.model(), Optimize.objectives(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Fixedpoint.pop(), Optimize.pop(), Fixedpoint.push(), Optimize.push(), Fixedpoint.query(), Fixedpoint.query_from_lvl(), Fixedpoint.reason_unknown(), Optimize.reason_unknown(), Fixedpoint.register_relation(), Fixedpoint.set(), Optimize.set(), Fixedpoint.set_predicate_representation(), Fixedpoint.sexpr(), Optimize.sexpr(), ApplyResult.sexpr(), Tactic.solver(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), Fixedpoint.to_string(), and Fixedpoint.update_rule().

◆ name

name

Definition at line 4407 of file z3py.py.