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

Constructor & Destructor Documentation

def __init__ (   self,
  name,
  ctx = None 
)

Definition at line 5065 of file z3py.py.

5065  def __init__(self, name, ctx=None):
5066  self.ctx = _get_ctx(ctx)
5067  self.name = name
5068  self.constructors = []
5069 
def __init__
Definition: z3py.py:5065

Member Function Documentation

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 5070 of file z3py.py.

5070  def __deepcopy__(self, memo={}):
5071  r = Datatype(self.name, self.ctx)
5072  r.constructors = copy.deepcopy(self.constructors)
5073  return r
5074 
def __deepcopy__
Definition: z3py.py:5070
def __repr__ (   self)

Definition at line 5106 of file z3py.py.

5106  def __repr__(self):
5107  return "Datatype(%s, %s)" % (self.name, self.constructors)
5108 
def __repr__(self)
Definition: z3py.py:5106
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 5109 of file z3py.py.

Referenced by Datatype.declare().

5109  def create(self):
5110  """Create a Z3 datatype based on the constructors declared using the method `declare()`.
5111 
5112  The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
5113 
5114  >>> List = Datatype('List')
5115  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5116  >>> List.declare('nil')
5117  >>> List = List.create()
5118  >>> List.nil
5119  nil
5120  >>> List.cons(10, List.nil)
5121  cons(10, nil)
5122  """
5123  return CreateDatatypes([self])[0]
5124 
5125 
def CreateDatatypes(ds)
Definition: z3py.py:5150
def create(self)
Definition: z3py.py:5109
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 5085 of file z3py.py.

Referenced by Datatype.create().

5085  def declare(self, name, *args):
5086  """Declare constructor named `name` with the given accessors `args`.
5087  Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort
5088  or a reference to the datatypes being declared.
5089 
5090  In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
5091  declares the constructor named `cons` that builds a new List using an integer and a List.
5092  It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer
5093  of a `cons` cell, and `cdr` the list of a `cons` cell. After all constructors were declared,
5094  we use the method create() to create the actual datatype in Z3.
5095 
5096  >>> List = Datatype('List')
5097  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5098  >>> List.declare('nil')
5099  >>> List = List.create()
5100  """
5101  if z3_debug():
5102  _z3_assert(isinstance(name, str), "String expected")
5103  _z3_assert(name != "", "Constructor name cannot be empty")
5104  return self.declare_core(name, "is-" + name, *args)
5105 
def declare(self, name, args)
Definition: z3py.py:5085
def z3_debug()
Definition: z3py.py:62
def declare_core(self, name, rec_name, args)
Definition: z3py.py:5075
def declare_core (   self,
  name,
  rec_name,
  args 
)

Definition at line 5075 of file z3py.py.

Referenced by Datatype.declare().

5075  def declare_core(self, name, rec_name, *args):
5076  if z3_debug():
5077  _z3_assert(isinstance(name, str), "String expected")
5078  _z3_assert(isinstance(rec_name, str), "String expected")
5079  _z3_assert(
5080  all([_valid_accessor(a) for a in args]),
5081  "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5082  )
5083  self.constructors.append((name, rec_name, args))
5084 
def z3_debug()
Definition: z3py.py:62
def declare_core(self, name, rec_name, args)
Definition: z3py.py:5075

Field Documentation

constructors

Definition at line 5068 of file z3py.py.

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

ctx
name

Definition at line 5067 of file z3py.py.

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