Z3 optimization: detect unboundedness via API - c++

I am experiencing difficulties in detecting unboundedness of an optimization problem. As stated in the examples and in some answers here, the printed result of an unbounded optimization problem equals to something like "oo", which has to be interpreted (via string compare?).
My question is: Is there any way to use the API to detect this?
I've searched for some time now and the only function, which might do what I want is Z3_mk_fpa_is_infinite(Z3_context c, Z3_ast t) which returns some Z3_ast object. The problem is: Is this the right approach and how do I get the unbounded property out of that Z3_ast object?

There is currently no built-in way to extract unbounded values or infinitesimals.
The optimization engine uses ad-hoc constants called "epsilon" (of type Real) and "oo" (of type Real or Integer) when representing maximal/minimal that
are unbounded or at a strict bound. There is no built-in recognizer for these constants and formally, they don't belong to the domain of Reals. They belong to an extension field. So formally, I would have to return an expression over a different number field or return what amounts to a triple of numerals (epsilon, standard numeral, infinite). For example, a standard numeral 5.6 would be represented as (0, 5.6, 0), and a numeral that comes just below 5.6 is represented as (-1, 5.6, 0), and a numeral that is +infinity is (0, 0, 1). Returning three values instead of one seemed no more satisfactory a solution to me as returning an expression. I am leaving it to users to post-process the expressions that are returned and indeed match for symbols "oo" and "epsilon" to decompose the values if they need so.

Related

The argument of the nan() function

Sometimes there is a need to get NaN value (for example, to return it from a function as an error value). С++11 provides the double nan(const char* tagp); function to get NaN see doc. This function gets C-string argument tagp which:
can be used by library implementations to distinguish different NaN
values in a implementation-specific manner. If this is an empty string (""), the function returns a generic NaN value
I want to understand this function more deeply. Could someone explain in more detail
What tagp values can be used besides empty string ""? Where can I find a list of possible values?
What happens if the passed tagp argument is wrong or not supported by the compiler?
Is it safe to use this implementation-specific function in cross-platform code?
С++11 provides the double nan(const char* tagp);
Some C chapter and verse (C17 § 7.12.11.2)
Description
2 The nan, nanf, and nanl functions convert the string pointed to by tagp according to the following rules. The call nan("n-char-sequence") is equivalent to strtod("NAN(n-char-sequence)", (char**)NULL); the call nan("") is equivalent to strtod("NAN()",(char**)NULL). If tagp does not point to an n-char sequence or an empty string, the call is equivalent to strtod("NAN",(char**)NULL). ...
Returns
3 The nan functions return a quiet NaN, if available, with content indicated through tagp. If the implementation does not support quiet NaNs, the functions return zero.
What tagp values can be used besides empty string ""? Where can I find a list of possible values?
tagp points to a sequence of 0-9, A-Z, a-z, _ characters.
What happens if the passed tagp argument is not supported by the compiler?
If not supported by C/C++ spec, "the call is equivalent to strtod("NAN",(char**)NULL)". This leads to an implementation defined result.
Is it safe to use this implementation-specific function in cross-platform code?
Yes, safe in that undefined behavior is not expected. Yet the meaning and NANs derived are implementation specific.
Interpretation of the n-char-sequence as decimal representation of the NAN payload is a possible outcome subject to limitations above.
See also What uses do floating point NaN payloads have? and wiki NaN

Given Two Regex, Determine if One is a Complement of Other

I'd like to know how you can tell if some regular expression is the complement of another regular expression. Let's say I have 2 regular expressions r_1 and r_2. I can certainly create a DFA out of each of them and then check to make sure that L(r_1) != L(r_2). But that doesn't necessarily mean that r_1 is the complement of r_2 and vice versa. Also, it seems to be that many different regular expressions that could be the same complement of a single regular expression.
So I'm wondering how, given two regular expressions, I can determine if one is the complement of another. This is also new to me, so perhaps I'm missing something that should be apparent.
Edit: I should point out that I am not simply trying to find the complement of a regular expression. I am given two regular expressions, and I am to determine if they are the complement of each other.
Here is one approach that is conceptually simple, if not terribly efficient (not that there is necessarily a more efficient solution...):
Construct NFAs M and N for regular expressions r and s, respectively. You can do this using the construction introduced in the proof that finite automata describe the same languages.
Determinize M and N to get M' and N'. We might as well go ahead and minimize them at this point... giving M'' and N''.
Construct a machine C using the Cartesian product machine construction on machines M'' and N''. Acceptance will be determined by the symmetric difference, or XOR, criterion: accepting states in the product machine correspond to pairs of states (m, n) where exactly one of the two states is accepting in its automaton.
Minimize C and call the result C'
If L(r) = L(s)', then the initial state of C' will be accepting and C' will have all transitions originating in the initial state also terminating in the initial state. If this is the case,
Why should this work? The symmetric difference of two sets is the set of everything in exactly one (not both, not neither). If L(s) and L(r) are complementary, then it is not difficult to see that the symmetric difference includes all strings (by definition, the complement of a set contains everything not in the set). Suppose now there were non-complementary sets whose symmetric difference were the universe of all strings. The sets are not complementary, so either (1) their union is non-empty or (2) their union is not the universe of all strings. In case (1), the symmetric difference will not include the shared element; in case (2), the symmetric difference will not include the missing strings. So, only complementary sets have the symmetric difference equal to the universe of all strings; and a minimal DFA for the set of all strings will always have an accepting initial state with self-loops.
For complement: L(r_1) == !L(r_2)

What is the decimal point ('.') in C++ and can I make one?

I am in a C++ class right now so this question will concern itself primarily with that language, though I haven't been able to find any information for any other language either and I have a feeling whatever the answer is it's probably largely cross language.
In C++ unmarked numbers are assumed to be of integral type ('4', for example, is an integer)
various bounding marks allow for the number to be interpreted differently (''4'', for example, is a character, '"4"' a string).
As far as I know, there is only one kind of unary mark: the decimal point. ('4.' is a double).
I would like to create a new unary mark that designates a constant number in the code to be interpreted as a member of a created datatype. More fundamentally, I would like to know what the '.' and ',' and '"', and ''' are (they aren't operators, keywords, or statements, so what are they?) and how the compiler deals with/interprets them.
More information, if you feel it is necessary:
I am trying to make a complex number header that I can include in any project to do complex math. I am aware of the library but it is, IMHO, ugly and if used extensively slows down coding time. Also I'm mostly trying to code this to improve my programming skills. My goal is to be able to declare a complex variable by doing something of the form cmplx num1= 3 + 4i; where '3' and '4' are arbitrary and 'i' is a mark similar to the decimal point which indicates '4' as imaginary.
I would like to create a new unary mark that designates a constant number in the code to be interpreted as a member of a created datatype.
You can use user defined literals that were introduced in C++11. As an example, assuming you have a class type Type and you want to use the num_y syntax, where num is a NumericType, you can do:
Type operator"" _y(NumericType i) {
return Type(i);
}
Live demo
Things like 4, "4" and 4. are all single tokens,
indivisible. There's no way you can add new tokens to the
language. In C++11, it is possible to define user defined
literals, but they still consist of several tokens; for complex,
a much more natural solution would be to support a constant i,
to allow writing things like 4 + 3*i. (But you'd still need
the C++11 support for constexpr for it to be a compile time
constant.)

In SML, can you convert ".3" to the real "0.3"?

I'm pretty new to SML and I've been using SML/NJ.
Let's say I have the following simple function:
fun test(x) = x / 2.0;
test(0.3); returns 0.15.
I'd like for it to also work with test(.3);
Right now I'm getting the following error:
- test(.3);
stdIn:23.6-23.9 Error: syntax error: deleting DOT INT RPAREN
Of course, I'd like it to work with any real of the form 0.X.
Is this doable? Thank you!
"A real constant is an integer constant, possibly followed by a point (.) and one or more digits, possibly followed by an exponent symbol E and an integer constant; at least one of the optional parts must occur, hence no integer constant is a real constant. Examples: 0.7, +3.32E5, 3E~7 . Non-examples: 23, .3, 4.E5, 1E2.0 ."
from: Definition of Standard ML Version 2 [Robert Harper, Robin Milner, Mads Tofte] 1988
Update:
The Definition of Standard ML (Revised) 1997 modifies the passage to:
an exponent symbol (E or e ) and an integer constant in decimal
notation;
It appears that Reals must have something before the decimal point, even if it's a zero, at least in the implementation of SML that you're using.
I can't find anything about this in the libraries or specification, so it could be specific to the implementation, but it is also true that all of the examples in both of those places do always put a zero before the decimal point, so it may, in fact, be a requirement of the language itself.

SML programming help tuples

exception No_intersection of string
fun check_in ((m1:real, b1:real), (m2:real, b2:real)):real*real =
The function is supposed to check for an intersection between the two lines. Each pair argument is a slope and a y intercept. I am supposed to find the intersection between the two if it is exists.
I can't make to seem this work for some reason, and have been struggling with this for hours.
Reals are not an equality type in SML, so (m1-m2) = 0 is a type error.
The reason for this is that the limited precision of floating-point representations can give unexpected results due to rounding errors (e.g. (1.0/7.7)*7.7 = 1.0 would return false). You can get around this by using the == operator from the Real library, i.e. Real.==(m1-m2,0) (or just Real.==(m1,m2)). But keep in mind that it can be unreliable.
The second problem is that, according to the return type, your function is supposed to return a value, not print it. All you need to do here is state the return value in the else clause, i.e. just replace print((x,y)) with (x,y).
And for what it's worth, I'd avoid using exceptions if you can; they kind of go against the idea of functional programming. Try returning a (real*real) option instead.