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

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
name,
ctx = None )

Definition at line 5154 of file z3py.py.

5154 def __init__(self, name, ctx=None):
5155 self.ctx = _get_ctx(ctx)
5156 self.name = name
5157 self.constructors = []
5158

Member Function Documentation

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 5159 of file z3py.py.

5159 def __deepcopy__(self, memo={}):
5160 r = Datatype(self.name, self.ctx)
5161 r.constructors = copy.deepcopy(self.constructors)
5162 return r
5163

◆ __repr__()

__repr__ ( self)

Definition at line 5195 of file z3py.py.

5195 def __repr__(self):
5196 return "Datatype(%s, %s)" % (self.name, self.constructors)
5197

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

5198 def create(self):
5199 """Create a Z3 datatype based on the constructors declared using the method `declare()`.
5200
5201 The function `CreateDatatypes()` must be used to define mutually recursive datatypes.
5202
5203 >>> List = Datatype('List')
5204 >>> List.declare('cons', ('car', IntSort()), ('cdr', List))
5205 >>> List.declare('nil')
5206 >>> List = List.create()
5207 >>> List.nil
5208 nil
5209 >>> List.cons(10, List.nil)
5210 cons(10, nil)
5211 """
5212 return CreateDatatypes([self])[0]
5213
5214

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

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

◆ declare_core()

declare_core ( self,
name,
rec_name,
* args )

Definition at line 5164 of file z3py.py.

5164 def declare_core(self, name, rec_name, *args):
5165 if z3_debug():
5166 _z3_assert(isinstance(name, str), "String expected")
5167 _z3_assert(isinstance(rec_name, str), "String expected")
5168 _z3_assert(
5169 all([_valid_accessor(a) for a in args]),
5170 "Valid list of accessors expected. An accessor is a pair of the form (String, Datatype|Sort)",
5171 )
5172 self.constructors.append((name, rec_name, args))
5173

Referenced by declare().

Field Documentation

◆ constructors

constructors = []

Definition at line 5157 of file z3py.py.

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

◆ ctx

ctx = _get_ctx(ctx)

Definition at line 5155 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 5156 of file z3py.py.

Referenced by __deepcopy__(), and __repr__().