Z3
Public Member Functions | Data Fields
Datatype Class Reference

Public Member Functions

def __init__
 
def __deepcopy__
 
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 5229 of file z3py.py.

Constructor & Destructor Documentation

def __init__ (   self,
  name,
  ctx = None 
)

Definition at line 5256 of file z3py.py.

5256  def __init__(self, name, ctx=None):
5257  self.ctx = _get_ctx(ctx)
5258  self.name = name
5259  self.constructors = []
5260 
def __init__
Definition: z3py.py:5256

Member Function Documentation

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 5261 of file z3py.py.

5261  def __deepcopy__(self, memo={}):
5262  r = Datatype(self.name, self.ctx)
5263  r.constructors = copy.deepcopy(self.constructors)
5264  return r
5265 
def __deepcopy__
Definition: z3py.py:5261
def __repr__ (   self)

Definition at line 5297 of file z3py.py.

5297  def __repr__(self):
5298  return "Datatype(%s, %s)" % (self.name, self.constructors)
5299 
def __repr__(self)
Definition: z3py.py:5297
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 5300 of file z3py.py.

Referenced by Datatype.declare().

5300  def create(self):
5301  """Create a Z3 datatype based on the constructors declared using the method `declare()`.
5302 
5303  The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
5304 
5305  >>> List = Datatype('List')
5306  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5307  >>> List.declare('nil')
5308  >>> List = List.create()
5309  >>> List.nil
5310  nil
5311  >>> List.cons(10, List.nil)
5312  cons(10, nil)
5313  """
5314  return CreateDatatypes([self])[0]
5315 
5316 
def CreateDatatypes(ds)
Definition: z3py.py:5341
def create(self)
Definition: z3py.py:5300
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 5276 of file z3py.py.

Referenced by Datatype.create().

5276  def declare(self, name, *args):
5277  """Declare constructor named `name` with the given accessors `args`.
5278  Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort
5279  or a reference to the datatypes being declared.
5280 
5281  In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
5282  declares the constructor named `cons` that builds a new List using an integer and a List.
5283  It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer
5284  of a `cons` cell, and `cdr` the list of a `cons` cell. After all constructors were declared,
5285  we use the method create() to create the actual datatype in Z3.
5286 
5287  >>> List = Datatype('List')
5288  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5289  >>> List.declare('nil')
5290  >>> List = List.create()
5291  """
5292  if z3_debug():
5293  _z3_assert(isinstance(name, str), "String expected")
5294  _z3_assert(name != "", "Constructor name cannot be empty")
5295  return self.declare_core(name, "is-" + name, *args)
5296 
def declare(self, name, args)
Definition: z3py.py:5276
def z3_debug()
Definition: z3py.py:70
def declare_core(self, name, rec_name, args)
Definition: z3py.py:5266
def declare_core (   self,
  name,
  rec_name,
  args 
)

Definition at line 5266 of file z3py.py.

Referenced by Datatype.declare().

5266  def declare_core(self, name, rec_name, *args):
5267  if z3_debug():
5268  _z3_assert(isinstance(name, str), "String expected")
5269  _z3_assert(isinstance(rec_name, str), "String expected")
5270  _z3_assert(
5271  all([_valid_accessor(a) for a in args]),
5272  "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5273  )
5274  self.constructors.append((name, rec_name, args))
5275 
def z3_debug()
Definition: z3py.py:70
def declare_core(self, name, rec_name, args)
Definition: z3py.py:5266

Field Documentation

constructors

Definition at line 5259 of file z3py.py.

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

ctx
name

Definition at line 5258 of file z3py.py.

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