How to give the right precondition to prove an assert statemnt in frama-c? - proof

I have been working on some basic programs in c to verify using the frama-c tool.I wanted to know why the assertion is not being proved in the program.Thanks in advance.
#include <limits.h>
/*#requires \valid(a) && \valid(b) && \separated(a,b);
assigns \nothing;
ensures (*a>*b ==> \result == *b) &&
(*b>=*a ==> \result == *a);
*/
int max_ptr(int* a,int* b){
return (*a>*b)?*b:*a;
}
extern int h;
//#assigns x;
int main(){
h=42;
int a=24;
int b=42;
//#assert h ==42;
int x;
x=max_ptr(&a,&b);
//# assert x == 42;
//# assert h ==42;
return 0;
}
All the scheduled goals were successfully proved but except for the assertion statement:
//# assert x == 42;
It was a timeout on the above assertion.Should there be any modifications to the function contract?

The assertion is not proved because it does not hold. Your max_ptr function actually computes the minimum of both pointed values, so x == 24 at the assertion point.
Automatic provers can rarely tell when something is false, instead of simply not being able to prove it.
If you use WP, it will be unable to prove the assertion, but this won't tell you whether it is false, or just hard to prove. Some tools may help finding counterexamples and getting some false statuses to appear, but by default WP's provers will simply say unknown.
Checking some properties using an automatic value analysis, such as Eva
If you run Eva (with -eva), in your program you will get an Invalid property. You can use the GUI to inspect the value of x at the program point (in the Values tab) and you will see that its value is {24}. Then you can backtrack using the GUI to find out when this value got there, using for instance a right-click on the x and then Dependencies -> Show defs.

Related

Does Cplex support using of ln or exponential function for a decision variable?

I am trying to write constraints using ln and exp function, yet I received an error that Cplex can't extract the expression.
forall (t in time)
Gw_C["Mxr"] == 20523 + 17954 * ln(maxl(pbefore[t]));
Ed_c ["RC"]== 0.0422* exp(0.1046* (maxl(pbefore[t])));
Gw_C["RC"] == 3590* pow((maxl(pbefore[t]), 0.6776);
Is there any other possible way to code these constraints on cplex?
Thanks
You may use exp and log if you rely on Constraint Programming within CPLEX:
using CP;
int scale=1000;
dvar int scalex in 1..10000;
dexpr float x=scalex/scale;
maximize x;
subject to
{
exp(x)<=100;
}
execute
{
writeln("x=",x);
}
works fine and gives:
x=4.605
But with Math Programming within CPLEX you cannot use exp like that.
What you can do instead if go through linearization.

Checking power of two for unsigned integer

Consider the following code snippet for checking whether a number is power of 2 or not.
*function to check if x is power of 2*
bool isPowerOfTwo (unsigned int x)
{
return (!(x&(x-1)));
}
What is wrong with above function?
It does reverse of what is required.
It works perfectly fine for all values of x.
It does not work for x = 0
It does not work for x = 1
The correct answer is option 3. but I cannot understand how ?
Shouldn't it be option 4. because 1&(1-1)=0 and !0 = true but 1 is not a power of two hence the function doesn't work for x=1
Edit: sorry I didn't realise 2^0 would be 1.
Got the answer, thanks

What happens when you pass an expression to a function which has passing by value-result?

I'm attending a course of principles of programming languages and there's this exercise where I'm supposed to tell what gets printed by this program:
{
int x=2;
void A (val/res int y)
{
x++;
write(y);
y=y+2;
}
A(x)
A(x+1)
write (x);
}
A is a function with value/result parameters passing, so right before returning it should copy the final value of its formal parameter (y) in the actual parameter. When A first gets called its actual parameter is x, so there's no problem there. However, the second call to A has x+1 as the actual parameter.
What does that mean? Maybe the final value of y gets lost because there's no variable where to copy it? Or maybe I should consider it like an equation, so if the final value of y is 7 I get that x + 1 = 7, and then the value of x is 6?
It means the value of the argument is copied to y:
When x=2, A(x) copies 2 to y at the start of A
When x=4, A(x+1) copies the value of x+1, or 5, to y at the start of A
However, as you pointed, out, passing x+1 for a value/result parameter is problematic, and I would expect any language supporting this type of parameter would not consider it legal, for just the reason you cite. If it is considered legal, how it is accomplished would be up to the language definition; I do not believe there is a standard way to handle this.

Piecewise functions in the Octave symbolic package?

Unlike Matlab, Octave Symbolic has no piecewise function. Is there a work around? I would like to do something like this:
syms x
y = piecewise(x0, 1)
Relatedly, how does one get pieces of a piecewise function? I ran the following:
>> int (exp(-a*x), x, 0, t)
And got the following correct answer displayed and stored in a variable:
t for a = 0
-a*t
1 e
- - ----- otherwise
a a
But now I would like to access the "otherwise" part of the answer so I can factor it. How do I do that?
(Yes, I can factor it in my head, but I am practicing for when more complicated expressions come along. I am also only really looking for an approach using symbolic expressions -- even though in any single case numerics may work fine, I want to understand the symbolic approach.)
Thanks!
Matlab's piecewise function seems to be fairly new (introduced in 2016b), but it basically just looks like a glorified ternary operator. Unfortunately I don't have 2016 to check if it performs any checks on the inputs or not, but in general you can recreate a 'ternary' operator in octave by indexing into a cell using logical indexing. E.g.
{#() return_A(), #() return_B(), #() return_default()}([test1, test2, true]){1}()
Explanation:
Step 1: You put all the values of interest in a cell array. Wrap them in function handles if you want to prevent them being evaluated at the time of parsing (e.g. if you wanted the output of the ternary operator to be to produce an error)
Step 2: Index this cell array using logical indexing, where at each index you perform a logical test
Step 3: If you need a 'default' case, use a 'true' test for the last element.
Step 4: From the cell (sub)array that results from above, select the first element and 'run' the resulting function handle. Selecting the first element has the effect that if more than one tests succeed, you only pick the first result; given the 'default' test will always succeed, this also makes sure that this is not picked unless it's the first and only test that succeeds (which it does so by default).
Here are the above steps implemented into a function (appropriate sanity checks omitted here for brevity), following the same syntax as matlab's piecewise:
function Out = piecewise (varargin)
Conditions = varargin(1:2:end); % Select all 'odd' inputs
Values = varargin(2:2:end); % Select all 'even' inputs
N = length (Conditions);
if length (Values) ~= N % 'default' case has been provided
Values{end+1} = Conditions{end}; % move default return-value to 'Values'
Conditions{end} = true; % replace final (ie. default) test with true
end
% Wrap return-values into function-handles
ValFuncs = cell (1, N);
for n = 1 : N; ValFuncs{n} = #() Values{n}; end
% Grab funhandle for first successful test and call it to return its value
Out = ValFuncs([Conditions{:}]){1}();
end
Example use:
>> syms x t;
>> F = #(a) piecewise(a == 0, t, (1/a)*exp(-a*t)/a);
>> F(0)
ans = (sym) t
>> F(3)
ans = (sym)
-3⋅t
ℯ
─────
9

What's the difference between call by reference and copy/restore

What's the difference in the outcome between call by reference and copy/restore?
Background: I'm currently studying distributed systems. Concerning the passing of reference parameters for remote procedure calls, the book states that: "the call by reference has been replaced by copy/restore. Although this is not always identical, it is good enough". I understand how call by reference and copy/restore work in principle, but I fail to see where a difference in the result may be?
Examples taken from here.
Main code:
#include <stdio.h>
int a;
int main() {
a = 3;
f( 4, &a );
printf("%d\n", a);
return 0;
}
Call by Value:
f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but now nothing is done with x anymore
a += 2*y;
// a is still 3 so the result is 11
}
Value is passed in and has no effect on the value of the variable passed in.
Call by Reference:
f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but because & is used x is the same as a
// meaning if you change x it will change a
a += 2*y;
// a is now 6 so the result is 14
}
Reference is passed in. Effectively the variable in the function is the same as the one outside.
Call with Copy/Restore:
int a;
void unsafe(int x) {
x= 2; //a is still 1
a= 0; //a is now 0
}//function ends so the value of x is now stored in a -> value of a is now 2
int main() {
a= 1;
unsafe(a); //when this ends the value of a will be 2
printf("%d\n", a); //prints 2
}
Value is passed in and has no effect on the value of the variable passed in UNTIL the end of the function, at which point the FINAL value of the function variable is stored in the passed in variable.
The basic difference between call by reference and copy/restore then is that changes made to the function variable will not show up in the passed in variable until after the end of the function while call by reference changes will be seen immediately.
Call by Copy/Restore is a special case of call-by-reference where the provided reference is unique to the caller. The final result on the referenced values will not be saved until the end of the function.
This type of calling is useful when a method in RPC called by reference. The actual data is sent to the server side and the final result will send to the client. This will reduce the traffic, since the server will not update the reference each time.
Call By Reference:
In call-by-reference, we pass a pointer to the called function. Any changes that happens to the data pointed by that pointer will be reflected immediately.
Suppose if there are numerous changes to be made to that data, while it wouldn’t incur much cost locally, it’ll be expensive in terms of network cost as for each change data will have to be copied back to the client.
C Code:
void addTwo(int *arr, int n){
for(int i=0;i<n;i++){
arr[i]+=2; //change is happening in the original data as well
}
}
int main(){
int arr[100]={1,2,3,...}; // assuming it to be initialised
addTwo(arr,100);
}
Call By Copy/Restore:
In call-by-copy/restore, the idea is that when the function is called with the reference to the data, only the final result of the changes made to the data is copied back to the original data(when the function is about to return) without making any changes to the original data during the function call, requiring only one transfer back to the client.
In the C code below, the data pointed by arr is copied in the function and stored back to arr after all the changes to the local data are finalised.
C Code:
void addTwo(int *arr, int n){
// copy data locally
larr = (int*)malloc(n*sizeof(int));
for(int i=0;i<n;i++){
larr[i]=arr[i];
}
for(int i=0;i<n;i++){
// change is happening to the local variable larr
larr[i]+=2;
}
//copy all the changes made to the local variable back to the original data
for(int i=0;i<n;i++){
arr[i]=larr[i];
}
}
int main(){
int arr[100]={1,2,3,...}; // assuming it to be initialised
addTwo(arr,100);
}
Note: Code shown above doesn’t represent actual RPC implementation, just an illustration of the concepts. In real RPC, complete data is passed in the message instead of pointers(addresses).