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

Constructor & Destructor Documentation

def __init__ (   self,
  name,
  ctx = None 
)

Definition at line 5009 of file z3py.py.

5009  def __init__(self, name, ctx=None):
5010  self.ctx = _get_ctx(ctx)
5011  self.name = name
5012  self.constructors = []
5013 
def __init__
Definition: z3py.py:5009

Member Function Documentation

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 5014 of file z3py.py.

5014  def __deepcopy__(self, memo={}):
5015  r = Datatype(self.name, self.ctx)
5016  r.constructors = copy.deepcopy(self.constructors)
5017  return r
5018 
def __deepcopy__
Definition: z3py.py:5014
def __repr__ (   self)

Definition at line 5050 of file z3py.py.

5050  def __repr__(self):
5051  return "Datatype(%s, %s)" % (self.name, self.constructors)
5052 
def __repr__(self)
Definition: z3py.py:5050
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 5053 of file z3py.py.

Referenced by Datatype.declare().

5053  def create(self):
5054  """Create a Z3 datatype based on the constructors declared using the method `declare()`.
5055 
5056  The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
5057 
5058  >>> List = Datatype('List')
5059  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5060  >>> List.declare('nil')
5061  >>> List = List.create()
5062  >>> List.nil
5063  nil
5064  >>> List.cons(10, List.nil)
5065  cons(10, nil)
5066  """
5067  return CreateDatatypes([self])[0]
5068 
5069 
def CreateDatatypes(ds)
Definition: z3py.py:5094
def create(self)
Definition: z3py.py:5053
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 5029 of file z3py.py.

Referenced by Datatype.create().

5029  def declare(self, name, *args):
5030  """Declare constructor named `name` with the given accessors `args`.
5031  Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort
5032  or a reference to the datatypes being declared.
5033 
5034  In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
5035  declares the constructor named `cons` that builds a new List using an integer and a List.
5036  It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer
5037  of a `cons` cell, and `cdr` the list of a `cons` cell. After all constructors were declared,
5038  we use the method create() to create the actual datatype in Z3.
5039 
5040  >>> List = Datatype('List')
5041  >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5042  >>> List.declare('nil')
5043  >>> List = List.create()
5044  """
5045  if z3_debug():
5046  _z3_assert(isinstance(name, str), "String expected")
5047  _z3_assert(name != "", "Constructor name cannot be empty")
5048  return self.declare_core(name, "is-" + name, *args)
5049 
def declare(self, name, args)
Definition: z3py.py:5029
def z3_debug()
Definition: z3py.py:64
def declare_core(self, name, rec_name, args)
Definition: z3py.py:5019
def declare_core (   self,
  name,
  rec_name,
  args 
)

Definition at line 5019 of file z3py.py.

Referenced by Datatype.declare().

5019  def declare_core(self, name, rec_name, *args):
5020  if z3_debug():
5021  _z3_assert(isinstance(name, str), "String expected")
5022  _z3_assert(isinstance(rec_name, str), "String expected")
5023  _z3_assert(
5024  all([_valid_accessor(a) for a in args]),
5025  "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5026  )
5027  self.constructors.append((name, rec_name, args))
5028 
def z3_debug()
Definition: z3py.py:64
def declare_core(self, name, rec_name, args)
Definition: z3py.py:5019

Field Documentation

constructors

Definition at line 5012 of file z3py.py.

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

ctx
name

Definition at line 5011 of file z3py.py.

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