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

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
name,
ctx = None )

Definition at line 5126 of file z3py.py.

5126 def __init__(self, name, ctx=None):
5127 self.ctx = _get_ctx(ctx)
5128 self.name = name
5129 self.constructors = []
5130

Member Function Documentation

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 5131 of file z3py.py.

5131 def __deepcopy__(self, memo={}):
5132 r = Datatype(self.name, self.ctx)
5133 r.constructors = copy.deepcopy(self.constructors)
5134 return r
5135

◆ __repr__()

__repr__ ( self)

Definition at line 5167 of file z3py.py.

5167 def __repr__(self):
5168 return "Datatype(%s, %s)" % (self.name, self.constructors)
5169

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

5170 def create(self):
5171 """Create a Z3 datatype based on the constructors declared using the method `declare()`.
5172
5173 The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
5174
5175 >>> List = Datatype('List')
5176 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5177 >>> List.declare('nil')
5178 >>> List = List.create()
5179 >>> List.nil
5180 nil
5181 >>> List.cons(10, List.nil)
5182 cons(10, nil)
5183 """
5184 return CreateDatatypes([self])[0]
5185
5186

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

5146 def declare(self, name, *args):
5147 """Declare constructor named `name` with the given accessors `args`.
5148 Each accessor is a pair `(name, sort)`, where `name` is a string and `sort` a Z3 sort
5149 or a reference to the datatypes being declared.
5150
5151 In the following example `List.declare('cons', ('car', IntSort()), ('cdr', List))`
5152 declares the constructor named `cons` that builds a new List using an integer and a List.
5153 It also declares the accessors `car` and `cdr`. The accessor `car` extracts the integer
5154 of a `cons` cell, and `cdr` the list of a `cons` cell. After all constructors were declared,
5155 we use the method create() to create the actual datatype in Z3.
5156
5157 >>> List = Datatype('List')
5158 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5159 >>> List.declare('nil')
5160 >>> List = List.create()
5161 """
5162 if z3_debug():
5163 _z3_assert(isinstance(name, str), "String expected")
5164 _z3_assert(name != "", "Constructor name cannot be empty")
5165 return self.declare_core(name, "is-" + name, *args)
5166

◆ declare_core()

declare_core ( self,
name,
rec_name,
* args )

Definition at line 5136 of file z3py.py.

5136 def declare_core(self, name, rec_name, *args):
5137 if z3_debug():
5138 _z3_assert(isinstance(name, str), "String expected")
5139 _z3_assert(isinstance(rec_name, str), "String expected")
5140 _z3_assert(
5141 all([_valid_accessor(a) for a in args]),
5142 "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5143 )
5144 self.constructors.append((name, rec_name, args))
5145

Referenced by Datatype.declare().

Field Documentation

◆ constructors

constructors = []

Definition at line 5129 of file z3py.py.

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

◆ ctx

ctx = _get_ctx(ctx)

Definition at line 5127 of file z3py.py.

Referenced by ArithRef.__add__(), BitVecRef.__add__(), BitVecRef.__and__(), FuncDeclRef.__call__(), AstMap.__contains__(), AstRef.__copy__(), AstVector.__copy__(), FuncInterp.__copy__(), Goal.__copy__(), ModelRef.__copy__(), AstMap.__deepcopy__(), AstRef.__deepcopy__(), AstVector.__deepcopy__(), Datatype.__deepcopy__(), FuncEntry.__deepcopy__(), FuncInterp.__deepcopy__(), Goal.__deepcopy__(), ModelRef.__deepcopy__(), ParamDescrsRef.__deepcopy__(), ParamsRef.__deepcopy__(), Statistics.__deepcopy__(), AstMap.__del__(), AstRef.__del__(), AstVector.__del__(), Context.__del__(), FuncEntry.__del__(), FuncInterp.__del__(), Goal.__del__(), ModelRef.__del__(), ParamDescrsRef.__del__(), ParamsRef.__del__(), ScopedConstructor.__del__(), ScopedConstructorList.__del__(), Solver.__del__(), Statistics.__del__(), ArithRef.__div__(), BitVecRef.__div__(), ExprRef.__eq__(), ArithRef.__ge__(), BitVecRef.__ge__(), AstMap.__getitem__(), AstVector.__getitem__(), ModelRef.__getitem__(), Statistics.__getitem__(), ArithRef.__gt__(), BitVecRef.__gt__(), BitVecRef.__invert__(), ArithRef.__le__(), BitVecRef.__le__(), AstMap.__len__(), AstVector.__len__(), ModelRef.__len__(), Statistics.__len__(), BitVecRef.__lshift__(), ArithRef.__lt__(), BitVecRef.__lt__(), ArithRef.__mod__(), BitVecRef.__mod__(), ArithRef.__mul__(), BitVecRef.__mul__(), BoolRef.__mul__(), ExprRef.__ne__(), ArithRef.__neg__(), BitVecRef.__neg__(), BitVecRef.__or__(), ArithRef.__pow__(), ArithRef.__radd__(), BitVecRef.__radd__(), BitVecRef.__rand__(), ArithRef.__rdiv__(), BitVecRef.__rdiv__(), AstMap.__repr__(), ParamDescrsRef.__repr__(), ParamsRef.__repr__(), Statistics.__repr__(), BitVecRef.__rlshift__(), ArithRef.__rmod__(), BitVecRef.__rmod__(), ArithRef.__rmul__(), BitVecRef.__rmul__(), BitVecRef.__ror__(), ArithRef.__rpow__(), BitVecRef.__rrshift__(), BitVecRef.__rshift__(), ArithRef.__rsub__(), BitVecRef.__rsub__(), BitVecRef.__rxor__(), AstMap.__setitem__(), AstVector.__setitem__(), ArithRef.__sub__(), BitVecRef.__sub__(), BitVecRef.__xor__(), DatatypeSortRef.accessor(), ExprRef.arg(), FuncEntry.arg_value(), FuncInterp.arity(), Goal.as_expr(), Solver.assert_and_track(), Goal.assert_exprs(), Solver.assert_exprs(), QuantifierRef.body(), Solver.check(), Goal.convert_model(), AstRef.ctx_ref(), ExprRef.decl(), ModelRef.decls(), ArrayRef.default(), RatNumRef.denominator(), Goal.depth(), Goal.dimacs(), FuncDeclRef.domain(), ArraySortRef.domain_n(), 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(), SortRef.name(), QuantifierRef.no_pattern(), FuncEntry.num_args(), FuncInterp.num_entries(), Solver.num_scopes(), ModelRef.num_sorts(), FuncDeclRef.params(), QuantifierRef.pattern(), AlgebraicNumRef.poly(), Solver.pop(), Goal.prec(), ModelRef.project(), ModelRef.project_with_witness(), AstVector.push(), Solver.push(), QuantifierRef.qid(), ArraySortRef.range(), FuncDeclRef.range(), DatatypeSortRef.recognizer(), Context.ref(), AstMap.reset(), Solver.reset(), AstVector.resize(), ParamsRef.set(), Solver.set(), AstVector.sexpr(), Goal.sexpr(), ModelRef.sexpr(), Goal.size(), ParamDescrsRef.size(), QuantifierRef.skolem_id(), AstRef.translate(), AstVector.translate(), Goal.translate(), ModelRef.translate(), ParamsRef.validate(), FuncEntry.value(), QuantifierRef.var_name(), and QuantifierRef.var_sort().

◆ name

name = name

Definition at line 5128 of file z3py.py.

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