I got difficulties in using Z3::expr as a map value type. The compiler complains no default constructor can be used. A toy example looks like the following.
#include <z3++.h>
#include <map>
//#include <vector>
using namespace std;
using namespace z3;
int main() {
map <int, expr> m;
context c;
m[1] = c.bool_const("x");
return 0;
}
The Compiler(Clang++) complains
no matching constructor for initialization of 'mapped_type' (aka 'z3::expr')
__i = insert(__i, value_type(__k, mapped_type()));
in instantiation of member function 'std::map<int, z3::expr, std::less<int>,
std::allocator<std::pair<const int, z3::expr> > >::operator[]' requested here
m[1] = c.bool_const("x");
/usr/include/z3++.h:562:9: note: candidate constructor not viable: requires single argument 'c', but no arguments were provided
expr(context & c):ast(c) {}
^
/usr/include/z3++.h:564:9: note: candidate constructor not viable: requires single argument 'n', but no arguments were provided
expr(expr const & n):ast(n) {}
^
/usr/include/z3++.h:563:9: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
expr(context & c, Z3_ast n):ast(c, reinterpret_cast<Z3_ast>(n)) {}
Using a vector to wrap the expr or find method in map as alternatives seems to be fine. Is there any way to use expr as a map value type + [] operator directly?
This is a general issue with map: [] requires an empty constructor for the instance type, see this and this.
(Ugly) Tweak.
You might want to derive a sub-class of map, add a local reference to a z3 context and provide this parameter either through the constructor of map_subclass or a method. Then, you should override [] so that it uses this reference to create a new expr instance with the constructor expr(context &c), instead of attempting to use expr().
My knowledge of z3's internals is limited, but as far as I know this should not break anything.
The idea is bad, since z3 is greedy and assumes it has ownership of all expressions/objects.
Unfortunately you need to use z3::expr_vector and store the offset positions.
You also have an out of bounds error, because you did not use the correct API for insertion into map/z3::vector (insert()/push_back()).
Related
According to Rascal's documentation, the "?" operator can be used to query if a variable is "defined".
For example:
int u=1;
int v; // Defined but uninitialised
u = v?2;
v is uninitialised and therefore u will get the value 2.
However, doing this flags a "Warning: deprecated feature: run-time check on variable initialisation"
Hence the question, what is the non-deprecated way to do what the ? operator did in Rascal?
You can check with the IsDefined operator only things that can in principle be "undefined". Variables are not in that class; they were accidentally and now we are deprecating that behavior. In principle, there exists no null or undefined value in Rascal.
Having said that there are situations with maps and keyword fields of nodes and algebraic constructors where it is possible that a declared name does not exist at runtime. So:
myMap[myKey]?def; // a map does not have to have the key
myCons.myKeywordField?def ; // a keyword field does not have to be set
The isDefined operator is part of the assignment syntax on the left-hand side, as explained here: https://www.rascal-mpl.org/docs/Rascal/Statements/Assignment/IsDefined/
Also, the same syntax can be used as an expression: https://www.rascal-mpl.org/docs/Rascal/Expressions/Values/Boolean/IfDefinedElse/
Again, checking variables for undefinedness does not make sense since variables are always defined in Rascal. It is a static error otherwise. Defined but uninitialized variables are for making matching patterns look more elegant:
int i; int j; // here they are declared with a type
// here they are not defined and may not be used
if (<i, j> := <1,2>) { // here they are bound/defined
// here they can be used
}
// here i and j are not defined again and may not be used
This sounds like an exact duplicate of: Does dart support operator overloading
But the name is misleading, the question is about how to override existing operators (the == operator).
As far as I understand, overloading a function means having multiple implementations that vary only in their parameters, but not in the name of the function:
int max(int a, int b);
double max(double a, double b);
By contrast, overriding means rewriting an existing implementation. Since the original function is replaced, there are no name clashes. This is common in OOP where you extend a base class and override its methods.
The docs say that there are overridable operators. So I see that you can implement custom operators. At the same time, dart does not support overloading methods. So, does dart support overloading operators?
Would it be possible to write the following code:
class Matrix{
Matrix operator+(int b){//...};
Matrix operator+(Matrix b({//...};
}
Yep, you can definitely do that, but you'll need to check the type inside the single method since there can't be duplicate methods for one operator:
class Matrix {
int num = 0;
Matrix(this.num);
Matrix operator+(dynamic b) {
if(b is int) {
return Matrix(this.num + b);
} else if(b is Matrix){
return Matrix(this.num + b.num);
}
}
}
void main() {
print((Matrix(5) + 6).num);
print((Matrix(7) + Matrix(3)).num);
}
You've essentially answered your own question.
there are overridable operators. So I see that you can implement custom operators. At the same time, dart does not support overloading methods. So, does dart support overloading operators?
The Dart language specification says:
10.1.1 Operators
Operators are instance methods with special names.
Dart does not support overloading methods (or functions), operators are equivalent to methods, ergo, Dart does not support operator overloading.
After loading up dartpad, dart does NOT seem to support overloading operators:
class A{
operator*(int b){
print("mul int");
}
operator*(double b){
print("mul double");
}
}
Leads to an error message:
Error compiling to JavaScript:
main.dart:5:11:
Error: '*' is already declared in this scope.
operator*(double b){
In geometry.dart there is the following lines :
/// Unary negation operator.
///
/// Returns an offset with the coordinates negated.
///
/// If the [Offset] represents an arrow on a plane, this operator returns the
/// same arrow but pointing in the reverse direction.
Offset operator -() => Offset(-dx, -dy);
After some research it appears that `-` operator can be used with 0 or 1 parameter, which allows it to be defined twice.
/// Binary subtraction operator.
///
/// Returns an offset whose [dx] value is the left-hand-side operand's [dx]
/// minus the right-hand-side operand's [dx] and whose [dy] value is the
/// left-hand-side operand's [dy] minus the right-hand-side operand's [dy].
///
/// See also [translate].
Offset operator -(Offset other) => Offset(dx - other.dx, dy - other.dy);
This is only due to the fact that - operator can be defined with 0 or 1 parameters.
The following code used to work in Dart 2.6 (very simplified example!):
T plus<T extends num>(T a, T b) => a + b;
Now it fails with: A value of type 'num' can't be returned from method 'plus' because it has a return type of 'T'.
Is there a way to abstract over double/int like intended in the example?
I'm assuming you are passing the --no-implicit-casts flag to the analyzer in order to get this error. The code is correct in the current Dart language, but it contains an implicit cast, so if you choose to disable those, the code will not be accepted.
The reason the code does not work is that the static type of a + b is num (both have type T which is only known to extend num, so they are treated as num, and num.operator+ returns num), and the return type is T which may extend num.
So, the code does an implicit cast from num to T if you allow it.
To make the code compile even without implicit casts, you have to turn it into an explicit cast:
T plus<T extends num>(T a, T b) => (a + b) as T;
Is it possible to store a procedure as a property of a derived type? I was thinking of something along the lines of:
module funcs_mod
public :: add
contains
function add(y,z) result (x)
integer,intent(in) :: y,z
integer :: x
x = y + z
end function
end module
module type_A_mod
use funcs_mod
public :: type_A,set_operator
type type_A
procedure(),pointer,nopass :: operator
end type
contains
subroutine set_operator(A,operator)
external :: operator
type(type_A),intent(inout) :: A
A%operator => operator
end subroutine
function operate(A,y,z) result(x)
type(type_A),intent(in) :: A
integer,intent(in) :: y,z
integer :: x
x = A%operator(y,z)
end function
end module
program test
use type_A_mod
use funcs_mod
type(type_A) :: A
call set_operator(A,add)
write(*,*) operate(A,1,2)
end program
But this doesn't successfully compile. Several errors are displayed including:
1) Syntax error in procedure pointer component
and
2) 'operator' at (1) is not a member of the 'type_a' structure
As well as some unsuccessful use statements. Is there a way to do this correctly? Any help is greatly appreciated.
UPDATE:
I've modified procedure,pointer to procedure(),pointer and now the errors are
1) FUNCTION attribute conflicts with SUBROUTINE attribute in 'operator'
and
2) Can't convert UNKNOWN to INTEGER(4)
Both refer to the line x = A%operator(y,z)
As you have discovered, the syntax for declaring a procedure pointer declaration requires procedure([interface]), pointer [, ...] :: .... You chose procedure(), pointer, nopass :: operator.
The consequence of procedure() is that you are not declaring whether operator is a function or a subroutine. There is nothing untoward in this, but more work then remains in convincing the compiler that you are using the references consistently. Your compiler appears to not believe you.
Rather than go into detail of what the compiler thinks you mean, I'll take a different approach.
You reference A%operator for a structure A of type with that component as the result of the function operate. You say clearly in declaring this latter function that its result is an integer.
Now, assuming that you don't want to do exciting things with type/kind conversion to get to that integer result, we'll take that you always intend for A%operator to be a function with integer result. That means you can declare that procedure pointer component to be a function with integer result.
This still leaves you with choices:
type type_A
procedure(integer),pointer,nopass :: operator
end type
being a function with integer result and implicit interface, and
type type_A
procedure(add),pointer,nopass :: operator
end type
being a function with explicit interface matching the function add.
Your ongoing design choices inform your final decision.
As a final note, you aren't using implicit none. This is important when we consider your line
external :: operator
If operator is a function then (by implicit typing rules) it has a (default) real result. So, you want to change to one of the following
integer, external :: operator
or
procedure(integer) :: operator
or
procedure(add) :: operator
To conclude, and echo the comment by Vladimir F, think very carefully about your design. You currently have constraints from the reference of operate (in the function result and its arguments) that look like you really do know that the component will have a specific interface. If you are sure of that, then please do use procedure(add) as the declaration/
Have this function defined in your module:
module Data
int inc(x) = x + 1;
Type this in the console:
rascal> import Data;
rascal> import List;
This works:
rascal> inc(1);
int: 2
But this does not:
rascal> list[int] y = [1,2,3];
rascal> mapper(y, inc);
|rascal://<path>|: insert into collection not supported on value and int
☞ Advice
But it works if inc(...)'s parameter type is declared:
int inc(int x) = x + 1;
So why does not having this type declaration work for using the inc(...) function directly, but not for passing that function to mapper(...)?
Because Rascal's type checker is still under development, you are not warned if you make a small mistake like forgetting to provide a type for a function parameter. It may still work, accidentally, in some circumstances but you are guaranteed to run into trouble somewhere as you've observed. The reason is that type inference for function parameters is simply not implemented as a feature. This is a language design decision with the intent of keeping error messages understandable.
So, this is not allowed:
int f(a) = a + 1;
And, it should be written like this:
int f(int a) = a + 1;
I consider it a bug that the interpreter doesn't complain about an untyped parameter. It is caused by the fact that we reuse the pattern matching code for both function parameters and inline patterns. [edit: issue has been registered at https://github.com/cwi-swat/rascal/issues/763]
In your case the example works because dynamically the type of the value is int and addition does not check the parameter types. The broken example breaks because the interpreter does checks the type of the function parameter at the call-site (which defaulted to value for the untyped parameter).