Z3
Public Member Functions | Data Fields
Datatype Class Reference

Public Member Functions

def __init__ (self, name, ctx=None)
 
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 4290 of file z3py.py.

Constructor & Destructor Documentation

§ __init__()

def __init__ (   self,
  name,
  ctx = None 
)

Definition at line 4316 of file z3py.py.

4316  def __init__(self, name, ctx=None):
4317  self.ctx = _get_ctx(ctx)
4318  self.name = name
4319  self.constructors = []
4320 

Member Function Documentation

§ __repr__()

def __repr__ (   self)

Definition at line 4348 of file z3py.py.

4348  def __repr__(self):
4349  return "Datatype(%s, %s)" % (self.name, self.constructors)
4350 

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

Referenced by Datatype.declare().

4351  def create(self):
4352  """Create a Z3 datatype based on the constructors declared using the mehtod `declare()`.
4353 
4354  The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
4355 
4356  >>> List = Datatype('List')
4357  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4358  >>> List.declare('nil')
4359  >>> List = List.create()
4360  >>> List.nil
4361  nil
4362  >>> List.cons(10, List.nil)
4363  cons(10, nil)
4364  """
4365  return CreateDatatypes([self])[0]
4366 
def CreateDatatypes(ds)
Definition: z3py.py:4385

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

Referenced by Datatype.create().

4328  def declare(self, name, *args):
4329  """Declare constructor named `name` with the given accessors `args`.
4330  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.
4331 
4332  In the followin example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
4333  declares the constructor named `cons` that builds a new List using an integer and a List.
4334  It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer of a `cons` cell,
4335  and `cdr` the list of a `cons` cell. After all constructors were declared, we use the method create() to create
4336  the actual datatype in Z3.
4337 
4338  >>> List = Datatype('List')
4339  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4340  >>> List.declare('nil')
4341  >>> List = List.create()
4342  """
4343  if __debug__:
4344  _z3_assert(isinstance(name, str), "String expected")
4345  _z3_assert(name != "", "Constructor name cannot be empty")
4346  return self.declare_core(name, "is_" + name, *args)
4347 

§ declare_core()

def declare_core (   self,
  name,
  rec_name,
  args 
)

Definition at line 4321 of file z3py.py.

4321  def declare_core(self, name, rec_name, *args):
4322  if __debug__:
4323  _z3_assert(isinstance(name, str), "String expected")
4324  _z3_assert(isinstance(rec_name, str), "String expected")
4325  _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)")
4326  self.constructors.append((name, rec_name, args))
4327 

Field Documentation

§ constructors

constructors

Definition at line 4319 of file z3py.py.

§ ctx

ctx

§ name

name

Definition at line 4318 of file z3py.py.