parametric data types in z3py - z3

Does z3py have functions to create a parametric data type, like what would be generated using the following SMTLIB code?
( declare - datatype List ( par ( T )
( ( nil ) ( cons ( car T ) ( cdr ( List T )) ))))

Yes. See here: https://ericpony.github.io/z3py-tutorial/advanced-examples.htm
Search for the section titled "Datatypes."
Here's the example right from that page, that does exactly what you want:
def DeclareList(sort):
List = Datatype('List_of_%s' % sort.name())
List.declare('cons', ('car', sort), ('cdr', List))
List.declare('nil')
return List.create()
IntList = DeclareList(IntSort())
RealList = DeclareList(RealSort())
IntListList = DeclareList(IntList)

Related

QUERY simplification with LAMBDA function

I have this formula so I want to make the same QUERY for each variable and join the results. The given formula works (no error in cell) but it give me only the first query result.
I want the result to be QUERYRESULT1 / QUERYRESULT2 / QUERYRESULT3, etc. and i could repeeat the query for each variable, but I'm asking for a way to make it with only one line (to simplify). Is it possible?
=MAP(
BI3:BI;BL3:BL;BO3:BO;BR3:BR;BU3:BU;BX3:BX;CA3:CA;CD3:CD;CG3:CG;CJ3:CJ;CM3:CM;CP3:CP;
LAMBDA(f;g;h;i;j;k;l;m;n;o;p;q;
TEXTJOIN(" / "; TRUE;
IFNA(
ARRAYFORMULA(
IFERROR(QUERY('BDD Componentes'!AR:AV;"SELECT AV WHERE AR = '"&{f;g;h;i;j;k;l;m;n;o;p;q}&"'";0))
)
)
)
)
)
here I am with the same suggestion as in your previous question. Try it and let me know:
=BYROW({BI3:BI\BL3:BL\BO3:BO\BR3:BR\BU3:BU\BX3:BX\CA3:CA\CD3:CD\CG3:CG\CJ3:CJ\CM3:CM\CP3:C};
LAMBDA(r;
TEXTJOIN(" / "; TRUE;
IFNA(
ARRAYFORMULA(
IFERROR(QUERY('BDD Componentes'!AR:AV;"SELECT AV WHERE AR = '"&r&"'";0))
)
)
)
)
)

LUA, divide negative value

For example, I have vaule "a" = -23 who I want to print as -2.3
I write this code who work good when a > 0, also work in other language string_buf = string.format ( "%1d,%d" ,a //10, math.abs(a) %10) but when I use only use "//" then I have one more and instead -2.3 see to -3.3
Where is problem?.
Ok, I try modf. Works.
a= 0xFF16 -- -234
--16bit register is negative?
if (a & 0x8000 ~=0) then
a= (~a +1) & 0xFFFF
a = -a
end
string_buf = string.format ( "%1d,%d" ,math.modf(a/10), math.abs(a)%10)

What's a way to retrive the "path" portion of dxlHere() and go up two dirs?

We are regularly setting up new DOORS installations on standalone networks, and each of these networks use slightly different drive mappings and installation directories. We have a set of DXL scripts that we copy over to each network that uses DOORS, but these DXL scripts reference some Microsoft Word templates that are used as the basis for custom-developed module export scripts.
We no longer have a DXL expert in-house, and I'm trying to make the scripts more portable so that they no
longer contain hard-coded file paths. Because we copy all of the templates and DXL files in a pre-defined directory structure, I can use the dxlHere() function to figure out the execution path of the DXL script, which would print something like this:
<C:\path\to\include\file\includeFile.inc:123>
<C:\path\to\include\file\includeFile.inc:321>
<Line:2>
<Line:5>
<Line:8>
What I'd like to do is extract everything before file\includeFile.inc:123>, excluding the starting <. Then I want to append templates\template.dotx.
For example, the final result would be:
C:\path\to\inclue\template.dotx
Are there any built-in DXL functions to handle string manipulation like this? Is regex the way to go? If so, what regexp would be appropriate to handle this?
Thanks!
I got this... kind of working.
dxlHere is something I don't work with much, but this seems to work- as long as it's saved to a an actual dxl or inc file (i.e. not just run from the editor)
string s = dxlHere()
string s2 = null
string s3 = null
Regexp r = regexp2 ( "\\..*:.*> $" )
Regexp r2 = regexp2 ( "/" )
if ( r s ) {
s2 = s[ 1 : ( ( start ( 0 ) ) - 1 ) ]
s3 = s[ 1 : ( ( start ( 0 ) ) - 1 ) ]
int x = 0
while ( r2 s2 ) {
x++
s2 = s2[ ( ( start ( 0 ) ) + 1 ) : ]
}
int z = 0
for ( y = 0; y <= length( s3 ); y++ ){
if ( s3[y] == '/' ) {
z++
if ( z == ( x - 2 ) ) {
s = s3[ 0 : y ]
break
}
}
}
}
print s
So we're doing a single regexp to check if we have a valid 'location', then running through it to find ever '/' character, then leaving off the last 2 of them.
Hope this helps!

Lua: set of k-dimensional points

I want to make sure that kD points which I generate randomly don't happen to be identical. To test this efficiently, I want to use a set (at least that's what I would do in C++).
I know that in Lua tables can be used as sets by inserting the object as value with any non-nil key (typically just true or an integer for multisets).
But the problem is that tables in Lua are by default just compared w.r.t. their address, which is of course different even if all the vector components (in my case) are equal.
So I thought I'd implement a equal and/or less than metamethod for my points. But this didn't work (see code below). Can anybody help?
local k = 3
local mt = {}
mt.__eq = function( a, b )
for dim = 1, k do
if a[dim] ~= b[dim] then return false end
end
return true
end
mt.__lt = function( a, b )
for dim = 1, k do
if a[dim] < b[dim] then
return true
elseif a[dim] > b[dim] then
return false
end
end
return false -- equal
end
local set = {}
local p1 = { 1, 2, 3 }
setmetatable( p1, mt )
set[p1] = true
local p2 = { 1, 2, 3 }
setmetatable( p2, mt )
set[p2] = true -- should just overwrite the old value
print( "p1 == p2 --> "..tostring( p1 == p2 ) )
print( "p1 < p2 --> "..tostring( p1 < p2 ) )
local setSize = 0
for _, _ in pairs( set ) do
setSize = setSize + 1
end
print( "Size of the set: "..setSize )
Lua has no facilities for allowing what you're trying to do. That is, there's no metamethod trickery that can make two different pieces of userdata or two different tables map to the same index to the table indexing system.
You'll have to do this manually. You can effectively write your own set, by keeping a list of vectors sorted. table.sort can do the job, since you already have an appropriate __lt metamethod.
If anyone is interested: I have come up with another work-around that has worked for me.
One can use a set of x-coordinates and instead of associating the set items with just "true" or "1", the respective values are sets themselves (of y-coordinates); and so on.
This leads to a tree-like structure where the path to each leaf represents a point (sequence of coordinates) and points with the same first coordinates have common parent nodes. Also all points are unique.

How do I get the table length of a lua table in c?

I have a c function that is called from lua. The first parameter is a table. That table is abused as an input array of numbers to an underlying api. So right now my code looks like this:
int n = 0;
lua_pushnil ( L );
while ( lua_next ( L, 2 ) ) {
n++;
lua_pop ( L, 1 );
}
int *flat = alloca ( n * 4 );
lua_pushnil ( L );
int i = 0;
while ( lua_next(L,2) ) {
flat[i++] = (int)lua_tonumber( L, -1 );
lua_pop ( L, 1 );
}
I typed the code blind, so please forgive errors. Also no error checking. But the problem is that I have to do the while loop twice. Is there an easy way to avoid that? I want to optimize for the case where the input is good - a table of ints.
The function you're looking for is unintuitively named lua_objlen, or in Lua 5.2, lua_len (there is a lua_rawlen if you wish to avoid metamethod invocations). It serves many roles (though some, like the length of a string, aren't very useful when you can just use lua_tolstring to get the string and its length), so you should be familiar with it.

Resources