Nesting Arrays in Datatypes #7334
Replies: 4 comments
-
Quoting from Section 10.4 of https://www.cs.colostate.edu/~cs440/spring19/slides/z3-tutorial.pdf:
The reason for this has to do with well-formedness. You can think of arrays as functions, and the cardinality of the resulting type cannot be modeled in ordinary set-theory based semantics. (It can be modeled using CPOs/domain-theory, but that's not what SMTLib is based on.) |
Beta Was this translation helpful? Give feedback.
-
The restriction is usually to negative occurrences of the datatype inside nested arrays / functions, which I what I thought was being referenced in the z3 tutorial line "Similarly, it may be convenient to use a nesting of algebraic data-types and arrays. Z3 supports nesting ADTs over sequences and over ranges of arrays.". As an example Experimenting with the smtlib prompt it appears this definition is accepted, but I do not know how to replicate in the z3 python or C api
For context on my application, I was trying to create a universal "Any" datatype for which I could encode many of the possible sorts. A poor man's generic/dynamic typing. Using an uninterpreted Name sort, this could perhaps encode some variation of PHOAS, and also similar types show up in interaction trees / iteratees (although I think these are usually coinductive). |
Beta Was this translation helpful? Give feedback.
-
This is confusing indeed. Perhaps the trick is to "stage" the creation? from z3 import *
MyType = Datatype('MyType')
MyType.declare('mk_nil')
MyTypeSort = MyType.create()
MyType.declare('mk_mytype', ('children', ArraySort(IntSort(), MyTypeSort)))
print(MyType.constructors) This prints: [('mk_nil', 'is-mk_nil', ()), ('mk_mytype', 'is-mk_mytype', (('children', Array(Int, MyType)),))] which doesn't look wrong. But it feels hacky, and I haven't played around with it to see if it actually works. Would be good to experiment. Would love to hear if you try this out and see if it actually works. |
Beta Was this translation helpful? Give feedback.
-
Playing it a bit: from z3 import *
MyType = Datatype('MyType')
MyType.declare('mk_nil')
MyTypeSort = MyType.create()
MyType.declare('mk_mytype', ('children', ArraySort(IntSort(), MyTypeSort)))
MyType = MyType.create()
x = Const("x", MyType)
s = Solver()
s.add(x != MyType.mk_nil)
print(s.check())
print(s.model()) prints:
which seems reasonable. But if I try: print(s.sexpr()) before the call to |
Beta Was this translation helpful? Give feedback.
-
How do I make a datatype that nests the datatypes into the result of an array in the python bindings? https://microsoft.github.io/z3guide/docs/theories/Datatypes/#nested-datatypes-with-arrays-and-sequences
The following does not work.
ArraySort
gets mad because it is expected aSortRef
not aDatatype
Beta Was this translation helpful? Give feedback.
All reactions