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

Constructor & Destructor Documentation

def __init__ (   self,
  name,
  ctx = None 
)

Definition at line 4202 of file z3py.py.

4202  def __init__(self, name, ctx=None):
4203  self.ctx = _get_ctx(ctx)
4204  self.name = name
4205  self.constructors = []
4206 
def __init__(self, name, ctx=None)
Definition: z3py.py:4202

Member Function Documentation

def __repr__ (   self)

Definition at line 4234 of file z3py.py.

4234  def __repr__(self):
4235  return "Datatype(%s, %s)" % (self.name, self.constructors)
4236 
def __repr__(self)
Definition: z3py.py:4234
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 4237 of file z3py.py.

Referenced by Datatype.declare().

4237  def create(self):
4238  """Create a Z3 datatype based on the constructors declared using the mehtod `declare()`.
4239 
4240  The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
4241 
4242  >>> List = Datatype('List')
4243  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4244  >>> List.declare('nil')
4245  >>> List = List.create()
4246  >>> List.nil
4247  nil
4248  >>> List.cons(10, List.nil)
4249  cons(10, nil)
4250  """
4251  return CreateDatatypes([self])[0]
4252 
def CreateDatatypes(ds)
Definition: z3py.py:4269
def create(self)
Definition: z3py.py:4237
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 4214 of file z3py.py.

Referenced by Datatype.create().

4214  def declare(self, name, *args):
4215  """Declare constructor named `name` with the given accessors `args`.
4216  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.
4217 
4218  In the followin example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
4219  declares the constructor named `cons` that builds a new List using an integer and a List.
4220  It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer of a `cons` cell,
4221  and `cdr` the list of a `cons` cell. After all constructors were declared, we use the method create() to create
4222  the actual datatype in Z3.
4223 
4224  >>> List = Datatype('List')
4225  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
4226  >>> List.declare('nil')
4227  >>> List = List.create()
4228  """
4229  if __debug__:
4230  _z3_assert(isinstance(name, str), "String expected")
4231  _z3_assert(name != "", "Constructor name cannot be empty")
4232  return self.declare_core(name, "is_" + name, *args)
4233 
def declare(self, name, args)
Definition: z3py.py:4214
def declare_core(self, name, rec_name, args)
Definition: z3py.py:4207
def declare_core (   self,
  name,
  rec_name,
  args 
)

Definition at line 4207 of file z3py.py.

Referenced by Datatype.declare().

4207  def declare_core(self, name, rec_name, *args):
4208  if __debug__:
4209  _z3_assert(isinstance(name, str), "String expected")
4210  _z3_assert(isinstance(rec_name, str), "String expected")
4211  _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)")
4212  self.constructors.append((name, rec_name, args))
4213 
def declare_core(self, name, rec_name, args)
Definition: z3py.py:4207

Field Documentation

constructors

Definition at line 4205 of file z3py.py.

Referenced by Datatype.__repr__().

ctx
name

Definition at line 4204 of file z3py.py.

Referenced by Datatype.__repr__().