Dafny - Call methods inside if statements? - if-statement

Like the title says I would like to call on a method that modifies some variables inside an if statement of another method, such as:
method A
...
{
... // Modifies some variables
}
method B
...
{
...
if(statement){
A();
}
...
}
This doesn't work since Dafny won't allow non ghost methods to be called in such a manner. What would a workaround to this issue be?

Figured it out, can cast it to a temporary bool variable and then use the bool variable in the expression:
...
var boolean:bool;
boolean := expression();
is(boolean){
...
}
...

Related

whenNew in Kotlin unit test & Missing calls inside every block

Specifically asking, is there a Kotlin version for this java statement?
whenNew(myClass.class).withAnyArguments().thenReturn(myObject);
I have looked everywhere and haven't found anything that has worked for me yet.
I have tried:
every { myClass(any(), any()) } returns myObject
To this I get this error - argumentA must be in the form m.n. This tells me that blank arguments are being fed to myClass constructor but this is not my motive. I want this class to always return myObject whenever a constructor is being called, irrespective of the arguments.
I have also tried this:
every { myClass(TEST_A, TEST_B) } returns myObject
Then I get this error - Missing calls inside every { ... } block.
Another key point to add is - this is not the class I am writing the test for. I am writing the test cases for myOtherClass (actually defined in kotlin as an object, see below). Within one of the methods in myOtherClass, myClass is instantiated by feeding a set of arguments to its public constructor.
object myOtherClass {
fun mainFunction(a: A, b: B): X {
val answer = getAnswer(a, b)
y = convertAnswerToX(answer)
return y
}
private fun getAnswer(a: A, b: B): myClass {
val a1 = doSomethingToA(a)
val b1 = doSomethingToB(b)
return myClass(a1, b1)
}
}

What makes safe call (question mark) to be interpreted differently from classic if?

In Kotlin, if we declare a class member as var and nullable type, compiler doesn't allow us to run the member function although we put an if statement before calling the function because the compiler can't guarantee that the member isn't been set to null after checking against null and before calling the method.
But if we are using a safe call compiler approves our code.
My question, how the compiler makes the safe call atomic? Isn't a second thread can change the variable between checking for null and calling the method (eat method in the example)?
Code for first situation:
class MyWolf
{
var w : Wolf? = Wolf()
fun myFunction()
{
if (w != null)
{
w.eat()
}
}
}
class Wolf
{
fun eat() : Unit
println("wolf is eating")
}
Code for second situation:
class MyWolf
{
var w : Wolf? = Wolf()
fun myFunction()
{
w?.eat()
}
}
class Wolf
{
fun eat():Unit
{
//code
}
}
The compiler puts the contents of the field to the local variable and then compares it with null. You can clearly see it if you decompile Kotlin bytecode.

static initialization inside lambda (or function) C++

How do I ensure that the initialization of a static field happens only once inside a lambda's body (or a function's)?
[] (string foo) {
static flat_hash_set<string> set;
// code to populate the set with some items.
// Question: how do I ensure this population code executed exactly once?
return set.contains(foo);
}
Static local variables are initialized only once, i.e. only the first time control passes through their declaration. On all further calls, the declaration is skipped. So you can put the code which populates the set into a function (or another lambda), and invoke it and use the returned set as the initializer.
[] (string foo) {
static flat_hash_set<string> set = populate_the_set();
return set.contains(foo);
}
or
[] (string foo) {
static flat_hash_set<string> set = [] () {
flat_hash_set<string> set;
// code to populate the set with some items.
return set;
} ();
return set.contains(foo);
}
One way to do this is to use a helper function that returns the set and initialize the set in the lambda with this
static flat_hash_set<string> set = MyHelperFunction();
You could also use a lambda instead of a helper function to keep the code local to the lambda like
flat_hash_set<string> set = []() { /* populate and return set here */ }();
Another way to do this is use std::call_once and pass a lambda to that which initializes the set.
Personally I would use the second option as it keeps the code local to the lambda and you don't need a global helper function or std::once_flag object

call parent function of mocked class

I have got the following question.
class A
{
public function isNew()
{
return ($this->ID == 0);
}
}
class B extends A
{
//Some functions
}
Now I want to mock Class B. So I have got some statements
$oMockedStm = $this->getMockBuilder('B')->getMock();
$oMockedStm->expects($this->any())->method('someMethod')->will($this->returnValue(TRUE));
$oMockedStm->expects($this->any())->method('anotherMethod')->will($this->returnValue(TRUE));
Now When I do
$this->assertTrue($oMockedStm->isNew());
I get the Error: Failed asserting that null is true.
How can this be. The function always returns true of false.
Does it have something to do with the fact that you can't call parent method of mocked objects?
I figured out that I didn't want to mock the whole class. Only specific functions.
So what you so when defining your mock object is you use the setMethods() function to specify the specific functions you want to mock.
So like this:
$oMockedStm = $this->getMockBuilder('B')
->setMethods(array('someMethod','anotherMethod'))
->getMock();

Mixins with variable number of string arguments in D?

I'm working on some D bindings for an existing C library, and I have a bunch of function definitions, and a bunch of bindings for them. For example:
// Functions
void function(int) funcA;
long function() funcB;
bool function(bool) funcC;
char function(string) funcD;
// etc...
// Bindings
if(!presentInLibrary("func")) return false;
if(!bindFunction(funcA, "funcA")) return false;
if(!bindFunction(funcB, "funcB")) return false;
if(!bindFunction(funcC, "funcC")) return false;
if(!bindFunction(funcD, "funcD")) return false;
// etc...
This model is very similar to how Derelict handles OpenGL extension loading. However, this seems like a lot of redundant typing. I'd really like a way to express the "binding" portion above as something like:
BINDGROUP("func", "funcA", "funcB", "funcC", "funcD", ...); // Name of function group, then variable list of function names.
Is this something that can be done with mixins?
I used this when I was doing dynamic loading, while it doesn't answer your question you may be able to adapt it:
void function() a;
int function(int) b;
void function(string) c;
string bindFunctions(string[] funcs...)
{
string ret;
foreach (func; funcs)
{
ret ~= func ~ ` = cast(typeof(` ~ func ~ `))lib.getSymbol("` ~ func ~ `");`;
}
return ret;
}
mixin(bindFunctions("a", "b", "c"));
Here bindFunctions("a", "b", "c") returns a string that looks something like:
a = cast(typeof(a))lib.getSymbol("a");
b = cast(typeof(b))lib.getSymbol("b");
c = cast(typeof(c))lib.getSymbol("c");
Where lib.getSymbol() returns a pointer from dl_open() etc. Hope this helps.
I assume you meant string mixins? You can just make straight-forward use of D's vararg syntax:
string BINDGROUP(string functionGroup, string[] functions...)
{
// ...
}
mixin(BINDGROUP("func", "funcA", "funcB", "funcC", "funcD"));
I believe this is what you're looking for
template BINDGROUP(string group,T...){
alias BINDGROUP presentInLibrary("func") && BINDGROUPFUNCS!(T);
}
template BINDGROUPFUNCS(T...){
static if(T.length)alias BINDGROUPFUNCS true; // all is successful
else alias BINDGROUPFUNCS bindFunction(mixin(T), T) && BINDGROUPFuncts!(T[1..$]);
}
I'm using recursive template declaration here, you could also do this with foreach loops