Z3
 
Loading...
Searching...
No Matches
Datatype Class Reference

Public Member Functions

 __init__ (self, name, ctx=None)
 
 __deepcopy__ (self, memo={})
 
 declare_core (self, name, rec_name, *args)
 
 declare (self, name, *args)
 
 __repr__ (self)
 
 create (self)
 

Data Fields

 ctx = _get_ctx(ctx)
 
 name = name
 
list 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 5136 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
name,
ctx = None )

Definition at line 5163 of file z3py.py.

5163 def __init__(self, name, ctx=None):
5164 self.ctx = _get_ctx(ctx)
5165 self.name = name
5166 self.constructors = []
5167

Member Function Documentation

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 5168 of file z3py.py.

5168 def __deepcopy__(self, memo={}):
5169 r = Datatype(self.name, self.ctx)
5170 r.constructors = copy.deepcopy(self.constructors)
5171 return r
5172

◆ __repr__()

__repr__ ( self)

Definition at line 5204 of file z3py.py.

5204 def __repr__(self):
5205 return "Datatype(%s, %s)" % (self.name, self.constructors)
5206

◆ create()

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

5207 def create(self):
5208 """Create a Z3 datatype based on the constructors declared using the method `declare()`.
5209
5210 The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
5211
5212 >>> List = Datatype('List')
5213 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5214 >>> List.declare('nil')
5215 >>> List = List.create()
5216 >>> List.nil
5217 nil
5218 >>> List.cons(10, List.nil)
5219 cons(10, nil)
5220 """
5221 return CreateDatatypes([self])[0]
5222
5223

◆ declare()

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

5183 def declare(self, name, *args):
5184 """Declare constructor named `name` with the given accessors `args`.
5185 Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort
5186 or a reference to the datatypes being declared.
5187
5188 In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
5189 declares the constructor named `cons` that builds a new List using an integer and a List.
5190 It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer
5191 of a `cons` cell, and `cdr` the list of a `cons` cell. After all constructors were declared,
5192 we use the method create() to create the actual datatype in Z3.
5193
5194 >>> List = Datatype('List')
5195 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5196 >>> List.declare('nil')
5197 >>> List = List.create()
5198 """
5199 if z3_debug():
5200 _z3_assert(isinstance(name, str), "String expected")
5201 _z3_assert(name != "", "Constructor name cannot be empty")
5202 return self.declare_core(name, "is-" + name, *args)
5203

◆ declare_core()

declare_core ( self,
name,
rec_name,
* args )

Definition at line 5173 of file z3py.py.

5173 def declare_core(self, name, rec_name, *args):
5174 if z3_debug():
5175 _z3_assert(isinstance(name, str), "String expected")
5176 _z3_assert(isinstance(rec_name, str), "String expected")
5177 _z3_assert(
5178 all([_valid_accessor(a) for a in args]),
5179 "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5180 )
5181 self.constructors.append((name, rec_name, args))
5182

Referenced by declare().

Field Documentation

◆ constructors

constructors = []

Definition at line 5166 of file z3py.py.

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

◆ ctx

ctx = _get_ctx(ctx)

Definition at line 5164 of file z3py.py.

Referenced by AstMap.__contains__(), AstVector.__copy__(), FuncInterp.__copy__(), Goal.__copy__(), ModelRef.__copy__(), AstMap.__deepcopy__(), AstVector.__deepcopy__(), __deepcopy__(), FuncEntry.__deepcopy__(), FuncInterp.__deepcopy__(), Goal.__deepcopy__(), ModelRef.__deepcopy__(), ParamDescrsRef.__deepcopy__(), ParamsRef.__deepcopy__(), Statistics.__deepcopy__(), AstMap.__del__(), AstVector.__del__(), FuncEntry.__del__(), FuncInterp.__del__(), Goal.__del__(), ModelRef.__del__(), ParamDescrsRef.__del__(), ParamsRef.__del__(), ScopedConstructor.__del__(), ScopedConstructorList.__del__(), Solver.__del__(), Statistics.__del__(), AstMap.__getitem__(), AstVector.__getitem__(), ModelRef.__getitem__(), Statistics.__getitem__(), AstMap.__len__(), AstVector.__len__(), ModelRef.__len__(), Statistics.__len__(), AstMap.__repr__(), ParamDescrsRef.__repr__(), ParamsRef.__repr__(), Statistics.__repr__(), AstMap.__setitem__(), AstVector.__setitem__(), FuncEntry.arg_value(), FuncInterp.arity(), Goal.as_expr(), Solver.assert_and_track(), Goal.assert_exprs(), Solver.assert_exprs(), Solver.check(), Goal.convert_model(), ModelRef.decls(), Goal.depth(), Goal.dimacs(), FuncInterp.else_value(), FuncInterp.entry(), AstMap.erase(), ModelRef.eval(), Goal.get(), ParamDescrsRef.get_documentation(), ModelRef.get_interp(), Statistics.get_key_value(), ParamDescrsRef.get_kind(), ParamDescrsRef.get_name(), ModelRef.get_sort(), ModelRef.get_universe(), Goal.inconsistent(), AstMap.keys(), Statistics.keys(), Solver.model(), FuncEntry.num_args(), FuncInterp.num_entries(), Solver.num_scopes(), ModelRef.num_sorts(), Solver.pop(), Goal.prec(), ModelRef.project(), ModelRef.project_with_witness(), AstVector.push(), Solver.push(), AstMap.reset(), Solver.reset(), AstVector.resize(), ParamsRef.set(), Solver.set(), AstVector.sexpr(), Goal.sexpr(), ModelRef.sexpr(), Goal.size(), ParamDescrsRef.size(), AstVector.translate(), Goal.translate(), ModelRef.translate(), ParamsRef.validate(), and FuncEntry.value().

◆ name

name = name

Definition at line 5165 of file z3py.py.

Referenced by __deepcopy__(), and __repr__().